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

Thread: Using enum, returning null

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using enum, returning null

    So this was a homework problem, but it's already been turned in. My instructor just hinted that we would be expanding on this program later, so I'm hoping to fix it. We were supposed to create a program that used a World class to create new worlds. I chose to use an enum and a random number generator to randomly choose a terrain for each world, then assign it a random temperature. Problem is, everything returns null. I get no errors from the compiler (eclipse), so it's obviously a logical error. I hate using enums, and could have done it easily several other ways, but now I'm frustrated and would appreciate any help fixing this so I can finally learn enums properly. Here is the World class:

    import java.util.Random;
     
    public class World 
    {
    	// attributes of every world
    	private final int MAX_WORLDS = 5;
    	private String[] terrain = new String[MAX_WORLDS];
    	private String[] name = new String[MAX_WORLDS];
    	private int[] avgTemp = new int[MAX_WORLDS];
    	private int count = 0;
     
    	// random number generator
    	Random rand = new Random();
     
    	// enum to pick random terrains
    	private enum terrainType {MOUNTAINOUS, DESERT, PLAINS, COASTAL};
     
    	// method will create a world with random terrain and temp
    	public void createWorld()
    	{
    		terrainType ter = terrainType.values()[rand.nextInt(4)];
    		//pickTerrain(ter);
    		terrain[count] = new String();
    		//int avgTemp[count] = new int[5];
     
    		// assigns an average temp based on terrain
    		switch(ter)
    		{
    		case MOUNTAINOUS:
    			terrain[count].equals("Mountainous");
    			avgTemp[count] = rand.nextInt(40)+50;
    			break;
    		case DESERT:
    			terrain[count].equals("Desert");
    			avgTemp[count] = rand.nextInt(30)+90;
    			break;
    		case PLAINS:
    			terrain[count].equals("Plains");
    			avgTemp[count] = rand.nextInt(40)+50;
    			break;
    		case COASTAL:
    			terrain[count].equals("Coastal");
    			avgTemp[count] = rand.nextInt(20)+60;
    			break;
    		default:
    			System.out.println("error");
    			break;
    		}
    		count++;
    	}
     
    	// displays worlds created
     
    	public String displayWorld(int a)
    	{
    		return "Terrain: " + terrain[a] + "\nAverage Temp: " + avgTemp[a];
    	}
    	/*private terrainType pickTerrain(terrainType ter)
    	{
    		int terrain = rand.nextInt(4);
     
    		return terrainType.values()[terrain];
    	}*/
     
    }

    And here is the driver I used to test it:

    import java.util.Scanner;
     
    public class Driver 
    {
     
    	// ------------------------------------------
    	// The main allows the user to create worlds
    	// ------------------------------------------
    	public static void main(String[] args) 
    	{
    		int choice = 3;
    		int count = 0;
    		World[] world = new World[5];
     
    		// executes until the user decides to quit
    		while(choice != 0)
    		{
    			Scanner scan = new Scanner(System.in);
     
     
    			System.out.println("Enter 1 to create a world\nEnter 2 to Display current world\n0 to quit");
    			choice = scan.nextInt();
    			world[count] = new World();
     
    			// evaluates user decision
    			if(choice == 1)
    			{
    				world[count].createWorld();
    			}
    			// prints current world
    			else if(choice == 2)
    			{
    				System.out.println(world[count].displayWorld(count));
    			}
     
    	}
    	}
     
    }

    I know it's pretty sloppy, but a lot of it was improvised trying to fix errors. Any help would be much appreciated.


  2. #2
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Using enum, returning null

    So here is what I'm seeing that could be causing some issues. First the Driver class:
    1. You set world[count] to a new instance of World regardless of whether or not one needs to be created.
    2. count never gets incremented after a world is created.
    3. Right now there is no check for whether or not the count is maxed out or not.

    The World class:
    1. You have it set so that each instance of this class will create 2 arrays of length 5, I do not see any purpose in this. Each instance only needs one avgTemp and one terrain.
    2. The count variable never gets incremented, if you want it to get +1 for each instance created then you will want it to be static. However, if you get rid of the arrays then it isn't needed.
    3. the equals method you use returns a boolean, it does not set the value of the variable.

    Start out with that and see what you can do. Hope this helped.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using enum, returning null

    Wow. Very obvious this was the first assignment of the semester. Not sure what I was thinking. Thanks.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using enum, returning null

    Took your suggestions on the World class and they worked perfectly. Started from scratch on the driver since it was garbage, lol. Thanks again.

Similar Threads

  1. [SOLVED] Enum types, how does he know %s value?
    By beer-in-box in forum Java Theory & Questions
    Replies: 11
    Last Post: September 17th, 2011, 12:47 PM
  2. String to Enum problems...
    By alex13809 in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 2nd, 2011, 10:59 AM
  3. ClassCastException with Enum Combo Box
    By Diplo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2011, 01:22 PM
  4. [SOLVED] Enum problem
    By pajfilms in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2011, 07:26 AM
  5. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM