Results 1 to 3 of 3
  1. #1
    -F7-ForLife's Avatar
    -F7-ForLife is offline Never Surrender
    Join Date
    May 2015
    Posts
    8
    Rep Power
    0

    Question Issues with Program Looping

    I'm writing a program that has three different calculators and an exit method. There is a main menu that lets you choose which calculator to use. When finished using a particular calculator, the program should loops bac to the main menu, allowing you to pick another option or close at your choosing.

    The issue I'm running into is when a certain method runs its course (except for exit, because it terminates the program entirely), instead of it looping back and restarting the main method at the options menu, it conflicts with Scanner and the re-calling of the method, then crashes.

    I'm getting nullPointerExceptions, leading me to believe that the old selection inputs are still stored in the Scanner that I have declared. I've tried to create multiple Scanners, but am still running into crashes. Suspensions keep happening at Scanner declaration and the findWithinHorizon statement when the sub-method ends and attempts to restart the main method.

    Here is the script from my main method (ERRORS at lines #54 and #65)

    Java Code:
    package practice_projects;
    import java.util.*;
    public class CalculatorMain 
    {
    	public static void main(String args[])
    	{
    		Scanner keyboard = new Scanner(System.in);
    		char selection;
    		boolean pcisselected, addisselected, subisselected, endisselected;
    	
    		System.out.println("Welcome to the calculator utility, v1.0!\nPlease select an option below:");
    		System.out.println("-----------------------------------");
    		System.out.println("- 1.    Paycheck calculator       -");
    		System.out.println("- 2.    Additron 3000             -");
    		System.out.println("- 3.    Subtractifier 10x         -");
    		System.out.println("- 4.    Exit                      -");
    		System.out.println("-----------------------------------");
    		System.out.print  ("Your selection:: ");
    
    		selection = keyboard.findWithinHorizon(".", 0).charAt(0);
    		pcisselected  = selection == '1'; 
    		addisselected = selection == '2';
    		subisselected = selection == '3';
    		endisselected = selection == '4';
    	
    		//Runs the paycheck calculator		
    		if (pcisselected == true)
    		{
    			paycheckcalculator paycheckObj = new paycheckcalculator();
    			paycheckObj.payCheckCalculator(); reloop();
    		}
    		//Runs the additron 3000
    		else if (addisselected == true)
    		{
    			addcalculator addObj = new addcalculator();
    			addObj.additron3000(); reloop();
    		}
    		//Runs the subtractifier 10x	
    		else if (subisselected == true)
    		{
    			subtractcalculator subObj = new subtractcalculator();
    			subObj.subtractifier10x(); reloop();
    		}
    		//Runs the end command	
    		else if (endisselected == true)
    		{
    			System.out.println("Thank you for using the calculator utility! Goodbye.");
    			keyboard.close();
    			System.exit(0);	
    		}
    	}
    		static void reloop()
    		{
    		Scanner keyboard = new Scanner(System.in);
    		char selection;
    		boolean pcisselected, addisselected, subisselected, endisselected;
    		System.out.println("Welcome back to the calculator utility!\nPlease select an option below:");
    		System.out.println("-----------------------------------");
    		System.out.println("- 1.    Paycheck calculator       -");
    		System.out.println("- 2.    Additron 3000             -");
    		System.out.println("- 3.    Subtractifier 10x         -");
    		System.out.println("- 4.    Exit                      -");
    		System.out.println("-----------------------------------");
    		System.out.print("Your selection: ");
    		selection = keyboard.findWithinHorizon(".", 0).charAt(0);
    		pcisselected  = selection == '1'; 
    		addisselected = selection == '2';
    		subisselected = selection == '3';
    		endisselected = selection == '4';
    		
    
    		
    		//Runs the paycheck calculator		
    		if (pcisselected == true)
    		{
    		paycheckcalculator paycheckObj = new paycheckcalculator();
    		paycheckObj.payCheckCalculator();
    		}
    		//Runs the additron 3000
    		else if (addisselected == true)
    		{
    		addcalculator addObj = new addcalculator();
    		addObj.additron3000();
    		}
    		//Runs the subtractifier 10x	
    		else if (subisselected == true)
    		{
    		subtractcalculator subObj = new subtractcalculator();
    		subObj.subtractifier10x();
    		}
    		//Runs the end command	
    		else if (endisselected == true)
    		{
    			System.out.println("Thank you for using the calculator utility. Goodbye.");
    			keyboard.close();
    			System.exit(0);
    		}
    		}
    }
    Is there a more efficient way to do this than how I am currently? I'm trying to keep the program alive, rather than running down the script and ending- much the way an MMORPG never ends once the content has been finished. How else can I keep the program routing back to the main menu instead of terminating entirely? Am I on the right track here?

    Thank you so much for the help!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Issues with Program Looping

    conflicts with Scanner and the re-calling of the method, then crashes.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    the program should loops bac to the main menu
    Where is the loop? I don't see any.

    Note: The formatting of the code needs work. The method definition header lines are hidden by being at the same indentations as the other statements.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    -F7-ForLife's Avatar
    -F7-ForLife is offline Never Surrender
    Join Date
    May 2015
    Posts
    8
    Rep Power
    0

    Default Re: Issues with Program Looping

    This post can be disregarded, as I have discovered the issue.

    The loop is essentially a duplicate method that I've placed in a static void construct below the primary method of each object (Lines 52 and onward). One of the issues that I was having was using the main method for object creation. This was not working out, as I am not able to recall the main method once it's running. I scrapped this program and rescripted it.

    I ran into more issues with the Scanner, but found that the Scanner not being closed in certain segments of the program was not allowing objects to be properly called without suspending the entire program. After carefully positioning Scanner closes, the program now loops around until it is commanded to exit at the main menu.

Similar Threads

  1. program keeps looping
    By allan in forum New To Java
    Replies: 2
    Last Post: 01-13-2014, 12:22 AM
  2. Issues looping and breaking in Pigs Dice Game
    By clown2921 in forum New To Java
    Replies: 4
    Last Post: 01-04-2014, 11:43 AM
  3. ChessMate GUI Program Issues.
    By Paul_Boland in forum New To Java
    Replies: 1
    Last Post: 01-19-2013, 06:12 AM
  4. help with program!! looping issue?
    By zombian in forum New To Java
    Replies: 6
    Last Post: 11-21-2010, 10:14 PM
  5. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •