scanner system.in useDelimiter doubt

Category: Java SE
 
gxfengry2009
2012-03-17 12:17:53

Sponsored Links

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.useDelimiter(",");
while (sc.hasNext()) {
System.out.println(sc.next());
}
sc.close();
}
}



input 1,2,3,4,5
result
1
2
3
4

Finally her how gone


Sponsored Links

jpc07
2012-03-17 12:36:50
When the Scanner to read data from System.in, call next () or hasnext (), if no data, the system will fall into waiting, waiting for you to input from the keyboard. In other words, you put hasnext () writes the while loop, the program will be stalled because of lack of data and the

Scanner System.in to read data from, or you know exactly what you will enter several data, call a few next ()
Either you read a line, then this line of decomposition, such as:
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        Scanner Scanner = new Scanner(s);
        Scanner.useDelimiter(",");
        while(Scanner.hasNext()) {
         System.out.println(Scanner.next());
        }
kaze13
2012-03-17 12:47:06
5 back to Canada,
just_one_time_1
2012-03-17 12:54:28
but

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(
new BufferedReader(new FileReader("12345.txt")));
sc.useDelimiter(",");
while (sc.hasNext()) {
System.out.println(sc.next());
}
sc.close();
}
}


12345.txt's content is
1,2 , 3,4,5

the output
1
2
3
4
5
zhhs1970
2012-03-17 01:12:27
how do you explain the results of TXT
s82127
2012-03-17 01:24:12
not already press Enter to confirm my input string yet, this should really I want to enter the content of it?

import java.util.Scanner;


public class Test {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
System.out.println(scan.next());
}

}
}



why I entered 12345 / / 5 is not followed by whitespace
the output is
1
2
3
4
5
while but it's not for a separator, it contains carriage return in the spaces, carriage returns should be \ n \ r right?
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.