如何使用while循环从文本文件中读取 [英] how to use a while loop to read from text file

查看:57
本文介绍了如何使用while循环从文本文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够从文件中读取 3 个单独的文本块,并使用 while 循环将它们(包括计算)显示到控制台.目前我只能显示一个文本块.

I need to be able to read 3 separate blocks of text from a file and display them including the calculations to the console using a while loop. At the moment I can only get one block of text to display.

我无法弄清楚如何在此处完全按照文本文件中的格式对其进行格式化,因此请原谅图像.文本文件:

I couldn't figure out how to format it on here exactly like it is in the text file so excuse the image. Text File:

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    FileReader input = new FileReader ("rooms.txt");
    Scanner console = new Scanner(System.in);
    Scanner read = new Scanner (input);
    final double defaultTax = .20;
    
    
    System.out.println("Do you wish to specify a custom tax rate? (yes/no): ");
    
     if (console.next().equals("yes")) {
         System.out.println("What would you like the tax rate to be?");
     }      
    //use while loop
     
    else
     {

        while (read.hasNextLine()) {
        
        String roomType = read.nextLine();
        int rooms = read.nextInt();
        double price = read.nextDouble();
        double totalIncome = (double) (rooms*price);
        double tax = (double) (price*rooms*defaultTax);
    System.out.println("Room type: " + roomType + " | No. of rooms: " + rooms + " | Room price: " + price + " | income: " + totalIncome + " | tax: " + tax);
        }
     }
}

电流输出:房间类型:单人 |房间数:5 |房间价格:23.5 |收入:117.5 |税:23.5

CURRENT OUTPUT: Room type: Single | No. of rooms: 5 | Room price: 23.5 | income: 117.5 | tax: 23.5

所需的输出将包括所有数据,包括计算.

The desired output would include all of the data, including the calculations.

Current error message:
    Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at cwk/cwk.test1.main(test1.java:31)

我实际上只是一名大学生,在某个模块的某个部分寻求帮助.我不是程序员,也不是在学习编程课程.请不要删除我的问题.

I am literally just a uni student looking for a bit of help on a bit of one module. I'm not a programmer, I'm not doing a programming course. please don't delete my question.

推荐答案

该问题与空格有关.当您调用 nextLine 时,它将读取整行,直到遇到 \n 或文件末尾.当您调用 nextIntnextDouble 时,它将跳过任何前面的空格,但之后不会消耗任何空格.

The issue is related to whitespace. When you call nextLine it will read a whole line, until it encounters \n or the end of the file. When you call nextInt and nextDouble it will skip any preceding whitespace, however it will not consume any whitespace after.

所以你的 while 循环的第一次迭代工作正常.然而,当 nextLine 被调用时的下一次迭代,则返回一个空字符串.之后,当 nextInt 被调用时,它遇到 "Double" 并且因此你得到一个 InputMismatchException.

So the first iteration of your while loop is working fine. However the next iteration when nextLine is called, then an empty string is returned. After that when nextInt is called, then it encounters "Double" and thus you get an InputMismatchException.

解决此问题的最简单方法是,在 while 循环结束时,您可以跳过所有后续空格:

The easiest way to fix this, is that at the end of your while loop, you can skip all subsequent whitespace:

while (read.hasNextLine()) {
    // Keep everything you already have

    read.skip("\\s+");
}

这篇关于如何使用while循环从文本文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆