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 :(
Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..
Quote:
Originally Posted by
boblingwide
Q1. this is my codes using "scan.next();"
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();"
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.
Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..
Quote:
Originally Posted by
Tolls
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();
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.
Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..
Quote:
Originally Posted by
Tolls
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!
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.
Re: Q> about "scan.nextLine();" and "scan.next();" i cant understand..
thanks Tolls and Norms, i got it already thnx!:(clap):