0

I am requesting input from a user from a menu using the Scanner class in another class. For some unknown reason the program always hangs when the in.nextInt() line is included. I have manually commented out every line to determine that the problem is in the in.nextInt() line but have no idea why that would be an issue.

Scanner in = new Scanner(System.in);
int choice = in.nextInt();

I have used a different scanner object to read text from a file earlier in my class but don't think that would be an issue. Any help would be greatly appreciated!

Edit: additional code

public void Manage() throws IOException, ListException
        {
        Scanner in = new Scanner(System.in);
        //Console console = System.console();
        System.out.println("1. Display items");
        System.out.println("2. Add item");
        System.out.println("3. Insert item");
        System.out.println("4. Delete item");
        System.out.println("5. Exit");

        System.out.println("Enter option:");
        int choice = in.nextInt();
        //int choice = Integer.parseInt(System.console().readLine());
        //choice = in.nextInt();

        switch (choice)
        {
        //display
        case 1:
            System.out.println(dList);
            break;
        //add
        case 2:
            System.out.println("Please enter the address:");
            String a = in.nextLine();
            System.out.println("Please enter the square footage:");
            double f = in.nextDouble();
            System.out.println("Please enter the value:");
            double v = in.nextDouble();
            try
            {
            Dwelling x = new Dwelling(a,f,v);
            dList.add(x);
            }
            catch (ListException e)
            {
                throw new ListException("damn");
            }
            break;
        //insert
        case 3:
            System.out.println("Please enter the address:");
            String ad = in.nextLine();
            System.out.println("Please enter the square footage:");
            double fo = in.nextDouble();
            System.out.println("Please enter the value:");
            double va = in.nextDouble();
            int p = in.nextInt();
            try
            {
            Dwelling y = new Dwelling(ad,fo,va);
            dList.insert(y,p);
            }
            catch (ListException e)
            {
                throw new ListException("fuck");
            }
            System.out.println("Please enter the position to insert:");


            break;
        //delete
        case 4:
            System.out.println("Please enter the position to delete:");
            int d = in.nextInt();
            try
            {
            dList.delete(d);
            }
            catch (ListException e)
            {
                throw new ListException("omg");
            }
            break;
        //exit
        case 5:
            break;

        }


\\update data file

}//end of manage method

Edit: Here is a copy of my main method that I am using to test my class

    public static void main(String [] args)
        {

        RealEstate2 r = new RealEstate2("SampleDwellings.txt");
        r.Manage();
        }//end of main
sbling
  • 1
  • 1
  • What's the input? any stacktrace? – Erwin May 24 '20 at 12:58
  • Without more code and more context, it will be hard to guess what could be wrong. Please check the [ask] to see what questions need in order to be easier to answer, and also consider creating and posting a [mre] with your question, code we can run and test. – DontKnowMuchBut Getting Better May 24 '20 at 13:01
  • when the next.int() line is included my menu does not even show up and any possible input i make interrupts and exits the program where my menu finally appears along with a "Interrupted" return – sbling May 24 '20 at 13:03
  • Can't you use the same scanner object to read the input , rather than creating again ? If you want to use another , you can close the first one using sc.close(); – Harmandeep Singh Kalsi May 24 '20 at 13:05
  • originally I did use the same scanner after closing it after the first use but ended up trying a different scanner when the issue involving the nextInt() remained – sbling May 24 '20 at 13:10
  • Can you show me your main method? Since I dont see any issue with this code. – Harmandeep Singh Kalsi May 24 '20 at 13:13

0 Answers0