java - Writing to OR Reading from a file -
i trying write program user has choice write data file or read file.
i don't know why if-else if structure not working correctly. when try view data, program displays data , asks more input when should not.
i post have hoping can seems foolish , simple problem.
import java.util.scanner; import java.io.*; public class candy { public static void main(string[] args) throws ioexception { string candyname; string input=""; //create scanner object scanner keyboard=new scanner(system.in); system.out.print("[v]iew or [a]ppend database (v or a)?===>"); string choice=keyboard.nextline(); if (choice.equalsignorecase("v")) { //open file file file=new file("candy"); scanner inputfile=new scanner(file); //display lines while (inputfile.hasnext()) { //read line candyname=inputfile.nextline(); //display line system.out.println(candyname); } //close file inputfile.close(); system.out.println("\ncomplete!"); } else if (choice.equalsignorecase("a")); { //open file filewriter fwriter=new filewriter("candy", true); system.out.println("type 'end' stop entering names\n"); { //get name of candy system.out.print("enter name of candy===>"); candyname=keyboard.nextline(); if (candyname.equalsignorecase("end")) { //break; //breaks out of loop //or use null if clause (empty) } else if (!(candyname.equalsignorecase("end"))) { //append file printwriter outputfile=new printwriter(fwriter); outputfile.println(candyname); } }while (!(candyname.equalsignorecase("end")));// || candyname !="end"); //close file fwriter.close(); system.out.println("\ncomplete!"); } } }
because else if closed
else if (choice.equalsignorecase("a"));
remove semicolon ;
else if (choice.equalsignorecase("a"))
Comments
Post a Comment