简体   繁体   中英

My bufferedread only reading the first line of my file?

The bufferedreader I have used in my code seems to read only the first line of the code. Can some one help me solve the problem, I've been trying for a long time.

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

My input was:

applep - for the recipe name

10 - for serving number

apple,10,apples - for the ingredient, I only added 1 ingredient.

When I read and read my file and change the recipe servinging number, it doesn't not work and gives in an 'read error'. In addition, to test the problem, I printed the variable 'line' and it only seems to read the first line.

Thanks in advance!

You have two independent cases - one for reading and one for writing. If in one case, you assign values ​​to variables, it does not mean that in other case you can read them. Also the counter is not set correctly. Try this code -

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

}

it gives -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM