Computer Science Canada

Illegal Start of Type Error in Java Program

Author:  Varsteil [ Thu Nov 01, 2012 9:41 am ]
Post subject:  Illegal Start of Type Error in Java Program

I'm new to Java programming and I was making a program to calculate Imperial and Metric conversions, however, I'm encountering 3 errors in my code; illegal start of type, identifier expected, and orphaned case. I'd appreciate it if anyone could tell me how to fix it.
code:
import java.util.Scanner;

public class MetricConversion {
  int number, choice = 0;
 
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the amount you want to convert");
    number = input.nextInt();
    System.out.println("Press 1 for Inches to Centimeters.");
    System.out.println("Press 2 for Feet to Centimeters.");
    System.out.println("Press 3 for Yards to Meters.");
    System.out.println("Press 4 for Miles to Kilometers.");
    System.out.println("Press 5 for Centimeters to Inches.");
    System.out.println("Press 6 for Centimeters to Feet.");
    System.out.println("Press 7 for Meters to Yards.");
    System.out.println("Press 8 for Kilometers to Miles.");
    System.out.print("Enter your choice: ");
    choice = input.nextInt(); 
    input.close();
  }
    switch (choice) { //illegal start of type// //<identifier> expected//
      case 1: inchesToCentimeters(); break; //orphaned case//
      case 2: feetToCentimeters(); break;
      case 3: yardsToMeter(); break;
      case 4: milesToKilometers(); break;
      case 5: centimetersToInches(); break;
      case 6: centimetersToFeet(); break;
      case 7: metersToYards(); break;
      case 8: kilometersToMiles(); break;
    }
 
public static void inchesToCentimeters() {
  double cm;
  cm = number * 2.54;
  System.out.println(number + " inches equals " + cm + " centimeters");
}
public static void feetToCentimeters() {
  double cm;
  cm = number * 30;
  System.out.println(number + " feet equals " + cm + " centimeters");
}
public static void yardsToMeter() {
  double meter;
  meter = number * 0.91;
  System.out.println(number + " yards equals " + meter + " meters");
}
public static void milesToKilometers() {
  double km;
  km = number * 1.6;
  System.out.println(number + " miles equals " + km + " kilometers");
}
public static void centimetersToInches() {
  double inches;
  inches = number / 2.54;
  System.out.println(number + " centimeters equals " + inches + " inches");
}
public static void centimetersToFeet() {
  double feet;
  feet = number / 30;
  System.out.println(number + " centimeters equals " + feet + " feet");
}
public static void metersToYards() {
  double yards;
  yards = number / 0.91;
  System.out.println(number + " meters equals " + yards + " yards");
}
public static void kilometersToMiles() {
  double miles;
  miles = number / 1.6;
  System.out.println(number + " kilometers equals " + miles + " miles");
}
}

Author:  DemonWasp [ Thu Nov 01, 2012 9:49 am ]
Post subject:  RE:Illegal Start of Type Error in Java Program

Your switch is outside your main(){} method. Put it inside the main() method.

It would be easier to read your code (and find this type of problem) if you indent correctly.

Author:  Varsteil [ Thu Nov 01, 2012 2:46 pm ]
Post subject:  Re: Illegal Start of Type Error in Java Program

Thanks, it fixed my error but now I'm getting a bunch of
"Cannot make a static reference to the non-static field number" errors
I think it means that it can't make a reference to my variables, but how do I fix it?
I'm not going to post the code again because there's a lot of errors but every line that has a variable in it gets this error.

Author:  Insectoid [ Thu Nov 01, 2012 6:10 pm ]
Post subject:  RE:Illegal Start of Type Error in Java Program

number and choice should be declared as static ints.

Author:  Varsteil [ Fri Nov 02, 2012 9:26 am ]
Post subject:  Re: Illegal Start of Type Error in Java Program

Thanks, that fixed it


: