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: What Am I Doing Wrong?(Arrays)

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What Am I Doing Wrong?(Arrays)

    So were suppose to create a program that reads in integers in the range of 0-50, puts them in an array
    and prints out how many times each integer is entered. Sorry I forgot to add that its not working properly and am wondering what can I fix?

    package HW12;
     
    import java.util.Scanner;
     
    //HW12.java		Stephen Wells
    //CS140-01		
    //This program reads in integers in the range of 0-50, puts them in an array
    //and prints out how many times each integer is entered.
     
    public class HW12
    {
    public static void main (String [] args)
     
    {
    	//Explains what the program does
    System.out.println("This program reads in integers in the range of 0-50, " +
    			"puts them in an array and prints out how many times each integer is entered.");
    //Build the Scanner object so user can read in information from the keyboard
    Scanner scan = new Scanner (System.in);	
     
    //Declare an array called "occurrences" that will hold 51 integers (0-50)
    	System.out.println("type size of array: ");
    	int number = scan.nextInt();
    	int[] array=new int[number];
     
    //Prompt the user to enter a random number of integers from 0-50
    for(int i=0; i<array.length; i++)
    {
    array[i]=scan.nextInt();
    }
    System.out.println("The integers entered are: ");
     
    //Get the integers from the keyboard
    for(int i=0; i<array.length;i++)
    {
    System.out.println(array[i]);
    }
    for(int i=0; i<array.length;i++)
    {
    System.out.println(i+": "+array[i]);
    }
     
     
     
    //Use a while loop to validate that the user enter an integer between 0 and 50 
    //the body of the while loop will capture the integers and put them in the array while 
    //counting them
    while (number < 0 || number > 50)
    {
    System.out.println("invalid - please enter a number between 0 and 50");
    number = scan.nextInt();
    }
     
    //Print the results using a for loop and an if statement4To print out the numbers you then just need to change your last for loop to be:
      for (int i = 0; i < array.length; i++) {
    	  if (array[i] > 0) {
    	  System.out.println(i + "The total number of times the integer is entered is: " + array[i]);
    	          }
            }
     
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,646
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: What Am I Doing Wrong?(Arrays)

    Did you have any questions about the posted code?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: What Am I Doing Wrong?(Arrays)

    Quote Originally Posted by zendorz View Post
    Sorry I forgot to add that its not working properly and am wondering what can I fix?
    You also forgot to say what the program should do, what the program does do, and if there are any errors associated with the problem you have.
    "What can i fix" is not a question you are likely to get a good answer to. Try to explain what is going on and what you are trying to accomplish. Ask questions that are more specific about what you don't understand.

Similar Threads

  1. arrays ... what am i doing wrong?
    By crazyboy5012 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2013, 06:25 AM
  2. Help with Arrays!
    By le pencil in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2013, 12:05 PM
  3. 3 Arrays
    By D3158 in forum Collections and Generics
    Replies: 27
    Last Post: June 16th, 2011, 11:10 AM
  4. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 04:27 PM
  5. arrays
    By dwamalwa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2010, 12:44 AM