# Strings; Extremely Basic Issue

• October 26th, 2013, 09:03 PM
Bojangles
Strings; Extremely Basic Issue
So, I'm a university student and extremely new to this. I have a professor that isn't exactly the best at teaching anything, so all of the learning is done entirely on my own through the text book.

The goal is to create a program that has the user enter their first name, last name, and year of birth. It should then give that information back to them, including the age in years, along with their maximum heart rate, and their target heart rate change. The maximum heart rate is determined by 220 - age in years, and the heart rate change is 50-85% of their maximum heart rate.

I know the issue can be solved with strings somehow, but I'm having a hard time understanding this concept. Any solutions or general guidance would be greatly appreciated. I'm sure this should be embarrassingly easy to do properly, but this is my attempt at making it work:

Code Java:

```import java.util.Scanner;   public class HeartRatesTest { public static void main( String[] args ) { Scanner input = new Scanner(System.in );   String FirstName; String LastName; String BirthYear; int Age; int MaxRate; int LowerRateChange; double HigherRateChange;   System.out.print( "Enter first name: " ); FirstName = input.nextLine();   System.out.print( "Enter last name: " ); LastName = input.nextLine();   System.out.print( "Enter year of birth: "); BirthYear = input.nextLine();   Age = 2013 - BirthYear;   MaxRate = 220 - Age;   LowerRateChange = MaxRate / 2; HigherRateChange = MaxRate * 0.85;   System.out.printf( "Hello, %d %d! You are %d years old.\nYour maximum heart rate is %d.\nYour target heart rate change is %d-%d.", FirstName, LastName, Age, MaxRate, LowerRateChange, HigherRateChange);   input.close(); } }```

I get an error in the section where I'm subtracting BirthYear from 2013, and I understand that is because I can't subtract the string from an int. I think. I just don't know how to make that.. not be an issue.

I've been working at this for longer than I'd like to admit using the book and the Internet as a guide, but I just can't seem to take it anywhere beyond this.
• October 26th, 2013, 09:49 PM
Junky
Re: Strings; Extremely Basic Issue
You are trying to subtract a String from an int. Does 100 - "cat" make sense to you? The Scanner class has methods for reading other types of data. Perhaps try one of them instead of reading year in as a String.

BTW, blaming your teaching doesn't wash. If the teacher covers something in class that you don't understand then the onus is upon you to approach them and seek clarification. I could attend a lecture given by Stephen Hawking and not understand a thing he says. Is it fair that I say "That Hawking sucks!"?
• October 26th, 2013, 10:05 PM
Bojangles
Re: Strings; Extremely Basic Issue
I understand that I can't subtract a string from an int. I said exactly that. What methods are you referring to for the Scanner class?

BTW, I have sought clarification from the professor. He is awful at explaining things. Often times, he loses track of what he's even trying to convey, and gets confused by his own slides and notes during lectures. When he does, which is every few slides, he just skips them and doesn't go back. When I ask him for help and e-mail him, I still don't end up getting the answers I came for.

No, it wouldn't be fair for you to say that about Hawking. But did I say that about my professor? I did not; I simply said that he's not the best at teaching, which is true.
• October 26th, 2013, 10:20 PM
Junky
Re: Strings; Extremely Basic Issue
You are currently using the nextLine method. Where did you find out about that method? Do you think there is a place that lists and explains all the methods that a class has?

Your problems with the teacher are yours. Don't whine about it to us. It has no bearing on your problem or the level of help you get. In fact it is likely to decrease your chances of getting help. Just concentrate on your code and asking a specific question about it.
• October 26th, 2013, 10:36 PM
Bojangles
Re: Strings; Extremely Basic Issue
I spoke two sentences about it originally, and the rest was responding to what you said to me. No need to be an asshole.
• October 26th, 2013, 10:47 PM
Junky
Re: Strings; Extremely Basic Issue
Quote:

Originally Posted by Bojangles
No need to be an asshole.

Ditto.

I was trying to give you advice on the best way to get help. Would you be willing to help someone who keeps coming up to you and whining about how crappy their life is? But if you don't want any help then feel free to continue whining. Or you can concentrate on your problem. Which is it?
• October 26th, 2013, 11:06 PM
Bojangles
Re: Strings; Extremely Basic Issue
Man, you really are grumpy and fond of exaggeration. I'd like help, I suppose. What else can you give me, Junky?
• October 26th, 2013, 11:24 PM
Junky
Re: Strings; Extremely Basic Issue
I have already given you what you need. Read the Java API for the Scanner class an see what methods it has. In particular you want to be able to read an integer value.
• October 28th, 2013, 08:46 AM
satyarlenka
Re: Strings; Extremely Basic Issue
Age = 2013 - BirthYear;
here you have declared Age and integer type variable which can store only integer data. Now, BirthYear is string type, which is why you cannot subtract it from an integer data. You cant use typecast too(find out yourself). Converting the string to integer should work.
Try Using Integer.parseInt()
• October 28th, 2013, 06:07 PM
Bojangles
Re: Strings; Extremely Basic Issue
Thank you for this, but I actually figured it out yesterday after I deleted all of it and started over.

Now, I have another simple issue but I didn't want to take up space and make a whole new thread for it.

Basically, I want encrypt to equal Tencrypted3, Tencrypted4, Tencrypted1, Tencrypted2, in that order.

The goal of this assignment is to have the user enter a four digit number, and then separate each number individually. Add seven to each single digit, then divide by 10 and replace the original digit with this new one. We then swap digit 1 & 3, as well as digit 2 & 4. Afterwards, we are to reverse the process to get back to the original number.

I have it completed all the way up to getting the mixed up number. My issue is getting the mixed up number stored as its own int, so I can then use it to reverse the process. It's probably super obvious, but nothing I've tried has worked. That's all I need, nothing more.

Code Java:

```import java.util.Scanner;   public class Encrypt { public static void main( String args[] ) { Scanner input = new Scanner(System.in );   int number; int number1; int number2; int number3; int number4;   int ecrypted;   int encrypted1;//These are used for the first step in encryption, adding 7 int encrypted2; int encrypted3; int encrypted4;   int Tencrypted1;//These are used for the second step in encryption, getting remainder int Tencrypted2;//It seems like there's probably a more efficient way to achieve this but it works int Tencrypted3; int Tencrypted4;   number = 99999;   while (number >= 9999 || number <= 999) { System.out.print("Please enter a four digit number: "); number = input.nextInt(); if (number >= 9999) { System.out.println("That's not a four digit number.\n"); } if (number <= 999) { System.out.println("That's not a four digit number.\n"); } }   number4 = number % 10; number3 = number / 10 % 10; number2 = number / 100 % 10; number1 = number / 1000 % 10;   encrypted1 = number1 + 7; encrypted2 = number2 + 7; encrypted3 = number3 + 7; encrypted4 = number4 + 7;   Tencrypted1 = encrypted1 % 10; Tencrypted2 = encrypted2 % 10; Tencrypted3 = encrypted3 % 10; Tencrypted4 = encrypted4 % 10;   System.out.printf("\nBAM:\n%d%d%d%d", Tencrypted3, Tencrypted4, Tencrypted1, Tencrypted2); encrypted =   input.close(); } }```

There's probably a much more convenient way to set up this code as well, but I'm not worried about efficiency in this case. My goal is only to make it work.