0

Im trying to run this Cube.java class from Manager.java after user types "cube". After they tape "cube" it should then run Cube.java. That works perfectly fine but then I want to go back to Manager.java after user types "exit" in Cube.java. The code does exit to Manager.java and runs the code up til the list of calculators then it gives me this error

Exception in thread "main" java.util.NoSuchElementException   
at java.base/java.util.Scanner.throwFor(Scanner.java:941)   
at java.base/java.util.Scanner.next(Scanner.java:1482)   
at Manager.main(Manager.java:33)   
at Cube.main(Cube.java:52)   

I'm not sure why it's not working.

This is my Cube.java code

import java.util.Scanner;
import java.lang.Math;
import java.lang.String;

public class Cube
{
    public static void main(String[] args)
    {
        //init scanner
        Scanner reader = new Scanner(System.in);

        //var
        String strSideLen; //usr input for len of side of cubde
        String units; //usr input for units
        double dblSideLen; //clctd for len of side of cube
        double surfArea; //clctd & output for surface area
        double volume; //clctd & output for volume
        double diaLen; //clctd & output for diagonal of of cube
        boolean repeat; //
        
        //dscrp of prgrm to usr
        System.out.println();
        System.out.println("Calculate your cube's surface area, volume, and the     length of it's diagonal.");
        System.out.println("Entering \"exit\" at any type will bring you back to the manager");
        System.out.println();

        //setting repeat to true
        repeat = true;

        while (repeat == true)
        {
            //rqst input
            System.out.print("Enter side length of your cube: ");
            strSideLen = reader.nextLine();
            System.out.println();

            if (strSideLen.equalsIgnoreCase("exit"))
            {
                //close reader
                reader.close();

                repeat = false;

                //bring to manager
                System.out.println("Program terminated, going to manager\n");
                Manager.main(args);
            }
            else if (isNumeric(strSideLen))
            {
                dblSideLen = Double.parseDouble (strSideLen); //str -> dbl

                //rqst unit
                System.out.print("Enter units: ");
                units = reader.next();
                System.out.println();
                
                if(units.equalsIgnoreCase("exit"))
                {
                    //close reader
                    reader.close();

                    repeat = false;

                    //bring to manager
                    System.out.println("Program terminated, going to manager\n");
                    Manager.main(args);
                }
                else 
                {
                    //cmpte outputs
                surfArea = 6 * Math.pow(dblSideLen, 2.0); //surfArea calc
                volume = Math.pow(dblSideLen, 3.0); //volume calc
                diaLen = dblSideLen * Math.sqrt(3.0); //diaLen calc

                //prnt out
                System.out.println("The surface area of your cube: " + surfArea + " " + units + " squared"); //prnt surfArea
                System.out.println("The volume of your cube: " + volume + " " + units + " cubed"); //prnt volume'
                System.out.println("The main diagonal (vertex to opposite vertex) of your cube is: " + diaLen + " " + units); //print diaLen
                System.out.println();
                }
            }
            else
            {
                System.out.println("please enter an actual number.");
            }
        }
    }

    //FUNCITON TO CHECK STR NUMERIC BEFORE PARSING
    public static boolean isNumeric(String str)
    {
        try
        {
            Double.parseDouble(str); //parses string if string value is numerical
            return true;
        } 
        catch(NumberFormatException ex) //returns false and does not parse if string value is not numerical
        {
            return false;
        }
    }    
}

This is my Manager.java code

import java.util.Scanner;

public class Manager
{
    public static void main (String[] args)
    {
        //init scanner
        Scanner readerAll = new Scanner(System.in);

        //var
        String calcIn; //input for which calc to run
        System.out.println();
        System.out.println("MANAGER");

        //split list
        String strMain = ">>> cube, >>> sphere"; //options for calc

        //split options
        String[] arrSplit = strMain.split(", ");
        for (int i=0; i < arrSplit.length; i++)
        {
            System.out.println(arrSplit[i]);
        }
        System.out.println();

        //ask for input
        System.out.print("enter your option: ");
        calcIn = readerAll.next();

        System.out.println();

        if (calcIn.equalsIgnoreCase("cube"))
        {
            Cube.main(args);
        }
        else if (calcIn.equalsIgnoreCase("sphere"))
        {
            Sphere.main(args);
        }

        //close reader
        readerAll.close();
    }
}
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – QBrute Sep 14 '22 at 17:43

0 Answers0