9 Replies - 812 Views - Last Post: 06 April 2010 - 09:23 PM Rate Topic: -----

#1 mothan  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 28-February 10

Temperature Conversion

Posted 06 April 2010 - 07:40 PM

Can someone please help me! I'm doing a loop and I should have 3 options: 1. Fahrenheit to Celsius, 2. Celsius to Fahrenheit, and 3. exit. This is my code

public class Project2 {
    private static int num;



   public static void main(String[] args) {
//Create a Scanner
        Scanner reader = new Scanner(System.in);

        //two variables, undefined.

        double usersEnteredTemperature; //the temperature you wish to get from
        //the user.
        double convertedTemperature;    //the converted temperature based on
        //the user choices and entered temperature.

        //Enter Input
        //Give user options and choices.



      int menuChoice;

      double celsius;
      double fahrenheit;

      do {

         System.out.println("Enter the number your choice");
        System.out.println("1. For Farenheit to Celsius");
        System.out.println("2. For Celsius to Farenheit");
        System.out.println("3.Exit");
        int num = reader.nextInt();

                 if (num == 1) {
            System.out.println("Enter Temperature in Farenheit : ");
            usersEnteredTemperature = reader.nextDouble();

            //do some conversion based on userinput store output into
            //convertedTemperature Celsius
            convertedTemperature = (5.0 / 9) * (usersEnteredTemperature - 32);
            System.out.println("Your degrees will be in celsius");
            System.out.println(convertedTemperature);
        } else if (num == 2) {
            System.out.println("Enter Temperature in Celsius: ");
            usersEnteredTemperature = reader.nextDouble();

            //do some conversion based on userinput store output into
            //convertedTemperature Fahrenheit
            convertedTemperature = (9.0 / 5) * usersEnteredTemperature + 32;
            System.out.println("Your degrees will be in Farenheit");
            System.out.println(convertedTemperature);
        }

         } while (num != 3);

        System.out.println("Good Bye!");

    }
}


Help please! This is the error I'm getting.

Quote

run:
Exception in thread "main" java.lang.UnsupportedOperationException: Not yet implemented
at Scanner.<init>(Scanner.java:16)
at Project2.main(Project2.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)


Moderator Edit: Please, in the future, :code:.

-Locke

Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Conversion

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Temperature Conversion

Posted 06 April 2010 - 08:02 PM

you have to import java.util.* package which contains the scanner class
Was This Post Helpful? 0
  • +
  • -

#3 LiveToCode  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 37
  • Joined: 17-March 10

Re: Temperature Conversion

Posted 06 April 2010 - 08:07 PM

You forgot to import java.util.Scanner; and you should replace the variable "num" with "menuChoice" because menuChoice is more decriptive it is declared for a reason right. The variable num in this case is not necessary so you can just delete it.

Edit - Ninja'ed

This post has been edited by LiveToCode: 06 April 2010 - 08:08 PM

Was This Post Helpful? 0
  • +
  • -

#4 mothan  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 28-February 10

Re: Temperature Conversion

Posted 06 April 2010 - 08:34 PM

I've add the import java.util.Scanner; and change the num to menuChoice, here is my code

package project2;

import java.util.Scanner;

public class Project2 {



public static void main(String[] args) {
//Create a Scanner
Scanner reader = new Scanner(System.in);

//two variables, undefined.

double usersEnteredTemperature; //the temperature you wish to get from
//the user.
double convertedTemperature; //the converted temperature based on
//the user choices and entered temperature.

//Enter Input
//Give user options and choices.



int menuChoice;

double celsius;
double fahrenheit;

do {

System.out.println("Enter the number your choice");
System.out.println("1. For Farenheit to Celsius");
System.out.println("2. For Celsius to Farenheit");
System.out.println("3.Exit");
int menuChoice= reader.nextInt();

if (menuChoice == 1) {
System.out.println("Enter Temperature in Farenheit : ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Celsius
convertedTemperature = (5.0 / 9) * (usersEnteredTemperature - 32);
System.out.println("Your degrees will be in celsius");
System.out.println(convertedTemperature);
} else if (menuChoice == 2) {
System.out.println("Enter Temperature in Celsius: ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Fahrenheit
convertedTemperature = (9.0 / 5) * usersEnteredTemperature + 32;
System.out.println("Your degrees will be in Farenheit");
System.out.println(convertedTemperature);
}

} while (menuChoice != 3);

System.out.println("Good Bye!");

}
}


The error I got is this.
run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - class Project2 is public, should be declared in a file named Project2.java
at project2.Project2.<clinit>(Main.java:11)
Could not find the main class: project2.Project2. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)


Help please!
Was This Post Helpful? 0
  • +
  • -

#5 MadScientist305  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 44
  • Joined: 26-December 09

Re: Temperature Conversion

Posted 06 April 2010 - 08:38 PM

Your public class is named Project2 yet the file that contains this code is not named Project2.java. Either rename your class to match your file name, or rename the file to match the class name.
Was This Post Helpful? 0
  • +
  • -

#6 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Temperature Conversion

Posted 06 April 2010 - 08:39 PM

Please use the code tags! :code: :)

Quote

class Project2 is public, should be declared in a file named Project2.java

Is your class named Project2.java?

Also, since you declare the source to be in the package "project2", it should be in a folder named project2

http://java.sun.com/...agingfiles.html
Was This Post Helpful? 0
  • +
  • -

#7 mothan  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 28-February 10

Re: Temperature Conversion

Posted 06 April 2010 - 09:05 PM

I've clean it up but it won't let me debug or run. please advice!
Was This Post Helpful? 0
  • +
  • -

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Temperature Conversion

Posted 06 April 2010 - 09:10 PM

Could you post your modified code?
Was This Post Helpful? 0
  • +
  • -

#9 mothan  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 28-February 10

Re: Temperature Conversion

Posted 06 April 2010 - 09:12 PM

Here is my code:
package project2;

import java.util.Scanner;

public class Project2 {


   
   public static void main(String[] args) {
//Create a Scanner
        Scanner reader = new Scanner(System.in);

        //two variables, undefined.

        double usersEnteredTemperature; //the temperature you wish to get from
        //the user.
        double convertedTemperature;    //the converted temperature based on
        //the user choices and entered temperature.

        //Enter Input
        //Give user options and choices.
        


      int menuChoice = 0;   
                         
      double celsius;  
      double fahrenheit; 

      do {

         System.out.println("Enter the number your choice");
        System.out.println("1. For Farenheit to Celsius");
        System.out.println("2. For Celsius to Farenheit");
        System.out.println("3.Exit");
        

                 if (menuChoice == 1) {
            System.out.println("Enter Temperature in Farenheit : ");
            usersEnteredTemperature = reader.nextDouble();

            //do some conversion based on userinput store output into
            //convertedTemperature Celsius
            convertedTemperature = (5.0 / 9) * (usersEnteredTemperature - 32);
            System.out.println("Your degrees will be in celsius");
            System.out.println(convertedTemperature);
        } else if (menuChoice == 2) {
            System.out.println("Enter Temperature in Celsius: ");
            usersEnteredTemperature = reader.nextDouble();

            //do some conversion based on userinput store output into
            //convertedTemperature Fahrenheit
            convertedTemperature = (9.0 / 5) * usersEnteredTemperature + 32;
            System.out.println("Your degrees will be in Farenheit");
            System.out.println(convertedTemperature);
        }

         } while (menuChoice != 3);

        System.out.println("Good Bye!");

    }
}


Edited by Dogstopper: USE THE CODE TAGS! :code:

This post has been edited by Dogstopper: 07 April 2010 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Temperature Conversion

Posted 06 April 2010 - 09:23 PM

Please use the code tags! :code:

Your code compiled fine for me, are you sure you're setting up the folders for the package correctly?

Also, you never accept user input, so there is an infinite loop
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1