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 6 of 6

Thread: help me with a conversion program please

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help me with a conversion program please

    okay so everything compiles BUT i have been stuck on why it doesnt work for days. When I compile it says " ----jGRASP: process aborted by user.

    ----jGRASP exec: java Conversion
    Wht am I doing wrong?


    import java.util.Scanner;
    import java.text.DecimalFormat;
     
    public class Conversion
    {
    	DecimalFormat formatter = new DecimalFormat("##0.0000");
     
    	public static void main(String[]args)
     
    	{
     
    	showMenu();
     
    	}
    	public static void showMenu()
    	{
     
    		double meters = 0.0;
    		Scanner keyboard = new Scanner(System.in);
    		do 
    		{
    		meters = keyboard.nextDouble();
     
     
     
    		if (meters<0)
    		System.out.println("Please enter a positive number");
    		meters=keyboard.nextDouble();
    		} 
    		while (meters>0);
    		menu(meters);
    	}
     
    	 public static void menu(double meters)
    	{
    		int selection=0;
    		Scanner keyboard = new Scanner(System.in);
     
     
    		while(selection !=4)
    		{
     
    		System.out.println("METER CONVERSION");
    		System.out.println("1) Convert to Kilometers");
    		System.out.println("2) Convert to Inches");
    		System.out.println("3) Convert to Feet");
    		System.out.println("4) Quit the Program");
    		System.out.println("Please make a selection:");
    		selection = keyboard.nextInt();
     
     
    			switch(selection)
    			{
     
    			case 1 :
    			showKilometers(meters);
    			System.out.println("Please enter the number of meters you want to convert:");
    			meters=keyboard.nextDouble();
    			break;
     
    			case 2:
     
    			showInches(meters);
    			System.out.println("Please enter the number of meters you want to convert:");
    			meters=keyboard.nextDouble();
    			break;
     
     
    			case 3:
    			showFeet(meters);
    			System.out.println("Please enter the number of meters you want to convert:");
    			meters=keyboard.nextDouble();
    			break;
     
    			case 4:
    			System.out.println();
    			break;
     
     
    			default:
    			System.out.println("Invalid selection.");
    			System.out.println();
     
    			}
    		}
    	}
     
     
    	public static double showKilometers(double meters)
    	{
    	double kilometers = 0.0;
    	kilometers = meters * 0.001;
     
    	System.out.println(meters+ " meters is " + kilometers + " kilometers.");
    	return kilometers;
    	}
    	public static double showInches(double meters)
    	{
    	double inches = 0.0;
    	inches=meters * 39.37;
     
    	System.out.println(meters + " meters is " + inches + " inches.");
    	return inches;
    	}
    	public static double showFeet(double meters)
    	{
    	double feet = 0.0;
    	feet=meters * 3.281;
     
    	System.out.println(meters + " meters is " + feet + " feet.");
    	return feet;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help me with a conversion program please

    Are you confusing compiling and running the program?
    The javac command compiles the source file and creates a .class file
    The java command executes the .class file.

    Can you try executing the program without JGrasp to see if there is a problem with the program?
    See if the java program will print out any error messages.

    Can you explain what happens when you execute the program?
    What does the program do?
    What do you do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with a conversion program please

    Well my program has to run like this over and over till the user quits the program
    METER CONVERSION
    1) Convert to Kilometers
    2) Convert to Inches
    3) Convert to Feet
    4) Quit the Program
    Please make a selection:
    Invalid selection.

    METER CONVERSION
    1) Convert to Kilometers
    2) Convert to Inches
    3) Convert to Feet
    4) Quit the Program
    Please make a selection:
    Please enter the number of meters you want to convert:
    Invalid entry.
    Please enter a positive number:
    Invalid entry.
    Please enter a positive number:
    Invalid entry.
    Please enter a positive number:
    125.0 meters is 0.1250 kilometers.


    but it doesnt run and shows this
    METER CONVERSION
    1) Convert to Kilometers
    2) Convert to Inches
    3) Convert to Feet
    4) Quit the Program
    Please make a selection:
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at Conversion.menu(Conversion.java:55)
    at Conversion.showMenu(Conversion.java:37)
    at Conversion.main(Conversion.java:18)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help me with a conversion program please

    Check the code in the while statement to be sure the condition is correct to keep the loop from going forever.

    When you print out a message that says the input is invalid, be sure to include the value of that input in the message so you know what the program is seeing.

    --- Update ---

    Check the code in the while statement to be sure the condition is correct to keep the loop from going forever.

    When you print out a message that says the input is invalid, be sure to include the value of that input in the message so you know what the program is seeing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with a conversion program please

    I been trying to see the error in this code for so long but can't seem to figure out what I'm doing wrong :/

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help me with a conversion program please

    Did you check the while statements? Are the conditions correct?

    Did you change the code to print out the invalid data?

    Please copy the full contents of the command prompt window for when you execute the program and paste it here. I don't see what the user entered in what was posted in post#3
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Question about methods, temperature conversion program
    By aivory616 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 23rd, 2012, 01:27 PM
  2. Help me with code conversion
    By davx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 04:29 PM
  3. Help with conversion
    By TheEvilCouncil in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2012, 08:29 PM
  4. BASE CONVERSION
    By bgwilf in forum Algorithms & Recursion
    Replies: 6
    Last Post: November 13th, 2010, 12:02 PM
  5. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM