bufferedreader - An error with reading a file - java -


i can't seem change quantities of recipe. please help. i'm beginner java. after enter new serving number, prints 'a read error has occur'?

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 recipe; private static int newservingnumber;  public static void main(string []args){      scanner userinput = new scanner(system.in);     system.out.println("hello. if write new recipe, please type in 'write', if change recipe, please type in 'read'.");     string option = userinput.next();     userinput.nextline();      if(option.equals("write")){          write();     }      if(option.equals("read")){          read();     }  }  public static void write(){      try{          filewriter task2recipe = new filewriter("c:/year 11/gcse computing/a453/task 2/recipe.txt");         bufferedwriter recipe = new bufferedwriter(task2recipe);         scanner userinput2 = new scanner(system.in);          system.out.println("please enter name of recipe, number of people servers, name of first ingredient, quantity , units separated comma");         string recipe = userinput2.next();         recipe.write(recipe);         recipe.newline();          recipe.close();     }catch(exception e){          system.out.println("a write error has occured");     }  }  public static void read(){      try{          scanner userinput3 = new scanner(system.in);         filereader file = new filereader("c:/year 11/gcse computing/a453/task 2/recipe.txt");         bufferedreader buffer = new bufferedreader(file);          system.out.println("would change serving number of recipe? please type in 'yes' proceed or 'no' reject");         string choice3 = userinput3.next();         userinput3.nextline();          string line;          if(choice3.equals("yes")){              system.out.println("please enter new serving number");             int newservingnumber = userinput3.nextint();           }          while ((line = buffer.readline()) !=null){              string splitter = ",";             string[]word = recipe.split(splitter);             int quantity = integer.parseint(word[3]);             int servingnumberint = integer.parseint(word[1]);             int multiplier = servingnumberint / quantity;             int newquantity = (multiplier * newservingnumber);             system.out.println("your new quantity " + newquantity);          }          buffer.close();      }catch(exception e){          system.out.println("a read error has occured");     } } } 

the issue string recipe never assigned value yet code attempts split string recipe null causing nullpointerexception

if reading recipe file, think may need split line seems getting assigned each line of recipe:

    while ((line = buffer.readline()) != null) {              //string[] word = recipe.split(splitter); casues npe             string[] word = line.split(",");             int quantity = integer.parseint(word[3]);             int servingnumberint = integer.parseint(word[1]);             int multiplier = servingnumberint / quantity;             int newquantity = (multiplier * newservingnumber);             system.out.println("your new quantity " + newquantity);       } 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -