mardi 27 novembre 2018

How to read predefined number of lines from scanner input based on multiple conditions for the input line

I need to read maximum 10 lines, with each lines having only characters * and . also with other conditions like begin with * and length 10. the last line that should be read should have a line following it with the word END

  Scanner keyboard = new Scanner(System.in);
    int countLine = 0;
    String line = "";
    while (countLine < 10) {
        line = keyboard.nextLine();
        countLine++;
        if (line.matches("[*.]+") && !line.equals("END") && line.startsWith("*") && line.length() < 10
               ) {
          // do something
        } else {
            break;
        }
    }
    keyboard.close();

Aucun commentaire:

Enregistrer un commentaire