Results 1 to 7 of 7
  1. #1
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Q> about "scan.nextLine();" and "scan.next();" i cant understand..

    Q1. this is my codes using "scan.next();"

    import java.util.Scanner;
    class cute
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    System.out.print("Your Name: ");
    String inname = scan.next();
    System.out.print("Your Age: ");
    int inage = scan.nextInt();
    System.out.print("Your Crush: ");
    String incrush = scan.next();
    System.out.print("Your Crush Age: ");
    int incrushage = scan.nextInt();
    System.out.print(inname+inage+" LOVES "+incrush+incrushage);
    }
    }
    ---- output --------------
    Your Name: piolo
    Your Age: 25
    Your Crush: mariane
    Your CrushAge: 25
    piolo25 LOVES mariane25
    ---------------------------



    Q2. using "scan.nextLine();"

    import java.util.Scanner;

    class cute
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    System.out.print("Your Name: ");
    String inname = scan.nextLine();
    System.out.print("Your Age: ");
    int inage = scan.nextInt();
    System.out.print("Your Crush: ");
    String incrush = scan.nextLine();
    System.out.print("Your Crush Age: ");
    int incrushage = scan.nextInt();
    System.out.print(inname+inage+" LOVES "+incrush+incrushage);
    }
    }
    -------- Output ------
    Your Name: piolo
    Your Age: 25
    Your Crush: Your Crush Age: 25
    piolo25 LOVES 25
    -----------------------

    my question is when i use scan.nextLine in the 2nd String in crush i cant use the scanner and go directly to the
    next which is "Your Crush Age:"

    is there a big diference in ".nextLIne()" and ".next()"?
    i tot its they same that accept string?
    im a nobie im sory, plz help me mods :(

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..

    Quote Originally Posted by boblingwide View Post
    Q1. this is my codes using "scan.next();"
    Java Code:
    import java.util.Scanner;
    class cute
    {
    	public static void main(String[] args)
    	{
    		Scanner scan = new Scanner(System.in);
    		System.out.print("Your Name: ");
    		String inname = scan.next();
    		System.out.print("Your Age: ");
    		int inage = scan.nextInt();
    		System.out.print("Your Crush: ");
    		String incrush = scan.next();
    		System.out.print("Your Crush Age: ");
    		int incrushage = scan.nextInt();
    		System.out.print(inname+inage+" LOVES "+incrush+incrushage);
    	}
    }
    ---- output --------------
    Your Name: piolo
    Your Age: 25
    Your Crush: mariane
    Your CrushAge: 25
    piolo25 LOVES mariane25
    ---------------------------



    Q2. using "scan.nextLine();"
    Java Code:
    import java.util.Scanner;
    
    class cute
    {
    	public static void main(String[] args)
    	{
    		Scanner scan = new Scanner(System.in);
    		System.out.print("Your Name: ");
    		String inname = scan.nextLine();
    		System.out.print("Your Age: ");
    		int inage = scan.nextInt();
    		System.out.print("Your Crush: ");
    		String incrush = scan.nextLine();
    		System.out.print("Your Crush Age: ");
    		int incrushage = scan.nextInt();
    		System.out.print(inname+inage+" LOVES "+incrush+incrushage);
    	}
    }
    -------- Output ------
    Your Name: piolo
    Your Age: 25
    Your Crush: Your Crush Age: 25
    piolo25 LOVES 25
    -----------------------

    my question is when i use scan.nextLine in the 2nd String in crush i cant use the scanner and go directly to the
    next which is "Your Crush Age:"

    is there a big diference in ".nextLIne()" and ".next()"?
    i tot its they same that accept string?
    im a nobie im sory, plz help me mods :(
    Please use code tags.

    Anyway, this is down to how Scanner reads things. It's a mix of an BufferedReader (nextLine is the equivalent of readLine()) and a tokeniser (the default split is on new lines I think).
    The next() one is simply picking up new tokens, which is fine. It tends to leave the new line in the buffer, though. The next call to it throws that new line away (though I'd have to read the code to check this) and reads the next token (up to the next newline).
    Now the nextLine() reads a whole line in (up to the new line), but it sees the newline left behind by any prior next() call and thinks "blank line".

    This is pretty much down to the token used in this case (a new line) being the same as, well, a new line.

    I don't know if I've explained that terribly well, though.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..

    Quote Originally Posted by Tolls View Post
    Please use code tags.

    Anyway, this is down to how Scanner reads things. It's a mix of an BufferedReader (nextLine is the equivalent of readLine()) and a tokeniser (the default split is on new lines I think).
    The next() one is simply picking up new tokens, which is fine. It tends to leave the new line in the buffer, though. The next call to it throws that new line away (though I'd have to read the code to check this) and reads the next token (up to the next newline).
    Now the nextLine() reads a whole line in (up to the new line), but it sees the newline left behind by any prior next() call and thinks "blank line".

    This is pretty much down to the token used in this case (a new line) being the same as, well, a new line.

    I don't know if I've explained that terribly well, though.
    im confuse, ahm so if im using many String, ex. 6 string, the format shud be this?
    String a = scan.nextLine();
    String b = scan.next();
    String c = scan.next();
    String d = scan.next();
    String e = scan.next();
    String f = scan.next();

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..

    Take this sequence:
    next
    next
    nextLine

    For the following input:
    something
    somethingelse
    and again

    The first next reads the 'something', but the newline is still in the buffer.
    The second next clears the buffer then reads 'somethingelse', and its newline is in the buffer.
    The nextline call comes in and reads that newline. It does not clear the buffer.
    This means the 'and again' is not read.

    This is all down to the token used to split values being, I believe the default is, a new line.

    So in your sequence (from your code):
    nextLine
    nextInt
    nextLine
    nextInt

    The first nextLine is OK, since there won't be a newline in the buffer so it reads the name.
    The nextInt call leaves a newline.
    This is then eaten by the nextLine call, thus missing the second name.
    The following nextInt call clears the buffer and reads the age.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..

    Quote Originally Posted by Tolls View Post
    Take this sequence:
    next
    next
    nextLine

    For the following input:
    something
    somethingelse
    and again

    The first next reads the 'something', but the newline is still in the buffer.
    The second next clears the buffer then reads 'somethingelse', and its newline is in the buffer.
    The nextline call comes in and reads that newline. It does not clear the buffer.
    This means the 'and again' is not read.

    This is all down to the token used to split values being, I believe the default is, a new line.

    So in your sequence (from your code):
    nextLine
    nextInt
    nextLine
    nextInt

    The first nextLine is OK, since there won't be a newline in the buffer so it reads the name.
    The nextInt call leaves a newline.
    This is then eaten by the nextLine call, thus missing the second name.
    The following nextInt call clears the buffer and reads the age.
    ahh my mind is getting more clearer now, thnx!

    so if im using many string this is the sequence
    next
    next
    next
    next
    nextline

    ahm i have a question, i compared with int, why int has a fixed sequence?
    nextInt
    nextInt
    nextInt
    nextInt
    nextInt

    why in using int, nextInt(), nextInt(), nextInt() its always clear the bufer, but in String u nid to use always next(),next(),nextLine()?

    sory for this noob question, i just want to know the proper coding in java, not just ordinary but i want to know the proper codings..
    thank you for your time!

    im willing to learn from you..thx!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..

    A good way to see how the code works is to write a small simple testing program that uses the different methods, prints out what each method reads, and then execute the program.
    Type in different things and see what happens. Try one word and press Enter.
    Then put several words on one line and press Enter and see what is read by each of the methods.
    Also test some of the methods whose names begin with has... to see what they return after you have typed in something.
    The more different tests you try, the more you will understand.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..

    thanks Tolls and Norms, i got it already thnx!

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. Scan code for "errors"
    By phixion in forum New To Java
    Replies: 0
    Last Post: 03-14-2011, 01:09 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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