简体   繁体   中英

Java Scanning to Line End

I am trying to scan just one whole in in a file using the Scanner .

The problem is I need to write a loop that only scans the one line.

For example the line is:

Hello world it is nice out

I want it to save that whole sentence in a String without continuing on and reading the rest of the file. Therefore:

while(charscan.hasNext()) {
    String test= charscan.next() + " ";        
}

Does not work.

 while(charscan.hasNext()){
        String test= charscan.nextLine() + " ";
        break;

  }

If you are interested with only one line and don't want to use a loop, then use an if statement instead ie

if(charscan.hasNext()){
    String test = charscan.nextLine() + " ";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM