Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Almost have my project done, just need help with one last thing.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Almost have my project done, just need help with one last thing.

    Ok, so my program works fine for the most part, except for a few minor issues that I absolutely have to fix. So, let me start by explaining a few things about the program. The program is suppose to get a number from the user, then when you select one of the options from the Menu(), it will call the Method pertaining to the option the user chooses. After that Method concludes, the program should bring the Menu() back up, so that the user may use the program again. This is where my problem occurs. The program will work fine the first time an option is selected, but when the Menu() is called again, whenever the user makes a selection, the program just ends. For examples of my Methods that call the Menu() back, see lines 139, 152, 165, 185, and 108.


    import java.util.Scanner; //for the ability to get input from 
    						  //the keyboard 
    import java.text.DecimalFormat;
     
    public class Project3
    {					  
     
    	public static void main(String[] args)
    	{
    		//enter variable below
    		double orgGrade = 0;   //Variable for the Original Grade
    		double newGrade = 0;	   //Variable for the Grade after the Curve
    		double gradePercent = 0;   //Variable for displaying the percentage from option 4.
    		double points = 10.0;  //Variable for points added
    		double percent = .10;   //Variable for given percentage
    		double gpoints = 0;          //Variable for the Given points
    		double gpercent = 0;         //Variable for the Given percentage
    		int a = 0;					 // Variable for Menu Selection
    		String Msg = "Press ENTER to return to the menu";
    		Scanner keyboard = new Scanner(System.in);
    		DecimalFormat df = new DecimalFormat("00.0");
    		//start program here
     
    		clearScreen();
     
    		orgGrade = orginalgrade(orgGrade);  //get users grade. 
     
    		clearScreen(); //clear the screen
     
     
    		a = menu(a, Msg);				//show menu/selections	
     
     
     
     
     
     
     
    			switch (a)
    			{	
    				case 1:
    				addPoints(a, newGrade, orgGrade, points, Msg);
    				break;
     
    				case 2:
    				addPercent(a, newGrade, orgGrade, percent, gradePercent, Msg);
    				break;
     
    				case 3:
    				givenPoints(a, newGrade, orgGrade, gpoints, Msg);
    				break;
     
    				case 4:
    				givenPercent(a, newGrade, orgGrade, gpercent, Msg);
    				break;
     
    				case 5:
     
    				break;
    			}
     
     
     
     
     
     
     
     
     
     
     
     
    	}//end main
    	public static void clearScreen()
    	{
    		System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    	}//end clearScreen
     
    	public static void freezeScreen()
    	{
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.print("	       -- Press Enter to Continue --");
    		keyboard.nextLine();
    	}//end freezeScreen
     
    	public static void freezeScreen(String Msg)
    	{
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.print(Msg);
    		keyboard.nextLine();
     
    	}//end freezeScreen
     
    	public static double orginalgrade(double orgGrade)
    	{
    		Scanner keyboard = new Scanner(System.in);
     
     
     
    		System.out.print("What is the original grade? ");     //Get original grade from user
    		orgGrade = keyboard.nextDouble();
     
    		return orgGrade; 
    	}//end orgGrade
     
    	public static int menu(int a, String Msg) 
    	{
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println("	    Grade Adjuster");
    		System.out.println("	   by Aaron Webster ");   // Greet user
    		System.out.println("******************************\n");
    		System.out.println("1. Curve by 10 points.");              //menu options here
    		System.out.println("2. Curve by 10 percent.");
    		System.out.println("3. Curve by a certain number of points.");
    		System.out.println("4. Curve by a given percentage"); 
    		System.out.println("5. Exit program\n");
    		System.out.print("Enter your selection here: ");
    		a = keyboard.nextInt();
     
    		if (a > 5 || a < 1)
    		{
    			System.out.print("That is not a valid option!\n\n");
    			freezeScreen(Msg);
    			clearScreen();
    			menu(a, Msg);
     
    			return a;
    		}
     
     
     
    		return a;
    	}//end Menu
     
    	public static double addPoints(int a, double newGrade, double orgGrade, double points, String Msg)
    	{
     
    				newGrade = orgGrade + points;
    				System.out.println("Curve applied: " + points);
    				System.out.println("Adjusted grade: " + newGrade);
    				freezeScreen(Msg);
    				clearScreen();
    				menu(a, Msg);
     
    		return newGrade;
    	}//end addPoints	
     
    	public static double addPercent(int a, double newGrade, double orgGrade, double percent, double gradePercent, String Msg)
    	{
    				newGrade = (orgGrade * percent) + orgGrade; 
    				gradePercent = orgGrade * percent;
    				System.out.println("Curve applied: " + gradePercent);
    				System.out.println("Adjusted grade: " + newGrade);
    				freezeScreen(Msg);
    				clearScreen();
    				menu(a, Msg);
     
    		return newGrade;
    	}//end addPercent
     
    	public static double givenPoints(int a, double newGrade, double orgGrade, double gpoints, String Msg)
    	{
    				Scanner keyboard = new Scanner(System.in);
     
    				System.out.println("How many points should be applied to curve the grade?");
    				gpoints = keyboard.nextDouble();
    				newGrade = gpoints + orgGrade;
    				if (newGrade > 100)
    				{
    					newGrade = 100;
    				}
    				System.out.println("Curve applied: " + gpoints);
    				System.out.println("Adjusted grade: " + newGrade);
    				freezeScreen(Msg);
    				clearScreen();
    				menu(a, Msg);
     
    		return newGrade;
    	}//end givenPoints
     
    	public static double givenPercent(int a, double newGrade, double orgGrade, double gpercent, String Msg)
    	{
    				Scanner keyboard = new Scanner(System.in);
     
    				System.out.println("Enter the percentage of the curve. (Ex. 10% would be .10)");
    				gpercent = keyboard.nextDouble();
    				newGrade = (gpercent * orgGrade) + orgGrade;
    				if (newGrade > 100)
    				{
    					newGrade = 100;
    				}
    				System.out.println("Curve applied: " + gpercent);
    				System.out.println("Adjusted grade: " + newGrade);
    				freezeScreen(Msg);
    				clearScreen();
    				menu(a, Msg);
     
    		return newGrade;
    	}//end givenPercent
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Almost have my project done, just need help with one last thing.

    when the Menu() is called again, whenever the user makes a selection, the program just ends
    Can you explain what "just ends" means? Does the program have an error that stops its execution?
    What makes the program stop executing?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Almost have my project done, just need help with one last thing.

    Maybe try using a do-while loop for the switch statement and update the value for (a) every time the user input a selection.

    sorry couldn't understand your program but try what I said, might work.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Almost have my project done, just need help with one last thing.

    No error, it just ends. Like, the Menu() is called again, you enter a selection but then it just ends. It never goes back to the switch statement.

    --- Update ---

    What is a do-while loop? and how do I update 'a' value? I tried updating it last night, and I couldn't figure it out

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Almost have my project done, just need help with one last thing.

    What is a do-while loop
    See the tutorial:
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    how do I update 'a' value?
    a = newValue;  //  update a
    //  or
    a = someMethod(); //  update a using value returned by method

    It never goes back to the switch statement.
    There is no loop for the execution to go back with. When the switch statement exits, the execution exits the main() method and the program will stop.

    You need to wrap the code you want repeated inside a loop like a while or do-while.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Almost have my project done, just need help with one last thing.

    Where would that need to go into my code? In every one of the methods I wrote? Because 'a' gets is the variable that holds the selection from the Menu(), so basically 'a' starts at 0, then when the Menu() is called, it is assigned either 1-4, the program calls whichever method is assigned to that number, and then the Menu() is called again, except when the Menu() is called for the second time, even if I hit a different selection, 'a' doesn't change it's value.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Almost have my project done, just need help with one last thing.

    Put the loop in the main() method:
    begin loop
    show menu
    get users response
    do what user wanted to do (this could include quit which would exit the loop)
    end loop

    These three lines are in most of the methods:
    				freezeScreen(Msg);
    				clearScreen();
    				menu(a, Msg);
    They should be removed from the methods and put in the main loop.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    ViewtifulAaron (March 31st, 2013)

  9. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Almost have my project done, just need help with one last thing.

    Ok, thanks a lot man, I'll try that out as soon as I can and post results

    --- Update ---

    ok, so I added a loop to my Main(), and removed the other things from the Methods. So, I have it now to where the program loops the menu(), but it doesn't execute anything in the switch statement. Should I add the switch statement to the loop?

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Almost have my project done, just need help with one last thing.

    doesn't execute anything in the switch statement.
    Post the code to show the problem.

    Be sure there is a 'default:' in the switch statement that prints out an error message and the value of the switch variable.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. need to figure how this thing will work
    By frosst in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 26th, 2011, 01:45 AM
  2. [SOLVED] Method help, one thing then another - puzzle
    By Scotty in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 01:52 PM
  3. Weird thing with JFrame
    By Brt93yoda in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 05:00 PM
  4. Trying 3 different ways to do the same thing, each one is wrong
    By shemer77 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 4th, 2010, 07:01 PM