Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Strings; Extremely Basic Issue

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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:

    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.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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!"?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Strings; Extremely Basic Issue

    Quote Originally Posted by Bojangles View Post
    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?
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  9. #9
    Junior Member satyarlenka's Avatar
    Join Date
    May 2013
    Location
    Bhubaneswar IND
    Posts
    7
    My Mood
    Inspired
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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()

  10. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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.

Similar Threads

  1. Replies: 4
    Last Post: April 27th, 2013, 05:10 PM
  2. Simple CashRegister program behaviour issue [Basic]
    By Moa in forum Java Theory & Questions
    Replies: 6
    Last Post: June 19th, 2012, 03:49 PM
  3. Extremely basic -- help with multiple arguments
    By jhnhskll in forum Object Oriented Programming
    Replies: 9
    Last Post: January 17th, 2012, 04:25 PM
  4. Basic loop issue
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 23rd, 2011, 05:10 PM