Can someone help me to figure out how to read my file from ":" (not included) til the end of the line?

Andrea Manisi :

My exercise ask to open the file sales.dat, and read only the number part of every line. All of this using the Scanner class

i've tried with while loops, for loops and every other possible solution on my book but none works

public static void main(String[] args) {
        String nomeFile = "sales.dat";
        Scanner inputStream = null;
        System.out.println("Il file "+ nomeFile + "\ncontiene le righe seguenti:\n");

        try {
            inputStream = new Scanner(new File(nomeFile));
            inputStream.useDelimiter(":");
            while(inputStream.hasNext()) {
                String riga = inputStream.nextLine();
                System.out.println(riga);
            }
        } catch (FileNotFoundException e) {
            System.out.println("Errore nell'apertura del file " + nomeFile);
            System.exit(0);
        }

        inputStream.close();
    }
}

Expected: if line is San Francisco: 198870.32 it must read and print only 198870.32

Actual: San Francisco: 198870.32
        Chicago: no report received
        New York: 298734.12
Roshana Pitigala :

Remove the line

inputStream.useDelimiter(":");

You may now read the whole line and then use split().

String riga = inputStream.nextLine(); //San Francisco: 198870.32
System.out.println(riga.split(":")[1].trim());

this should give you 198870.32.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161386&siteId=1