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

Thread: Need help with 2D Array

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Need help with 2D Array

    Working on a project where I have to use a 2D array to store highest and lowest temperatures for each month of the year.

    Trying to get the first bit of it to compile but I keep getting these errors:

    Line 33: variable high might not have been initialized
    for (int high; high < array.length; high++)

    Line 35: variable low might not have been initialized
    for (int low; low < array.length; low++)

    Seems like a simple error, but I just can't figure it out..
    here is the code:

    import java.util.*;
     
    public class HighestAndLowestTemperature
    {
    	static Scanner console = new Scanner(System.in);
     
    	static int array;
    	static int high;
    	static int low;
    	static int index;
     
    	public static void main(String[] args)
    	{
    		System.out.println ("Enter the highest temperature for each month of the year: ");
    		high = console.nextInt();
     
    		System.out.println("Enter the lowest temperature for each month of the year: ");
    		low = console.nextInt();
     
    		int[][] array = new int[high][low];
     
    	}
    	public static void getData(int array[][])
    	{
    		for (int high; high < array.length; high++)
    		{
    			for (int low; low < array.length; low++)
    			System.out.print(array[high][low] + " ");
    		}
    	}
    }

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with 2D Array

    variable high might not have been initialized
    The error message says that the compiler does not see where you have assigned a value to the high variable before you use it.
    Check your code and make sure you give it a value BEFORE you try to use it.

    The normal syntax for a for statement is:
    for (int high = 0; high < array.length; high++)

  3. The Following User Says Thank You to Norm For This Useful Post:

    n00bprogrammer (September 3rd, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help with 2D Array

    ergh.. Can't believe I didn't catch that.

    Thanks

  5. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help with 2D Array

    Sorry to be a pest but I'm having another problem that I just can't figure out.

    To get input from the user I figured I would need another for loop to get the highest temps for each 12 months and then the lowest temps for each 12 months. My code is compiling but when I run it.. it loops through everything and doesn't allow me to enter any data...

    import java.util.*;
     
    public class HighestAndLowestTemperature
    {
    	static Scanner console = new Scanner(System.in);
     
    	static int array;
    	static int high;
    	static int low;
    	static int index;
     
    	public static void main(String[] args)
    	{
    		int[][] array = new int[high][low];
     
    		System.out.println ("Enter the 12 highest temperature for each month of the year: ");
    		for (int high = 0; high < array.length; high++)
    		{
    			high = console.nextInt();
    			System.out.println();
    		}
    		System.out.println("Enter the 12 lowest temperature for each month of the year: ");
    		for (int low = 0; low < array.length; low++)
    		{
    			low = console.nextInt();
    			System.out.println();
    		}
     
     
    	}
    	public static void getData(int array[][])
    	{
    		for (int high = 0; high < array.length; high++)
    		{
    			for (int low = 0; low < array.length; low++)
    			System.out.print(array[high][low] + " ");
    		}
    	}
    }

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with 2D Array

    What controls how many times you go thru a for loop? Is that value > 0?
    If it is zero will you loop any times?

    Look back at your code in your first post.

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM

Tags for this Thread