-2

I am having issues with this array code. It needs to have a number count that displays the numbers that are entered by the user. So for instance, if the user enters three four's it will say "4 3." I already have the majority of the code done, but it is just returning the first number that's entered.

import java.util.Scanner;

public class NumberCounts 
{ 
  public static void main(String args[])
  { 
     int[] answer = new int [100];
     inputArray(answer);
  }

  public static void inputArray(int[] Numbercounts)
  {  
     System.out.println("This Program is written by Benjamin Barcomb\n");
     Scanner kb = new Scanner(System.in); 

     System.out.print("How many numbers will you enter? : ");
     kb.nextInt(); 
     //int[] answer = new int[100];

     int arryNum[] = new int[] {};

     System.out.println("\nEnter numbers in [0, 100] range separated by a space:");
     int input = kb.nextInt();

     System.out.println("Number Count");

     int[] counter = new int[] {input};
     for (int i = 0; i < arryNum.length; i++) {
        System.out.println("     " + (i + 1) + "      " + counter[i]);
     }

     for (int i = 0; i < counter.length; i++) {
        System.out.println("     " + (i + 1) + "     " + counter[i]);
     }
  }   
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    I feel this question is too vague. You need to ask with more focus. What, exactly, isn't working the way you expect it to? What aspect of your code's behavior do you find confusing? Do you fully expect it to work the way it's currently written? This seems more like homework help; I'm no moderator but I suspect StackOverflow is not the appropriate place – Dan Passaro Nov 30 '14 at 02:46

3 Answers3

2

Where to start...

1) You create an array answer of length 100, pass it to your method, then never use it anywhere.

2) You ask the user to tell you how many numbers they are going to enter, you get that input with kb.nextInt(), then do nothing with it. You never assign that int to a variable.

3) You create an array arryNum and leave it empty. You never put anything into it, ever.

4) You ask the user to input numbers separated by spaces. You then take only the first int they enter.

Since it seems like you are just learning I will leave the coding to you. Hopefully if you can now see what certain parts of your code are doing you will be able to move on from there. My one tip would be to use more descriptive variable names. It makes it easier for you to read what is going on when you look at your code.

Some Solutions

1) Unless the requirements say you must pass an array into the inputArray() method, I would just remove it all together since you are doing all the other work in the method. I would remove int[] answer = new int [100]; call the method with no parameters inputArray() and change the signature of the method to just

public static void inputArray() { ... }

2) When you ask the user how many numbers they are going to enter you are basically asking them "How long should my array be?" When you get this int you should use it to make your new array. Something like

System.out.print("How many numbers will you enter? ");
int usersNumbers = new int[kb.nextInt()];

Notice I have changed the name of arrayNum to usersNumbers. It's more descriptive and makes reading your code easier.

3) Now you want to get all those numbers and put them into that array. Since you need to get multiple numbers you will need a loop. Again, unless the requirements say you have to, I would do this a bit differently. Instead of entering all the numbers on one line separated by spaces, I would ask for the numbers individually. Since we know the length of the array (how many numbers they are going to enter), we know how many times to prompt them.

System.out.println("\nEnter numbers in [0, 100] range.\n");
for (int i = 0; i < usersNums.length; i++) {
    System.out.println("Enter Your Number:");
    usersNums[i] = kb.nextInt();
}

4) Now onto counting the number of occurrences of each int entered. Since the user can enter their numbers in any random order it makes things tougher. Again, I'm not sure what exactly your requirements allow or not, but here is what I did. First I want to get a list of the unique numbers. For example, if the array is {2, 4, 2, 5, 2, 4} I want to get a list with {2, 4, 5}. Once I have that I can look at each of those numbers and go through the array counting how many times I see each one. An easy way to get a list of unique numbers is to put them into a Set. It is a data structure which doesn't allow duplicates. A TreeSet is a Set which puts the numbers in order, just to make it easier to read.

Set<Integer> uniqueNumbers = new TreeSet<Integer>();
for (int i : usersNums) { uniqueNumbers.add(i); }

Now that I have the list of unique numbers I can start counting. I start count at 0. Then for every number in the set, I look at every number in the array and see if they are the same. If they are, count goes up by 1. Once I get to the end of the array I print my results for that number, then reset count to 0.

int count = 0;
for (Integer i : uniqueNumbers) {                 //For every number in the Set
    for (int j = 0; j < usersNums.length; j++) {  //Look at every number in the array
        if (i == usersNums[j]) { count++; }       //If the same, count++
    }
    System.out.print(i + " - " + count + "\n");   //Print results
    count = 0;                                    //Reset Count
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • I see what you are saying, but like you said I am definitely a beginner programmer. I'm not sure how to use that answer array anywhere. I do see what your saying with the arryNum array, but unsure where to use that as I'm using it in a loop later on in the program. I just do not understand arrays to be completely honest, and in all humility I do understand that I'm not smart when it comes to programming. If there is anymore assistance you could give I would greatly appreciate it. – Benjamin Barcomb Nov 30 '14 at 03:37
  • Afk atm but I will add more info soon. – takendarkk Nov 30 '14 at 03:38
  • @BenjaminBarcomb Added more info. To print the count I need to know if users are entering numbers in some random order (like 5 2 6 6 3 5 7) or are they in order (2 3 5 5 6 6 7). – takendarkk Nov 30 '14 at 04:11
  • thank you so much, they will be entering random numbers. – Benjamin Barcomb Nov 30 '14 at 04:15
  • Well that makes the count part harder lol. I will add info about that in a bit. – takendarkk Nov 30 '14 at 04:17
  • @BenjaminBarcomb Content added. – takendarkk Nov 30 '14 at 05:27
  • Thank You very much! I think I finally got it. Just a few minor quirks to fix up. Thank you for your time and help! – Benjamin Barcomb Nov 30 '14 at 05:54
  • Sure thing. Just be sure you are understanding the things you do so that you are actually learning, which it seems like you are already doing very well. – takendarkk Nov 30 '14 at 05:57
  • phew, I'm trying. I think I got the world's worst Java programming teacher for my first programming class though. Class average on the midterm was 45% haha. – Benjamin Barcomb Nov 30 '14 at 06:08
  • That seems to be a trend. Every teacher I ever had was far worse at programming than any entry level programmer I have met. – takendarkk Nov 30 '14 at 06:09
  • Why is your `int count = 0;` declaration outside the loop? If you put it inside the outer `for`, before the inner `for`, you don't need to have the last `count = 0` line – Dan Passaro Nov 30 '14 at 16:32
  • No particular reason. This is just how I thought of it. – takendarkk Nov 30 '14 at 17:57
1

It seems you're only getting the first value because you only called nextInt() on your scanner once. You should ideally have a while loop to keep gathering user input. Also, I think you're trying to store your answers in an array, which really isn't ideal since you don't know how big your input will be. You should really use a list. Something along the lines of

List<Integer> data = new ArrayList<Integer>();
while (kb.hasNext()) {
    data.add(kb.next());
}
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
rachelyen
  • 11
  • 2
  • 1
    ah, but isn't that the point? nextInt() was only called once when he wanted the user to input values, so op only got one value stored from the input. – rachelyen Nov 30 '14 at 02:56
  • The real problem is OP has several issues. This does help solve one of them. – Dan Passaro Nov 30 '14 at 03:08
  • I understand that I have several issues, this is why I'm reaching out for help. Trust me I have spent hours trying to figure it out. I am new to arrays so I need assistance. – Benjamin Barcomb Nov 30 '14 at 03:09
  • @BenjaminBarcomb I understand. As I said I'm as unofficial as it gets, but my understanding is StackOverflow is for very focused questions. This question (understandably) has several different aspects that need improvement. I would suggest /r/javahelp on reddit (although I'm not sure if I'm allowed to talk about the wider internet...) – Dan Passaro Nov 30 '14 at 03:15
  • @leo-the-manic I posted on Reddit as well, but stack overflow has always helped me more when it comes to programming questions. – Benjamin Barcomb Nov 30 '14 at 03:27
1

Explain your code hope you understand how to fix it.

First, I assume you want your user to enter more than one number but you use nextInt which means

The java.util.Scanner.nextInt() method Scans the next token of the input as an int.An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.

Suggestion: try to use to while loop to read more number and quite when your user enter -999999 for example as a termination for your while loop.

Second, use Array is not right data structure to use because you do not know the size of array.

There are 2 ways to fix this issue:

1. first to ask the size of array from the user like how many numbers the user wants to input which is not what you want.

2. use another data structure that shrinks and expands by itself which is ArrayList

Third, you did not say whether you gonna have different numbers in your input like

1 1 2 3 3 4 4 5  or unsorted like `1 2 1 4 3 4 5`

or just your input includes the same numbers like

2 2 2 2 2 2 2

More info for third point is here

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58