0

I created a program for taking a criminal case and storing it, and then I added another switch to it so that I can access other stuff I wish to add in the program. But there seems to be an error when the choice is executed. Switch just won't recognize my choice, and instead repeats the menu that resides within the loop. There's no error during compilation. Here's the 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 a 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 a crime scene, Fleeing scene of crime, Carrying unlicensed vehicles and 

weapons.\nDate of Crime Committed:- 18/9/2015\nVictim:- None.");
                    for (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;
        }
     }
  }

}

  • 2
    *Please* take time to format your code when you post on Stack Overflow - I won't expect your code looks like that in your IDE (or whatever). Indent the code, remove the huge amounts of extra whitespace, etc. There's also rather more code than you really need to demonstrate the problem - if the problem is only in the choosing bit, do you need the rest at all? See http://stackoverflow.com/help/mcve – Jon Skeet Sep 26 '15 at 06:45
  • 2
    As always: `Scanner.nextInt()` does _not_ consume the newline character that must be entered on the console. So you must run an extra `s.nextLine()` after `s.nextInt()`. Because of way too much code, I cannot see whether there are mor problems in your code. – Seelenvirtuose Sep 26 '15 at 06:47
  • 1
    Thank you! And will take care of the formatting next time surely! – Hetanshu Bharadiya Sep 26 '15 at 06:51
  • 3
    @HetanshuBharadiya You can take care of it _this_ time. Just edit your question ... – Seelenvirtuose Sep 26 '15 at 06:52

2 Answers2

2

Without testing I guess the bug is in this 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 a case enter a\nto quit enter q");
        String input = s.nextLine();

You are reading integer with nextInt() method, and then, you should call nextLine() like follows

int choice=s.nextInt();
s.nextLine()
switch (choice)
{
....

Look at this question to understand this behavior:

Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

EDIT (my personal advice):

I am reading single integer in one line like follows:

int choice = Integer.parseInt(s.nextLine());

Try to implement this version

Community
  • 1
  • 1
maskacovnik
  • 3,009
  • 5
  • 18
  • 26
1

The boolean was not being reset to false, and hence loop was never accessed! Here's the final coding with the answer!

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=Integer.parseInt(s.nextLine());
switch (choice)
{
case 1:{

        while (!quit) {
            System.out.println();
            System.out.println("To view current cases enter v\nto add a 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 a crime scene, Fleeing scene of crime, Carrying unlicensed vehicles and 

weapons.\nDate of Crime Committed:- 18/9/2015\nVictim:- None.");
                    for (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;


                }

            }
        }
quit=false;
break;    
}
case 2:System.out.println("Thank you."); 
break;

case 3:exit=true;
}
}
}

}