i created program taking criminal case , storing it, , added switch can access other stuff wish add in program. there seems error when choice executed. switch won't recognize choice, , instead repeats menu resides within loop. there's no error during compilation. here's coding...
import java.util.arraylist; import java.util.scanner; public class criminalcase { private string batput; public string getbatput(){return batput;} public criminalcase(string batput){ this.batput = batput; } private static class robin{ string batman(){ scanner s=new scanner (system.in); system.out.println(); system.out.println("enter name."); string a=s.nextline(); system.out.println("enter date of birth."); string b=s.nextline(); system.out.println("enter sex."); string c=s.nextline(); system.out.println("enter crime committed."); string d=s.nextline(); system.out.println("enter date of crime committed."); string e=s.nextline(); system.out.println("enter victim."); string f=s.nextline(); system.out.println(); string g=""+"\n"+""+"name:- "+a +"\ndob:- "+b +"\nsex:- "+c +"\ncrime committed:- "+d +"\ndate of crime committed:- "+e +"\nvictim:- "+f; system.out.println(); return g; } } public static void main(string[] args) { arraylist<criminalcase> cases = new arraylist<>(); boolean quit = false; scanner s = new scanner(system.in); robin j=new robin(); boolean exit=false; for(;!exit;){ system.out.println("for cases press 1.\nfor printing thank you, press 2.\nto exit, press 3."); int choice=s.nextint(); switch (choice){ case 1:{ while (!quit) { system.out.println(); system.out.println("to view current cases enter v\nto add case enter a\nto quit enter q"); string input = s.nextline(); switch(input){ case ("v"): { system.out.println(""); system.out.println("the following cases exist:"); system.out.println("\nname:- batman\ndob:- unknown\nsex:- male\ncrime committed:- tresspassing crime scene, fleeing scene of crime, carrying unlicensed vehicles , weapons.\ndate of crime committed:- 18/9/2015\nvictim:- none."); (criminalcase c : cases) system.out.println(c.getbatput()); break; } case("a"):{ string batput=j.batman(); cases.add(new criminalcase(batput)); break; } case("q"):{ quit = true; } } } break; } case 2:system.out.println("thank you."); break; case 3:exit=true; } } }
}
without testing guess bug in piece of code:
int choice=s.nextint(); switch (choice) { case 1:{ while (!quit) { system.out.println(); system.out.println("to view current cases enter v\nto add case enter a\nto quit enter q"); string input = s.nextline();
you reading integer nextint()
method, , then, should call nextline()
follows
int choice=s.nextint(); s.nextline() switch (choice) { ....
look @ question understand behavior:
scanner skipping nextline() after using next(), nextint() or other nextfoo() methods
edit (my personal advice):
i reading single integer in 1 line follows:
int choice = integer.parseint(s.nextline());
try implement version
Comments
Post a Comment