0

If I am scanning from a text

Scanner s= new Scanner("texto.txt");

// I want to compare the next char from the line with a <

// like this:

if(s.nextChar().equals("<")){
.....

I know that s.nextChar() does not exist but there is any similar thing to use in this case?

Rong Nguyen
  • 3,913
  • 4
  • 25
  • 50

3 Answers3

2

Your code would something like...

Scanner s= new Scanner("texto.txt");
s.useDelimiter("");
while (s.hasNext()) {
    if(s.nextChar()=='<'){
 .....
} 

Note that after the call of s.nextChar(), the value is actually fetched, so its better to keep the variable, if you would like to use it further, like:

char ch = s.nextChar();
Gyanendra Dwivedi
  • 5,105
  • 1
  • 23
  • 49
0

Consider dumping Scanner and using FileReader:

FileReader fileReader = new FileReader("textto.txt");

int charRead
while( (charRead = fileReader.read()) != -1)
{
   if(charRead == '<')
   {
      //do something
   }
}
lreeder
  • 10,862
  • 2
  • 50
  • 61
0
      FileReader reader = null;
  try {
     reader = new FileReader("");
     int ch = reader.read() ; 
     while (ch != -1) {
        // check for your char here
     }
  } catch (FileNotFoundException ex) {
     //
  } catch (IOException ex) {
     //
  } finally {
     try {
        reader.close();
     } catch (IOException ex) {
        //
     }
  }