Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By Norm

Thread: Typing dobule name in the String.

  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Typing dobule name in the String.

    Hi, I am making a program where the user types in how many students, the student name and the student result.
    My problem is typing in a dobule name for instance: Tom Tom.
    I have tried both:

    String name = input.Next(); -> Only works with one name.
    and
    String name = input.nextLine(); -> Skips right trough and ask me to type result again.

    Here is my code:

    Java Code:
    import java.util.Scanner;
    
    public class c5e8 {
    public static void main(String[]args){
    	Scanner input = new Scanner (System.in);
    	int highestResult = 0;
    	String highestName = " ";
    	
    	
    	
    	//Type in number of students.
    	System.out.print("Write the number of students ");
    	int students = input.nextInt();
    	
    	
    	int count;
    	for (count = 0; count <students; count++){
    		System.out.print("Type in the result ");
    		int result = input.nextInt();
    		System.out.println("Type in Student name");
    		String name = input.nextLine();
    		
    		//Change the highest result.
    		if( result > highestResult){
    			highestResult = result;
    			highestName = name;		
    		}	
    	}
    	System.out.println("The highest score is  " + highestResult 
    			+ "by " + highestName);
    	
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Typing dobule name in the String.

    There is a problem mixing the use of Scanner methods like next() or nextInt() with nextLine().
    The next methods can leave a newline character in Scanner's buffer. The newline character will be returned by the next call to nextLine(), usually as an empty String.
    A solution is to call nextLine() to clear the buffer before trying to use it to read data.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Typing dobule name in the String.

    Quote Originally Posted by Norm View Post
    There is a problem mixing the use of Scanner methods like next() or nextInt() with nextLine().
    The next methods can leave a newline character in Scanner's buffer. The newline character will be returned by the next call to nextLine(), usually as an empty String.
    A solution is to call nextLine() to clear the buffer before trying to use it to read data.
    Hi Norm,
    Thanks for your reply. I have had this problem in multiple programs now.
    Ive read your reply a few times now, trying to figure out how I can make my program work, unfortually I am not sure what I should add to my code. How do I do this?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Typing dobule name in the String.

    Do some experimenting in a small program to see how to use nextLine() to clear the newline character from the buffer.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Typing dobule name in the String.

    Thank you for your reply Norm,
    I must admit, I dont really understand what you mean by "use nextLine() to clear the newline character from the buffer".
    Is it possible to explain it in an easier way or with an example?
    Thank you very much.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Typing dobule name in the String.

    If you use nextInt or nextDouble or next() then you shouldn't generally use nextLine().
    This is because they work differently.
    The last one reads a line up to and including the next new line.
    The others all read up to, but not including, the next token (which in your case includes the new line character(s))...this leaves that token in the buffer, so when you call nextLine() it returns a blank line.

    Replace your use of nextLine() with next(), or add another nextLine() call to consume that new line character. The former is probably neater.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Typing dobule name in the String.

    Did you try writing a small test program to test various combinations of the Scanner class's methods?
    Tolls likes this.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Typing dobule name in the String.

    To facilitate your testing, you may want to start with something like this:

    Java Code:
    String input = " 10 20 \n 30 Foo 40 60.2 50\n";
    Scanner in = new Scanner(input);
    Then call various appropriate methods of the Scanner class and print the output.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Typing dobule name in the String.

    Hi guys,
    Thanks alot for your help and your advice.

    I have tried a few ways of doing this now, and I found a way that works. using 2 x next();
    But I am not sure if this is correct way of doing it?

    Java Code:
    import java.util.Scanner;
    
    public class c5stringdoublename {
    	public static void main(String[]args){
    		Scanner input = new Scanner (System.in);
    		
    		System.out.print("Type in an int :");
    		int i1 = input.nextInt();
    		
    		System.out.print("Type in a String(Double name) :");
    		String s1 = input.next();
    		String s2 = input.next();
    		
    		System.out.print(i1 + " " +  s1 +" " + s2);
    	}
    }

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Typing dobule name in the String.

    You don't need 2 next() calls.
    One should do it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Typing dobule name in the String.

    Quote Originally Posted by Tolls View Post
    You don't need 2 next() calls.
    One should do it.
    If I only use one and type in forinstance "Tom Tomsen" as the name, it only prints the "Tom".
    If I used two It did print "Tom Tomsen".

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Typing dobule name in the String.

    The two calls to next() will work if the names always have two parts. But what it there is only one part?
    Or three parts?
    If the whole name is typed on one line then the nextLine() method will read all the parts into a String.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Typing dobule name in the String.

    Ah yes, I just reread the OP, and realised what I missed...

    And this is another reason I try and avoid Scanner for all but the most trivial problems.
    Read the line, parse the line, using a BufferedReader, IMO.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Typing dobule name in the String.

    I don't know why the Scanner class was designed that way. It seems to cause problems for most students.
    Skipping over new-line characters when using next methods one after the other but stopping at new-line characters when nextLine() is used.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Typing dobule name in the String.

    Quote Originally Posted by Norm View Post
    The two calls to next() will work if the names always have two parts. But what it there is only one part?
    Or three parts?
    If the whole name is typed on one line then the nextLine() method will read all the parts into a String.
    If it only has two parts it works yes, but any other alternative, one, three etc then it becomes problematic again, so I guess my attempt is not a solution to the problem I am having.
    And it does not work with nextLine() either, so to be honest I am not sure how to get this to work, even with all the tips and advice.

    Java Code:
    import java.util.Scanner;
    
    public class c5stringdoublename {
    	public static void main(String[]args){
    		Scanner input = new Scanner (System.in);
    		
    		System.out.print("Type in an int :");
    		int i1 = input.nextInt();
    		
    		System.out.print("Type in a String(Double name) :");
    		String s1 = input.nextLine();
    
    		
    		System.out.print(i1 + " " +  s1);
    	}
    }
    Gives me this output:
    Type in an int :5
    Type in a String(Double name) :5

    Unsure how to solve this problem =/

  16. #16
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Typing dobule name in the String.

    Quote Originally Posted by Tolls View Post
    Ah yes, I just reread the OP, and realised what I missed...

    And this is another reason I try and avoid Scanner for all but the most trivial problems.
    Read the line, parse the line, using a BufferedReader, IMO.
    I have not had any experience with BufferedReader so far, I can look into this.
    Is my problem solvable by the use of Scanner?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Typing dobule name in the String.

    Yes, the Scanner class can be used, but you need to handle the new-line character if you use the nextLine() method.

    how to get this to work
    Write a testing program that uses many different combinations of calls to next methods and the nextLine() method.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Typing dobule name in the String.

    So I need to remove the new-line character, in my example the 5, before I give the user the chance to type in its name?
    This is doable with the right combination of calls to the next and nextLine method?
    Have I understood it correctly so far?

    Thanks alot for helping out, I am asking a lot of questions now, some might be stupid, but I am learning alot and the help from the forum is great :)

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Typing dobule name in the String.

    Quote Originally Posted by Thammer View Post
    I have not had any experience with BufferedReader so far, I can look into this.
    Is my problem solvable by the use of Scanner?
    Post #6, last sentence, gives two options.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Typing dobule name in the String.

    Java Code:
    import java.util.Scanner;
    
    public class c5stringdoublename {
    	public static void main(String[]args){
    		Scanner input = new Scanner (System.in);
    		
    		System.out.print("Type in an int :");
    		int i1 = input.nextInt();
    		
    		System.out.print("Type in a String(Double name) :");
    		String s1 = input.nextLine();
    		String s2 = input.nextLine();
    		
    		System.out.print(i1 + " " +  s2);
    	}
    }
    Thanks alot for the help guys, with the code above it seems like the next-line char is consumed by the s1 string, and the s2 string gives the user the chance to type in any length of the string.
    Last edited by Thammer; 10-28-2014 at 01:15 PM.

Similar Threads

  1. Issue with typing
    By AlexGraal in forum Eclipse
    Replies: 1
    Last Post: 04-10-2014, 08:02 AM
  2. problem with typing game
    By beginner123 in forum New To Java
    Replies: 1
    Last Post: 11-20-2012, 09:46 PM
  3. typing application
    By beginner123 in forum New To Java
    Replies: 16
    Last Post: 11-18-2012, 01:08 PM
  4. Typing into terminal
    By keo in forum New To Java
    Replies: 1
    Last Post: 03-15-2011, 09:09 AM
  5. Replies: 5
    Last Post: 10-30-2010, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •