Home > Enterprise >  How to get the scan.nextLine() without skipping it
How to get the scan.nextLine() without skipping it

Time:05-01

public void readDrinks(File file) throws DrinkReaderException{
            try {
                Scanner scan = new Scanner(file);
                scan.useDelimiter("::");
                String name,category;
                int price;
                boolean premium;
                while(scan.hasNextLine()) {
                    name = "";category = ""; price = -1;
                    if(scan.hasNext())
                        name = scan.next();
                    if(scan.hasNext())
                        category = scan.next();
                    if(scan.hasNextInt())
                        price = scan.nextInt();
                    if(scan.hasNextBoolean())
                        premium = scan.nextBoolean();
                    else
                        premium = false;
                    System.out.println(name   category   price   premium);
                    if(name.isEmpty() || category.isEmpty()) {
                        System.out.printf("--Skip the item: %s\n");
                        continue;
                    }
                    if(price == -1 ) {
                        System.out.printf("--Incorrect price skip: %s\n");
                        continue;
                    }
                    readerList.add(new Drink(name,category,price,premium));
                }
                scan.close();
            }
            catch(FileNotFoundException e) {
                readFileUI();
            }
    }

I'm not sure on how to make it so that if the object lack name or category it will print itself like "--Skip the item: Iced Coffe::30" because if I were to make a = scan.nextLine() and then put a as %s in name.isEmpty || category.isEmpty it will skip the current line to the next line which will make scan.next() scan.nextInt() scan.nextBoolean() reading the next Line

this is the file of "list of item" I need to do

Name::Category::0::true
Espresso::Hot Coffee::35::false
Chocolate Milk Shake::Cool Drink::60::true 
Hot Plain Milk::Hot Milk::false 
Honey Lemon Iced Tea::Ice Tea::40::false
Double Chocolate Milk Shake::Iced Milk::50::true
Hot Chinese Tea::Hot Tea::30:: false
Iced Caramel Milk::Iced Milk::40
Iced Mocha::Iced Coffee::40::false
Iced Coffee::30
Hot Americano::Hot Coffee::40::false

and this is the result I was hoping to make enter image description here

  • Related