Jump to content

Number Occurrence using Array and different methods for input and output

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Tommy Ng

Tommy Ng

    Newbie

  • Members
  • Pip
  • 4 posts
What I'm trying to do is to find the occurrence of numbers between 1-100. The trick part is, the input and the output must be in different separate methods besides the main method.

Here is what I have. I am so lost at this point, I seem to got the code to running, but the numbers won't stack up!! =[

run:
FUN
Enter the integers between 1 and 100: 1 1 2 4 1 5 0
1 occurs 1 time
1 occurs 1 time
2 occurs 1 time
4 occurs 1 time
1 occurs 1 time
5 occurs 1 time

I want it to look like

run:
FUN
Enter the integers between 1 and 100: 1 1 2 4 1 5 0
1 occurs 3 times
2 occurs 1 time
4 occurs 1 time
5 occurs 1 time

package numberse;


import java.util.Scanner;



public class NumberSe {


    

   public static void main(String[] args) {

               System.out.println("FUN");

        userInput();

        

    }    

    

    

    public static void userInput() {

        Scanner input = new Scanner(System.in);


        int userIn = 0;

        for (userIn = 0; userIn < 100; userIn++) {

        }

        System.out.print("Enter the integers between 1 and 100: ");

        userIn = input.nextInt();

        while (userIn != 0) {

        printOut(userIn);

            

            userIn = input.nextInt();

        }

   }


    

    

   public static void printOut(int score) {

        



        int[] scores = new int[100];

        

        int[] counts = new int[100];

        


        for (int userIn = 0; userIn < 100; userIn++) {

        counts[userIn] = 0;

        }


        int userIn = score;

        while (userIn != 0) {

            counts[userIn]++;

            userIn = 0; 

        }

        

        

        

        for (userIn = 0; userIn < 100; userIn++) {

            if (counts[userIn] != 0) {

                if (counts[userIn] > 1) {

                    System.out.println(userIn + " occurs " + counts[userIn] + " times");

                } else {

                    System.out.println(userIn + " occurs " + counts[userIn] + " time");

                }

            }

        }

      

   }

}

I'm guessing my error happens in
        while (userIn != 0) {

            counts[userIn]++;

            userIn = 0; 

        }


since I set the value to 0 so it forces the program to stop in a way.

My thought process when I tried to do this was:

1. User enters the input other than 0 (inputs stored in one method)
2. The input is then passed onto my printOut method until a 0 is entered by the user


Can someone help me fix my code and show me how to do this properly with at least a main method, an input method and output method? Thanks! I really appreciate it!

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Your arrays:
int[] scores = new int[100];
int[] counts = new int[100];
Will die when the method ends + every time you run that method again they get newly created, they should be created outside.

What is the "scores" array for anyways? It's not used.

#3
Tommy Ng

Tommy Ng

    Newbie

  • Members
  • Pip
  • 4 posts
i figured it out in class today :w00t: i redid the whole code using 3 methods

a main method to call the array, an input with return method for user to enter integer, and a void output to receive and process the numbers




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users