如何在不跳过的情况下获取 scan.nextLine()

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

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();
            }
    }

我不确定如何制作它,以便如果对象缺少名称或类别,它将自己打印为“--跳过该项目:Iced Coffe::30” 因为如果我要制作 a = scan.nextLine() 然后将 a as %s in name.isEmpty || category.isEmpty 它将跳过当前行到下一行,这将使 scan.next() scan.nextInt() scan.nextBoolean() 读取下一行

这是我需要做的“项目列表”文件

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

这就是我希望得到的结果 enter image description here

我基本上会保持与您相同的结构,但我不会使用一个 Scanner 我会使用其中的两个:一个读取每个文件的行,第二个(您的)从每一行获取数据.

public void readDrinks(File file) throws DrinkReaderException {
        try {
            Scanner scanLines = new Scanner(file);
            String line, name, category;
            int price;
            boolean premium;

            while (scanLines.hasNextLine()) {
                //EDIT: added a trim to get rid of eventual spaces at the end and properly read a boolean
                line = scanLines.nextLine().trim();

                Scanner scanParams = new Scanner(line);
                scanParams.useDelimiter("::");

                name = "";
                category = "";
                price = -1;

                if (scanParams.hasNext())
                    name = scanParams.next();
                if (scanParams.hasNext())
                    category = scanParams.next();
                if (scanParams.hasNextInt())
                    price = scanParams.nextInt();
                if (scanParams.hasNextBoolean())
                    premium = scanParams.nextBoolean();
                else
                    premium = false;
                System.out.println(name + " - " + category + " - " + price + " - " + premium);
                if (name.isEmpty() || category.isEmpty()) {
                    System.out.printf("--Skip the item: %s%n", line);
                    continue;
                }
                if (price == -1) {
                    System.out.printf("--Incorrect price skip: %s%n", line);
                    continue;
                }
                readerList.add(new Drink(name, category, price, premium));
            }
            scanLines.close();
        } catch (FileNotFoundException e) {
            readFileUI();
        }
    }

输出