5 Replies - 461 Views - Last Post: 06 October 2013 - 01:57 PM Rate Topic: -----

#1 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

What am I doing wrong with this program?

Posted 06 October 2013 - 01:46 PM

What am I doing wrong with this code? I have been looking at this for the last hour and cannot figure out what is going on.

import java.util.*;

public class DebugNine4 {
	enum Planet {
		MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE
	};

	public static void main(String[] args) {
		Planet planet;
		String userEntry;
		int position;
		int comparison;
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a planet in our solar system >> ");
		planet = input.nextLine().toUpperCase();
		planet = Planet.valueOf(planet);
		System.out.println("You entered " + planet);
		position = planet.ordinal();
		System.out.println(planet + " is " + (position + 1)
				+ " planet(s) from the sun");
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: What am I doing wrong with this program?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: What am I doing wrong with this program?

Posted 06 October 2013 - 01:47 PM

Define "wrong".
Was This Post Helpful? 0
  • +
  • -

#3 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

Re: What am I doing wrong with this program?

Posted 06 October 2013 - 01:49 PM

Error I am getting:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Type mismatch: cannot convert from String to DebugNine4.Planet
	The method valueOf(String) in the type DebugNine4.Planet is not applicable for the arguments (DebugNine4.Planet)

	at DebugNine4.main(DebugNine4.java:15)


Was This Post Helpful? 0
  • +
  • -

#4 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: What am I doing wrong with this program?

Posted 06 October 2013 - 01:55 PM

The reason this doesn't work is because the most basic enums do not have String values. They have int values. Your program accepts String values as inputs, not integers. Therefore, the comparisons you're attempting to perform are invalid.
Was This Post Helpful? 1
  • +
  • -

#5 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

Re: What am I doing wrong with this program?

Posted 06 October 2013 - 01:56 PM

Ahh, okay. Sometimes you are looking at code so long you miss the obvious.
Was This Post Helpful? 0
  • +
  • -

#6 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: What am I doing wrong with this program?

Posted 06 October 2013 - 01:57 PM

This line:
planet = input.nextLine().toUpperCase();

planet is of type Planet and input is of type Scanner. So the compiler tells you that they do not match.
I think you wanted to do something like this:
userEntry = input.nextLine().toUpperCase();


This line:
planet = Planet.valueOf(planet)

Where is that method? It's not in the code provided.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1