How to use Scanner methods when scanning a String?

I have a text file that consists of a few lines of a small list of integers separated by spaces. I was to scan the first line, and do something for each integer that is in the first line.

    String lineString = textScan.nextLine(); //scans the first line of the text file.
    Scanner Line = new Scanner(lineString); //scans the String retrieved from the text file.

    while (Line.hasNextInt())
    { //do stuff 
    }

So I have a scanner (textScan) that scans the first line of the text file and then saves it as a String. Then I have a second scanner that scans through the String to check if the values in it are integers.

But the while statement doesn't allow me to use "Line.hasNextInt" to scan through the String! So how do I scan through the String line to figure out if it has integers or not?

EDIT: Sorry, my mistake in wording. The loop will infinitely run, and so I tried creating a print statement before the loop: System.out.println(Line);, and it prints out this error:

java.util.Scanner[delimiters=p{javaWhitespace}+][position=0][match valid=false] [need input=false][source closed=false][skipped=false][group separator=,][decim al separator=.][positive prefix=][negative prefix=Q-E][positive suffix=][nega tive suffix=][NaN string=Q?E][infinity string=Q∞E]

The odd thing is that it compiles fine?

The input file:

5 4 4 3 5 
4 2 2 5 3 
5 2 5 2 3 
5 4 3 2 3 
5 4 2 2 5 
4 4 2 4 2 
3 3 5 3 5 
5 2 4 5 2 
4 4 5 4 2 
2 4 3 5 2 
3 3 3 5 3 
2 4 5 3 4 
3 5 5 4 3 
3 4 2 2 4 
5 5 5 4 4 
3 4 4 4 5 
3 2 4 2 4 
5 4 4 2 4 
5 3 5 2 3 

I see the issue. You need to use Line.nextInt() inside the loop to move the cursor.

If not, the Line always points to the beginning and the loop never ends.

To use it correctly, call nextInt() so it picks up the next token:

while (Line.hasNextInt())
{ 
     System.out.println(Line.nextInt());
}

Infinite Loop:

while (Line.hasNextInt())
{ 
     System.out.println("Infinity");
}

You don't necessary need to solve that problem using scanner.

public static void main(String[] args) {

    String myString = "This is not int";
    String myInt = "123456";
    int copyOfInt = 0;
    try {
        copyOfInt = Integer.parseInt(myInt);
        System.out.println(copyOfInt);
        copyOfInt = Integer.parseInt(myString);
        System.out.println(myString);
    } catch (Exception e) {
        System.out.println("Not int");
    }
    // Put this in a loop and you can keep doing stuff
}

Or you can use FileInputStream, which I think is better. Here is an example:

    try (FileInputStream readFile = new FileInputStream(refFile);
            InputStreamReader readIn = new InputStreamReader(readFile, "UTF8")) {
        BufferedReader ReadBuffer = new BufferedReader(readIn);
        String line = "";
        while ((line = ReadBuffer.readLine()) != null) {
            // Search my string of interest 
            if (line.equals("Potential/V, Current/A")) {
                while ((line = ReadBuffer.readLine()) != null) {
                    // Get data after string of interest 
                    // Use createDataType_1_Object() to get my data
                    myDataArray.add(createDataType_1_Object(line));
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }

很抱歉,如果您在同一行中有多个整数,可以使用分隔符分隔它们并仍然使用单个扫描器,则可能会误解您的问题:

String yourFilename = "filename.txt";

File file = new File(yourFilename).useDelimiter(" ");
Scanner sn = new Scanner(file);

while(sn.hasNextInt()){
// Do your work here
}
链接地址: http://www.djcxy.com/p/96062.html

上一篇: Scanner#nextFloat的怪异行为

下一篇: 扫描字符串时如何使用扫描仪方法?