Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the end of a line with a Scanner?

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

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

//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();

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

like image 425
Samuel French Avatar asked Feb 08 '12 23:02

Samuel French


4 Answers

If you want to use only Scanner, you need to create a temp string instantiate it to nextLine() of the grid of data (so it returns only the line it skipped) and a new Scanner object scanning the temp string. This way you're only using that line and hasNext() won't return a false positive (It isn't really a false positive because that's what it was meant to do, but in your situation it would technically be). You just keep nextLine()ing the first scanner and changing the temp string and the second scanner to scan each new line etc.

like image 188
Jon Avatar answered Nov 08 '22 11:11

Jon


Lines are usually delimitted by \n or \r so if you need to check for it you can try doing it that way, though I'm not sure why you'd want to since you are already using nextLine() to read a whole line.

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

like image 29
Ian Dallas Avatar answered Nov 08 '22 11:11

Ian Dallas


Wow I've been using java for 10 years and have never heard of scanner! It appears to use white space delimiters by default so you can't tell when an end of line occurs.

Looks like you can change the delimiters of the scanner - see the example at 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();
like image 1
davidfrancis Avatar answered Nov 08 '22 12:11

davidfrancis


you can use the method hasNextLine to iterate the file line by line instead of word by word, then split the line by whitespaces and make your operations on the word

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();
like image 1
Daniel Camarda Avatar answered Nov 08 '22 11:11

Daniel Camarda