Need help finding the end of a line with a scanner

I have a scanner in my program that reads in parts of a file and formats them for HTML. When I read my file, I need to know how to make the scanner know that it is at the end of the line and start writing to the next line.

Here is the relevant part of my code, let me know if I left something:

//scanner object to read the input file Scanner sc = new Scanner(file); //filewriter object for writing to the output file FileWriter fWrite = new FileWriter(outFile); //Reads in the input file 1 word at a time and decides how to ////add it to the output file while (sc.hasNext() == true) { String tempString = sc.next(); if (colorMap.containsKey(tempString) == true) { String word = tempString; String color = colorMap.get(word); String codeOut = colorize(word, color); fWrite.write(codeOut + " "); } else { fWrite.write(tempString + " "); } } //closes the files reader.close(); fWrite.close(); sc.close(); 

Well, I found out about sc.nextLine (); but I still don't know how to determine when I am at the end of a line.

+4
source share
4 answers

If you want to use only the scanner, you need to create a temp line to create it in nextLine () from the data grid (so that it returns only the missing line) and a new Scanner object that scans the temp line. That way, you only use this string, and hasNext () will not return a false positive result (this is not a false positive, because that is what it should have done, but in your situation it would be technically). You simply hold nextLine () in the first scanner and change the temp line and the second scanner to scan each new line, etc.

+5
source

Wow, I've been using java for 10 years and have never heard of a scanner! Apparently, space separators are used by default, so you cannot determine when the end of a line occurs.

It looks like you can change the scanner delimiters - see the example in the Scanner class :

  String input = "1 fish 2 fish red fish blue fish"; Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*"); System.out.println(s.nextInt()); System.out.println(s.nextInt()); System.out.println(s.next()); System.out.println(s.next()); s.close(); 
+1
source

Lines are usually limited to \n or \r , so if you need to check this out, you can try to do it this way, although I'm not sure why you want it, since you are already using nextLine() to read the whole line.

There is Scanner.hasNextLine() if you are worried that hasNext() not working for your specific case (not sure why it won't, though).

+1
source

you can use the hasNextLine method to iterate line by line instead of word by word, then divide the line into spaces and do your word operations

here is the same code using hasNextLine and split

 //scanner object to read the input file Scanner sc = new Scanner(file); //filewriter object for writing to the output file FileWriter fWrite = new FileWriter(outFile); //get the line separator for the current platform String newLine = System.getProperty("line.separator"); //Reads in the input file 1 word at a time and decides how to ////add it to the output file while (sc.hasNextLine()) { // split the line by whitespaces [ \t\n\x0B\f\r] String[] words = sc.nextLine().split("\\s"); for(String word : words) { if (colorMap.containsKey(word)) { String color = colorMap.get(word); String codeOut = colorize(word, color); fWrite.write(codeOut + " "); } else { fWrite.write(word + " "); } } fWrite.write(newLine); } //closes the files reader.close(); fWrite.close(); sc.close(); 
+1
source

Source: https://habr.com/ru/post/1395515/


All Articles