我的bufferedreader只读取我文件的第一行?

时间:2014-02-09 12:31:48

标签: java bufferedreader

我在代码中使用的bufferedreader似乎只读取代码的第一行。有人可以帮助我解决问题,我已经尝试了很长时间。

import java.util.Scanner;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;

public class Task2Recipe {

    private static String Ingredient;
    private static String ServingNumber;


    public static void main(String[] args) {

        Scanner user_input = new Scanner(System.in);
        System.out.println("Hello. If you would like to write a new recipe, please type in 'write', if you would like to change and view a recipe, please type in 'read'");
        String choice = user_input.next();
        user_input.nextLine();

        if (choice.equals("write")) {
            write();
        }

        if (choice.equals("read")) {
            read();
        }
    }

    public static void write() {

        try {
            FileWriter Task2Recipe = new FileWriter("P:/Year 11/GCSE Computing/A453/Task 2/Recipe.txt");
            BufferedWriter recipe = new BufferedWriter(Task2Recipe);
            Scanner user_input = new Scanner(System.in);

            System.out.println("Please enter the name of your recipe, if more than 1 word, seperate your words with a dash");
            String RecipeName = user_input.next();
            recipe.write("Name of recipe: " + RecipeName);
            recipe.newLine();

            System.out.println("Please enter the number of people your recipe serves");
            ServingNumber = user_input.next();
            recipe.write(ServingNumber);
            recipe.newLine();

            System.out.println("Please enter the name of your first ingredient, the quantity and units separated with a comma");
            Ingredient = user_input.next();
            recipe.write(Ingredient);
            recipe.newLine();

            System.out.println("Do you want to enter another ingredient? yes/no? Please type in either in lower case");
            String choice2 = user_input.next();

            user_input.nextLine();
            while (choice2.equals("yes")) {

                System.out.println("Please enter the name of your ingredient, the quantity and units separated with a comma");
                Ingredient = user_input.nextLine();
                recipe.write(Ingredient);

                System.out.println("Do you want to enter another ingredient? yes/no? Please type in either in lower case");
                choice2 = user_input.next();
                user_input.nextLine();
            }
            recipe.close();
        } catch (Exception e) {
            System.out.println("A write error has occured");

        }
    }

    public static void read() {

        try {
            Scanner user_input = new Scanner(System.in);
            FileReader file = new FileReader("P:Year 11/GCSE Computing/A453/Task 2/Recipe.txt");
            BufferedReader buffer = new BufferedReader(file);

            System.out.println("Would you like to change the serving number of your recipe, type in 'yes' to proceed, type in 'no'");
            String choice3 = user_input.next();
            user_input.nextLine();
            while (choice3.equals("yes")) {

                String line;
                System.out.println("Please enter the new serving number");
                int NewServingNumber = user_input.nextInt();

                int counter = 0;

                while ((line = buffer.readLine()) != null) {

                    counter++;

                    if (counter == 2) {
                    }

                    if (counter > 3) {

                        String[] word = Ingredient.split(",");
                        int Quantity = Integer.parseInt(word[1]);
                        int ServingNumberInt = Integer.parseInt(ServingNumber);
                        int Multiplier = ServingNumberInt / Quantity;
                        int NewQuantity = (Multiplier * NewServingNumber);

                        System.out.println("Your new quantity is " + NewQuantity);
                    }
                }

                System.out.println(line);

                buffer.close();
            }
        } catch (Exception e) {
            System.out.println("A read error has occured");
        }
    }
}

我的意见是:

applep - 用于食谱名称

10 - 服务号码

苹果,10,苹果 - 对于这种成分,我只添加了1种成分。

当我读取并读取我的文件并更改配方服务号码时,它不起作用并给出“读取错误”。另外,为了测试这个问题,我打印了变量'line',它似乎只是读取了第一行。

提前致谢!

1 个答案:

答案 0 :(得分:0)

你有两个独立的案例 - 一个用于阅读,一个用于写作。如果在一种情况下,您为变量赋值,则并不意味着在其他情况下您可以读取它们。计数器也未正确设置。试试这段代码 -

while((line = buffer.readLine()) !=null) {
    counter++;
    if (counter == 3) {
        //String[]word = Ingredient.split(",");
        String[]word = line.split(",");

        int Quantity = Integer.parseInt(word[1]);
        //int ServingNumberInt = Integer.parseInt();
        int Multiplier = NewServingNumber / Quantity;
        int NewQuantity = (Multiplier * NewServingNumber);

        System.out.println("Your new quantity is " + NewQuantity);   
    }

}

它给出了 -

Hello. If you would like to write a new recipe, please type in 'write', if you would like to change and view a recipe, please type in 'read'
read
Would you like to change the serving number of your recipe, type in 'yes' to proceed, type in 'no'
yes
Please enter the new serving number
10
Your new quantity is 10
相关问题