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: Loading array

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Loading array

    Hey guys -

    Alright, so for this assignment, we're "simulating" entering high temperature values into an array. The array is of size 32, and each index in the array corresponds to the day (hence why the a[0] is an invalid temperature).

    Here's the code.
    public static void loadManuel(int[] a) {
    		int length = a.length;
    		a[0] = 2043;
    		for (int i = 1; i < length; i++) {
    			System.out.print(i + " - ");
     
    			a[i] = scanner.nextInt();
    		}
     
    	}

    This code will work just fine, but sometimes when we're entering the temperature values we're supposed to "miss a day". What I'm wondering is, how do I make the code keep going even if there isn't data entered for that particular index?

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,163
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Loading array

    What does the user do to indicate a day is missing? Enter some other value? Simply press enter? Something else?

    How do you want the missing days represented? As some invalid temperature (what would that be)? As a null value? Do you want the size of the array to change, only including valid temperatures? Do you care about being able to tell which day had which temperature?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Loading array

    Right now, when I press the run button in Eclipse, it will start at number 1 and ask the user to enter an integer. If I press enter without entering anything, the program will quit. Since the teacher wants us to compute the average later on, I'd assume we want the value to be null since if we filled it with an invalid temperature that would affect the average.

    The array size will stay the same.

    The system.out.println(" - ") just means it'll put a dash. For example, if I enter 5 for a high at the date (index) 13, my result would look like "13 - 5".

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,163
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Loading array

    That's all well and good, but you still haven't told us how a user indicates that an index should be skipped, or what that means really.

    If you want to store null values in your array, you'll have to make it an array of Integers instead of ints. Integers are Objects, which are null by default, whereas ints are primitives, which are 0 by default.

    Also, why are you filling the first spot of the array (index 0) with an invalid value? Why not just make the array one index smaller?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. loading jsp pages in different browsers
    By sridevi karampudi in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: February 5th, 2013, 11:44 PM
  2. Loading Images and Edit it
    By shadihrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 25th, 2010, 08:39 PM
  3. Loading a file in a different folder
    By tiemykim in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:53 AM
  4. Loading in images
    By eXcellion in forum Java Theory & Questions
    Replies: 3
    Last Post: January 17th, 2010, 11:13 AM
  5. Replies: 3
    Last Post: April 14th, 2009, 08:35 AM