Results 1 to 7 of 7
- 04-05-2012, 02:04 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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 :(
- 04-05-2012, 02:46 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..
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.
- 04-05-2012, 03:42 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..
- 04-05-2012, 04:26 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 04-05-2012, 05:40 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..
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!
- 04-05-2012, 07:10 PM #6
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.
- 04-06-2012, 08:29 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
Scan code for "errors"
By phixion in forum New To JavaReplies: 0Last Post: 03-14-2011, 01:09 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks