Results 1 to 7 of 7
  1. #1
    Mideoan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default I need information to loop, just not sure how to get it to do so. . .

    Hi everyone. This is actually my first post. I'm new to Java and relatively new to programming all together. I'm taking an intro to Java course and also taking an intro to Visual Basic in Visual Studio 2008. Here is what I'm trying to do. The assignment calls for separate void Methods.

    I need to have the user input the amount in meters.
    After the input of meters is done, they then select the option of how it is converted, options 1-4.
    What I'm having trouble with is getting the options to loop until the user decides to choose option 4, which will then end the program. Here is my code I have so far, I have bolded where I'm trying to work it out, with no success so far.

    import java.util.Scanner;


    public class Conversion_Program {

    // Write a program that asks the user to enter a distance in meters. The //program will then present the following menu slections:
    // 1. Convert to kilometers
    // 2. Convert to inches
    // 3. Convert to feet
    // 4. Quit the program

    /**
    * @param args
    */
    public static void main(String[] args) {
    {

    double myKilometers = 0;
    double myInches = 0;
    double myFeet = 0;
    int number;
    String input;


    Scanner keyboard = new Scanner(System.in);


    System.out.println("Enter a distance in meters:" );
    input = keyboard.nextLine();
    System.out.println("1. Convert to kilometers");
    System.out.println("2. Convert to inches");
    System.out.println("3. Convet to fett");
    System.out.println("4. Quit the program");
    number = keyboard.nextInt();


    // If statement to determine number


    if (number == 1)
    myKilometers = showKilometers(Double.parseDouble(input));
    System.out.println(+ myKilometers);
    if (number == 2)
    myInches = showInches(Double.parseDouble(input));
    System.out.println(+ myInches);
    if (number == 3)
    myFeet = showFeet(Double.parseDouble(input));
    System.out.println(+ myFeet);
    if (number <= 3)
    System.out.println("Enter a distance in meters:" );
    input = keyboard.nextLine();
    System.out.println("1. Convert to kilometers");
    System.out.println("2. Convert to inches");
    System.out.println("3. Convet to fett");
    System.out.println("4. Quit the program");
    number = keyboard.nextInt();

    if (number == 4)
    System.exit(0);


    }
    }


    public static double showKilometers(double meters)
    {
    double kilometers = meters * 0.001;
    return kilometers;


    }

    public static double showInches(double meters)
    {
    double inches = meters * 39.37;
    return inches;
    }

    public static double showFeet(double meters)
    {
    double feet = meters * 3.281;
    return feet;
    }

    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Mideoan View Post
    What I'm having trouble with is getting the options to loop until the user decides to choose option 4
    You said the key word. You need to have the options printed inside a loop. What you can do is move the printing of the menu off to another method and have a while loop.
    Java Code:
    while user input doesn't equal 4 {
        print menu
        get user input
        do action
    }

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Mideoan View Post
    The assignment calls for separate void Methods.
    Yet all your method return a double.

  4. #4
    Mideoan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Sorry, I mistype / misspoke on the void method. It just calls for a Value-Returning Method. Still learning here :)

    So I incorporated the while method, perhaps I did it wrong, because it is continuously looping in the console box after the first option is entered by the user. Here is what I have now:

    while (number != 4)

    System.out.println("1. Convert to kilometers");
    System.out.println("2. Convert to inches");
    System.out.println("3. Convet to fett");
    System.out.println("4. Quit the program");
    number = keyboard.nextInt();

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Since you did not post all your code the problem may be in the code you omitted. Try and include all the relevant code and use code tags to make it easier to read.

    If this is your actual code and not just some attempt to replicate it then I can see that you do not have braces around your while loop. Which makes your code:
    Java Code:
    while (number != 4) {
        System.out.println("1. Convert to kilometers");
    }
    System.out.println("2. Convert to inches");
    System.out.println("3. Convet to fett");
    System.out.println("4. Quit the program");
    number = keyboard.nextInt();

  6. #6
    Mideoan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you for all your help so far. It is much appreciated. I have figured out the while statement. Now I have one last issue. The console is displaying all three values on separate lines. I want it to just return 1 value for the option.


    The console is returning something like this
    Enter a distance in meters:
    500
    1. Convert to kilometers
    2. Convert to inches
    3. Convet to feet
    4. Quit the program
    1
    0.5
    0.0
    0.0
    1. Convert to kilometers
    2. Convert to inches
    3. Convet to feet
    4. Quit the program
    2
    0.5
    19685.0
    0.0
    1. Convert to kilometers
    2. Convert to inches
    3. Convet to feet
    4. Quit the program
    3
    0.5
    19685.0
    1640.5
    1. Convert to kilometers
    2. Convert to inches
    3. Convet to feet
    4. Quit the program
    4
    0.5
    19685.0
    1640.5

    Here all of my code with comments to read easier.
    Java Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class Conversion_Program {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		{
    		// Declared variables
    		double myKilometers = 0;
    		double myInches = 0;
    		double myFeet = 0;
    		int number;
    		String input;
    		
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		// Get the user to input distance in meters and the option to how they want it converted
    		System.out.println("Enter a distance in meters:" );
    		input = keyboard.nextLine();
    		System.out.println("1. Convert to kilometers");
    		System.out.println("2. Convert to inches");
    		System.out.println("3. Convet to feet");
    		System.out.println("4. Quit the program");
    		number = keyboard.nextInt(); 
    		
    		
    	
    		// If statement to determine number of return conversion
    		
    		
    		if (number == 1)
    			 myKilometers = showKilometers(Double.parseDouble(input));
    			System.out.println(+ myKilometers);
    		 if (number == 2)
    			myInches = showInches(Double.parseDouble(input));
    		 	System.out.println(+ myInches);
    		 if (number == 3)
    			myFeet = showFeet(Double.parseDouble(input));
    		 	System.out.println(+ myFeet);
    		 
    		 	// while statement to keep the options repeating until the user chooses 4
    		 	
    		 	while (number != 4)
    		 	{
    		 	System.out.println("1. Convert to kilometers");
    			System.out.println("2. Convert to inches");
    			System.out.println("3. Convet to feet");
    			System.out.println("4. Quit the program");
    			number = keyboard.nextInt();
    			
    			if (number == 1)
    				 myKilometers = showKilometers(Double.parseDouble(input));
    				System.out.println(+ myKilometers);
    			 if (number == 2)
    				myInches = showInches(Double.parseDouble(input));
    			 	System.out.println(+ myInches);
    			 if (number == 3)
    				myFeet = showFeet(Double.parseDouble(input));
    			 	System.out.println(+ myFeet);
    		 	}
    		 	// Enter 4 to terminate program
    		 if (number == 4)
    			System.exit(0);

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  2. need some compatibility information
    By wildheart25c in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-25-2010, 08:29 AM
  3. storing information
    By bsebal28 in forum New To Java
    Replies: 3
    Last Post: 03-26-2009, 09:10 AM
  4. Security Information
    By saty_32016 in forum CLDC and MIDP
    Replies: 0
    Last Post: 03-05-2009, 09:14 AM
  5. how to get information of a file
    By leonard in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 06:03 PM

Posting Permissions

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