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

Thread: Array (simple).

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Array (simple).

    This code should allow the user to input the array length (less that 10), input data into the array, then view the data in the array. What is wrong with the code, and how do I fix it?

    import java.util.Scanner;
     
    public class Array {
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		int length;
    		int[] numbers;
    		int counter;
    		int count;
     
    		System.out.print("Length: ");
    		length = input.nextInt();
    		numbers = new int[length];
     
    		if(length <= 10){
    			for(counter = 0; counter < numbers.length; counter++);
    				{count = counter + 1;
    				System.out.println("Number "+count+": ");
    				numbers[counter] = input.nextInt();}
     
    			for(counter = 0; counter < numbers.length; counter++);
    				{System.out.println("Number "+counter+": "+numbers[counter]);}}
     
    		else{System.out.println("Too big");}
    	}
     
    }


  2. #2
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: Array (simple).

    you should post the errors you get...
    anyway why do you put a semicolon ; after the for loop? doing that you stop the loop there and leave alone the code below
    delete that semicolon and everything will work, the code should be like that

    for(int i; i<5;i++){
    //code
    }

  3. The Following 2 Users Say Thank You to chri For This Useful Post:

    18107 (December 25th, 2012), curmudgeon (December 25th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Array (simple).

    Thanks, it works now.

Similar Threads

  1. trouble with making a simple construct for an array of double type
    By Abadude in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 29th, 2012, 05:05 PM
  2. Simple Shopping Cart..... not so simple
    By jpsider in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 20th, 2012, 02:18 PM
  3. Error for a simple array
    By DudeJericho in forum Collections and Generics
    Replies: 4
    Last Post: April 25th, 2011, 02:46 PM
  4. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM

Tags for this Thread