java - NumberFormatExceptions being thrown everywhere. Why? (parseInt) -
first time poster here annoying issue. have decided create text-based rpg in java in order consolidate have been learning. , lengthy games, saving/loading system necessary. problems arise.
i have created menu system, works fine. however, when decide start new game, numberformatexceptions thrown left, right , center. source code down below, @ appreciated. have separated each important section separate java files.
//imports import java.io.ioexception; import java.io.file; import java.io.*; import java.util.scanner; import javax.sound.sampled.audioinputstream; import javax.sound.sampled.audiosystem; import javax.sound.sampled.clip; //'choices' - yemi oladimeji/yemzeee class choices{ public static void main(string [] args) throws ioexception{ scanner input = new scanner(system.in); string startchoice = null; //introduction begin gamemethods.space(); system.out.println("######## ## ## ######## ######## ######## ######## ########"); system.out.println("## ## ## ## ## ## ## ## ## "); system.out.println("## ## ## ## ## ## ## ## ## "); system.out.println("## ######## ## ## ## ## ###### ###### "); system.out.println("## ## ## ## ## ## ## ## ##"); system.out.println("## ## ## ## ## ## ## ## ##"); system.out.println("######## ## ## ######## ######## ######## ######## ########"); gamemethods.space(); system.out.println("greetings, , welcome choices, 2d text-based rpg!"); system.out.println("what do?"); system.out.println("<p>lay :: <a>bout :: <q>uit"); startchoice = input.nextline(); gamemethods.schoice(startchoice); } }//end choices.java this beginning of gamemethods.java file
import java.util.scanner; import java.io.*; import java.io.ioexception; public class gamemethods extends choicesavemethods{ static scanner input = new scanner(system.in); static string startchoice; static string loadchoice; public static void buffer(){ try{ system.in.read(); } catch(exception e){ } } public static void space(){ system.out.println(" "); } public static void quit(){ system.exit(1); } public static void menucall(){ system.out.println("<p>lay :: <a>bout :: <q>uit"); startchoice = input.nextline(); gamemethods.schoice(startchoice); } public static void schoice(string startchoice){ if(startchoice.equals("a")){ gamemethods.space(); system.out.println("this text-based rpg, created yemi oladimeji."); system.out.println("it focuses on story , said story changes depend on \nthe choices make in world."); system.out.println("as quite story driven, there option @ \nthe end of journey output story text file save , keep."); system.out.println("now know game about, want go menu? \n(y/n)"); string achoice = input.nextline(); if(achoice.equals("y")){ gamemethods.menucall(); } else if(achoice.equals("n")){ system.out.println("or quit game?"); string qchoice = input.nextline(); if(qchoice.equals("y")){ system.exit(1); } else if(qchoice.equals("n")){ gamemethods.menucall(); } } } if(startchoice.equals("q")){ gamemethods.quit(); } if(startchoice.equals("p")){ try{ bufferedreader savefile = new bufferedreader(new filereader("c:\\users\\yemi\\desktop\\thegame\\savefiles\\textsave1.txt")); areamarkers[0] = integer.parseint(savefile.readline()); areamarkers[1] = integer.parseint(savefile.readline()); areamarkers[2] = integer.parseint(savefile.readline()); areamarkers[3] = integer.parseint(savefile.readline()); savefile.close(); for(int x = 0; x < 4; x++){ if(areamarkers[x] == 1){ system.out.println("a save game has been located. load it?(y/n)"); loadchoice = input.nextline(); if(loadchoice.equals("y")){ system.out.println("the save/load feature works."); } else if(loadchoice.equals("n")){ system.out.println("starting new game..."); gamemethods.gamestart(); } } } } catch(ioexception ex){ ex.printstacktrace(); } forestarea(); } } public static void gamestart(){ //introduction begin gamemethods.space(); system.out.println("welcome 'choices'. influence world decisions. react \nhow wish..."); gamemethods.space(); system.out.println("this game built immerse in hooking storyline no graphics, \nand imagination can that. \nthat , audio. purpose, recommend use headphones \nwhilst playing game."); system.out.println("i require 2 pieces of information. name. it?\n(this called in game)"); system.out.println("your name: "); string name1 = input.nextline(); system.out.println("and name user account\n(so program can locate required files): "); string username = input.nextline(); system.out.println("there 2 rules..."); system.out.println("this (*) means flashback occurring."); system.out.println("and when there pause, means \nshould press enter/return continue."); system.out.println("you that? good."); system.out.println("now. let begin game , remember. \nyour choices affect path."); //introduction end } } and last java file, 1 contains areas , saving method:
import java.io.*; import java.util.scanner; import java.io.ioexception; public class choicesavemethods{ //area variables: static int[] areamarkers = new int[4]; static int[] areachoices = new int[4]; static scanner input = new scanner(system.in); public static void forestarea(){ gamemethods.space(); system.out.println("area 1: forest. here?: "); system.out.println("1 - save game?"); system.out.println("2 - view description of area?"); system.out.println("3 - progress next area?"); system.out.println("4 - exit main menu?"); areachoices[0] = input.nextint(); if(areachoices[0] == 1){ try{ filewriter savefile1 = new filewriter("c:\\users\\yemi\\desktop\\thegame\\savefiles\\textsave1.txt"); areamarkers[0] = 1; savefile1.write(1); system.out.println("game has been saved successfully."); savefile1.close(); forestarea(); } catch (ioexception ex){ ex.printstacktrace(); } } else if(areachoices[0] == 2){ system.out.println("this forest. has trees."); forestarea(); } else if(areachoices[0] == 3){ system.out.println("progressing next area..."); cityarea(); } else if(areachoices[0] == 4){ system.out.println("returning main menu."); gamemethods.menucall(); } } public static void cityarea(){ gamemethods.space(); system.out.println("area 2: city. here?: "); system.out.println("1 - save game?"); system.out.println("2 - view description of area?"); system.out.println("3 - progress next area?"); system.out.println("4 - exit main menu?"); areachoices[1] = input.nextint(); if(areachoices[1] == 1){ try{ filewriter savefile2 = new filewriter("c:\\users\\yemi\\desktop\\thegame\\savefiles\\textsave1.txt"); areamarkers[1] = 1; savefile2.write(areamarkers[1]); system.out.println("game has been saved successfully."); savefile2.close(); cityarea(); } catch(ioexception ex){ ex.printstacktrace(); } } else if(areachoices[1] == 2){ system.out.println("this city. there alot of people."); cityarea(); } else if(areachoices[1] == 3){ system.out.println("progressing next area..."); magmaarea(); } else if(areachoices[1] == 4){ system.out.println("returning main menu."); gamemethods.menucall(); } } public static void magmaarea(){ gamemethods.space(); system.out.println("area 3: magma pool. here?: "); system.out.println("1 - save game?"); system.out.println("2 - view description of area?"); system.out.println("3 - progress next area?"); system.out.println("4 - exit main menu?"); areachoices[2] = input.nextint(); if(areachoices[2] == 1){ try{ filewriter savefile3 = new filewriter("c:\\users\\yemi\\desktop\\thegame\\savefiles\\textsave1.txt"); areamarkers[2] = 1; savefile3.write(areamarkers[2]); system.out.println("game has been saved successfully."); savefile3.close(); magmaarea(); } catch(ioexception ex){ ex.printstacktrace(); } } else if(areachoices[2] == 2){ system.out.println("a blazing pool of molten rock. hot."); magmaarea(); } else if(areachoices[2] == 3){ system.out.println("progressing next area."); hospitalarea(); } else if(areachoices[2] == 4){ system.out.println("returning main menu."); gamemethods.menucall(); } } public static void hospitalarea(){ gamemethods.space(); system.out.println("area 4: hospital. here?: "); system.out.println("1 - save game?"); system.out.println("2 - view description of area?"); system.out.println("3 - exit main menu."); areachoices[3] = input.nextint(); if(areachoices[3] == 1){ try { filewriter savefile4 = new filewriter("c:\\users\\yemi\\desktop\\thegame\\savefiles\\textsave1.txt"); areamarkers[3] = 1; savefile4.write(areamarkers[3]); system.out.println("game has been saved."); savefile4.close(); hospitalarea(); } catch(ioexception ex){ ex.printstacktrace(); } } else if(areachoices[3] == 2){ system.out.println("a disused, abandoned hospital. has eerie feel it."); hospitalarea(); } else if(areachoices[3] == 3){ system.out.println("returning main menu..."); gamemethods.menucall(); } } } and exception being thrown this:
exception in thread "main" java.lang.numberformatexception: input string: "" @ java.lang.numberformatexception.forinputstring(numberformatexception.java:65) @ java.lang.integer.parseint(integer.java:504) @ java.lang.integer.parseint(integer.java:527) @ gamemethods.schoice(gamemethods.java:59) @ choices.main(choices.java:32) i realize lot asking for, first time poster, running out of ideas here. feel free copy , run code. answers @ appreciated. again, thank you.
agree jahroy; stack trace telling what's wrong. looking @ code you're trying read 4 integers on 4 lines save file, save logic in *area() methods ever write 1 integer. maybe intended write out integer , 3 zeros instead?
probably better use single integer indicate location in save file (0=forest, 1=city, 2=magma, etc.) rather 4 0/1 ints on separate lines.
Comments
Post a Comment