A program with input and output on different method besides the main method
The code posted below is an occurrence problem with how many times a number from 1-100 occurs, program stops executing when user enters 0. As for my homework assignment, I need to have the input and output in separate methods besides the main method. The first thing I did was write the whole program, then I got the input/output to another method... now I am stuck. I can't seem to figure out how to get the input and output to be in their own methods. Please help and thanks!
Code :
package numberse;
import java.util.Scanner;
public class NumberSe {
public static void main(String[] args) {
System.out.println("FUN");
userInput(args);
}
public static void userInput(String[] args) {
Scanner input = new Scanner(System.in);
int[] count = new int[100];
int userIn = 0;
for (userIn = 0; userIn < 100; userIn++) {
count[userIn] = 0;
}
System.out.print("Enter the integers between 1 and 100: ");
userIn = input.nextInt();
while (userIn != 0) {
count[userIn]++;
userIn = input.nextInt();
}
for (userIn = 0; userIn < 100; userIn++) {
if (count[userIn] != 0) {
if (count[userIn] > 1) {
System.out.println(userIn + " occurs " + count[userIn] + " times");
} else {
System.out.println(userIn + " occurs " + count[userIn] + " time");
}
}
}
}
}
Re: A program with input and output on different method besides the main method
One of the problems you will face is how to access data in the different methods. Say you have one method to input all the numbers, how do you access those numbers in another method to display the results. You have two options. Declare a bunch of instance variables that all methods have access to (this will require creating an object in the main method to start things off). Or you can pass all the data about as parameters.
There's soemthing to get you started thinking about.
Re: A program with input and output on different method besides the main method
I just learned how to do arrays.. I'm not sure how to do it, but I tried changing the code and changed it to this. But now I am stuck, please help!
Code :
package numberse;
import java.util.Scanner;
public class NumberSe {
public static void main(String[] args) {
System.out.println("FUN");
userInput(args);
}
public static void userInput(String[] args) {
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[] count = new int[100];
for (score = 0; score < 100; score++) {
count[score] = 0;
}
while (score != 0) {
count[score]++;
}
for (score = 0; score < 100; score++) {
if (count[score] != 0) {
if (count[score] > 1) {
System.out.println(score + " occurs " + count[score] + " times");
} else {
System.out.println(score + " occurs " + count[score] + " time");
}
}
}
}
}
Re: A program with input and output on different method besides the main method
Ok, a few things.
Arguments:
In your
Code Java:
public static void userInput(String[] args)
{
//Code Eluded
}
method, you don't actually ever use the parameter args. It would be better if it is just
Code Java:
public static void userInput()
{
//Code Eluded
}
Indenting/Syntax:
I recommend you look at Java Style Guide: Formatting: White Space
Array Help:
http://download.oracle.com/javase/tu...ts/arrays.html
Initializing Arrays:
Specifically:
Code Java:
int[] count = new int[100];
for (score = 0; score < 100; score++) {
count[score] = 0;
}
That code doesn't do anything different than
Code Java:
int[] count = new int[100];
because it automatically initializes all the variables in the array to 0.
And finally:
How To Ask Questions The Smart Way
Be Precise