mardi 13 février 2018

Errors in I/O with Scanner class (JAVA)

I have to write a program where the user write the price of an item and, when the users press ctrld the program will print the lower price (without the name of the item). The program doesn't print anythink or mismatch. What can i do? This should be the output:

INPUT

apples 10
pears 20
grapes 5

OUTPUT

5

Here is the code:

import java.util.Scanner;
public class StampaMinimoParola {
    public static void main(String[] args) {
        Scanner s=new Scanner (System.in);
        String name="";
        int lower=Integer.MAX_VALUE;
        int price;

        while (s.hasNext()) { //start of the while loop
            name=s.nextLine(); //name of the article
            price=s.nextInt();//price of the article

            if (price<lower) {
                System.out.println(price);

            }
            //end of while cycle
        }

    }

}

Aucun commentaire:

Enregistrer un commentaire