mercredi 4 décembre 2019

How to get multiple of strings from a text from a Scanner in Java?

I've been working on this for a while. I am trying to get several of strings to set them from a scanner that has text in it. Now, my code is suppose to be able to take in three important stuff from the text from the scanner. name, closed and the point, but I need to leave them out and just get the rest of the line. The tricky part is that it can be in any order so I need to be able to do it regardless of the order. I've been working on this for a while and I can't use the string [] since the name could possibly attain a last name, therefore I need the scanner to read the rest of the line.

Here is what I have so far in my code:

private String name;
private boolean closedness;

public con(Scanner read)
{
 while(read.hasNext())
    {
      if(read.hasNextLine())
      {
        if (read.next().contains("name"))
        {
          name = read.nextLine().replaceAll("\\s+", "");
        }
        if(read.next().contains("true"))
        {
           closedness = true;
        }
      }
    }
}

Right now, I am getting an exception error.

What is in the scanner is

name jack

closed false

endleg

Now keep in mind, it could also have

closed false

name max rodriguez

endleg

It needs to be able to do it in any order and set it to my private methods. let me know.

Aucun commentaire:

Enregistrer un commentaire