I have an issue. My lecturer wants me to make a loop, with an input of JOptionPane and an output of console. How can I use loop for JOptionPane and send an output through console.
Here's my code:
int even = 0;
int odd = 0;
int e_e = 0;
int o_o = 0;
String a1 = JOptionPane.showInputDialog(null, "Type in 10 integer");
for (int counter = 0; counter < 10; counter++){
int a = Integer.parseInt(a1);
if (a % 2 == 0) {
even++;
e_e += a;
} else {
odd++;
o_o += a;
}
}
System.out.println("\n\nNumber of even numbers : " + even);
System.out.println("Number of odd numbers : " + odd);
System.out.println("Total of even numbers : " + e_e);
System.out.println("Total of odd numbers : " + o_o);
I would try using a DO-WHILE loop with and an int[], example:
int size = 10;
int count = 0;
int[] yourNumbers = new int[size];
do {
yourNumbers[count] = Integer.parseInt(JOptionPane.showInputDialog(null,
"Your message here."));
count++;
} while (count < 10);
This way you can loop through and grab all the numbers. Then you can use a FOR-LOOP to cycle through and print what you need
System.out.println("Even Numbers are: ");
for(int i = 0; i < yourNumbers.length; i++) {
if (yourNumbers[i] % 2 == 0) {
System.out.println(yourNumbers[i]);
}
}
System.out.println("Odd Numbers are: ");
for(int i = 0; i < yourNumbers.length; i++) {
if (yourNumbers[i] % 2 != 0) {
System.out.println(yourNumbers[i]);
}
}
The problem with your current code is that you only ask the user one time to input a number but you actually seem to want 10 values. So you parse ten times the same value.
The solution is simple, put the dialog inside the loop (only changed the lines with comments):
int even = 0;
int odd = 0;
int e_e = 0;
int o_o = 0;
// No return type, just a message
JOptionPane.showMessageDialog(null, "Type in 10 integer");
for (int counter = 0; counter < 10; counter++) {
// Dialog inside the loop, asking to
// input a number in every iteration
String value = JOptionPane.showInputDialog(null, "Type in "
+ (counter + 1) + ". value");
int a = Integer.parseInt(value);
if (a % 2 == 0) {
even++;
e_e += a;
} else {
odd++;
o_o += a;
}
}
System.out.println("\n\nNumber of even numbers : " + even);
System.out.println("Number of odd numbers : " + odd);
System.out.println("Total of even numbers : " + e_e);
System.out.println("Total of odd numbers : " + o_o);
Related
This code is designed to list off the prime numbers between a minimum and maximum input by the user. When the numbers are output at the end of the code, they are all on the same line. I would like there to be ten numbers per line, so I assume that it takes a loop of some kind to indent every ten numbers, however I don't know how to do this. While I have the code posted here, other, unrelated feedback would be helpful.
String primenumbers = "";
System.out.println("Prime Number Generator.");
System.out.print("Minimum: ");
int oldmin = s.nextInt();
s.nextLine();
int min = oldmin;
System.out.print("Maximum: ");
int max = s.nextInt();
s.nextLine();
System.out.println();
for (min = min; min <= max; min++)
{
int counter=0;
int num = min;
for(num = min; num >= 1; num--)
{
if(min % num == 0)
{
counter = counter + 1;
}
}
if (counter == 2)
{
primenumbers = primenumbers +min+ " ";
}
}
System.out.println("Primes Between "+oldmin+" & "+max+":");
System.out.print(primenumbers);
Instead of concatinating prime numbers into a string, you can add them into a list and iterate the list in the end, e.g.:
primenumbers = primenumbers +min+ " "; will be replaced with
List<Integer> primenumbers = new ArrayList<>();
primenumbers.add(min);
After System.out.println("Primes Between "+oldmin+" & "+max+":"); statement, you can do the following:
for(int i=0 ; i < primenumbers.size() ; i++){
System.out.print(primenumbers.get(i) + " ");
if(i+1 % 10 == 0){
System.out.println();
}
}
I have written a small program that takes user input, n (int), and calculates every prime number up to n, using a loop in a loop. If the user inputs forexample 85023, it will be a very long series of numbers. How do i make the output break every 10th number? this would make the output alot more proffesional and neat to look at.
Relevant code:
for (i = 1; i <= n; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
// Append prime numbers to string
primeNumbers = primeNumbers + i + " ";
}
}
// Print Number
System.out.println("Primtallene fra 1 til " + n + " er: ");
System.out.println(primeNumbers);
}
You could have a separate counter which counts the number of prime numbers you have currently found, like this:
int primeCount = 0;
for (i = 1; i <= n; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
// Append prime numbers to string
primeNumbers = primeNumbers + i + " ";
// Increment prime counter
primeCount++;
// If 10 primes have been printed then add new line and reset counter
if ((primeCount % 10) == 0) {
primeNumbers += "\n";
}
}
}
// Print Number
System.out.println("Primtallene fra 1 til " + n + " er: ");
System.out.println(primeNumbers);
}
\n is the newline character, hope this helps.
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int[] array = new int[5];
System.out.print("Please enter five numbers. \na=");
array[0] = input.nextInt();
System.out.print("\nb=");
array[1] = input.nextInt();
System.out.print("\nc=");
array[2] = input.nextInt();
System.out.print("\nd=");
array[3] = input.nextInt();
System.out.print("\ne=");
array[4] = input.nextInt();
boolean totalIsZero = false;
for (int i=0;i<array.length ;i++) {
for (int j=1;i>j ;j++ ) {
if ((array[i] + array[j])==0) {
System.out.println("The numbers " + array[i] + " and " + array[j] + " have a total sum equal to 0.");
totalIsZero = true;
}
}
}
if (!totalIsZero) {
System.out.print("None of the numbers have a total sum of 0 with each other. ");
}
}
Here is some simple code I just wrote. Its task is to check if the sum between every two numbers in an array (consisting of five numbers) is equal to zero.
The problem I have is that when there are two pairs of numbers, both equal to 0, at the end of the program there is a message for one of the pairs only, not for both, as I expected.
How can I fix that, so the user can read that there are two pairs of numbers equal to 0?
Not sure if this will work perfectly because I haven't tested it and I haven't used java in a while, but just create the array the same way you do it in your post, but try the rest for the actual bulk of the function.
// various input calls above^ to create array
int count = 0;
for(int i = 0; i < array.length; i++)
{
for(int j = i + 1; j < array.length; j++)
{
if(array[i] + array[j] == 0)
{
System.out.println("The numbers " + array[i] + " and " +
array[j] +
" have a sum equal to zero.");
count++;
}
}
}
if(count == 0)
{
System.out.println("No sum between any numbers is equal to 0");
}
This method allows the user to input the rainfall for every month of the year. I'm trying to prevent data less than zero from being stored in the array. I'm using a do-while loop, but I can't seem to figure out how to check if the input is less than zero. Thanks for your help guys, cheers!
public static double[] getRainFall()
{
double[] rainfallMonths = new double[12];
double[] rainfall = new double[12];
do
{
for(int x = 0; x < rainfallMonths.length; x++)
{
System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
rainfallMonths[x] = keyboard.nextDouble();
rainfall[x] = rainfallMonths[x];
if(rainfallMonths < 0)
{
System.out.println("Input is Invalid");
}
}
}while(rainfallMonths < 0);
for(int count = 0; count < rainfallMonths.length; count++)
{
System.out.println("Rainfall Month #" + (count + 1) + ": " + rainfall[count]);
}
return rainfall;
}
Your logic is a little off, not to mention that you're trying to compare an array to an int...
First, the logic...
do
for x = 0 to rainfallMonths.length -1 do
... get input...
while value < 0
The problem here is, you've already assigned the input to all the elements of the array in the for-next loop, but then you are trying to validate the value that was input outside of the for-next which is likely never to return a valid result...and it's too late...
Instead, you want to reverse the logic...
for x = 0 to rainfallMonths.length -1 do
do
value = get input from user
while value < 0
rainfallMonths[x] = value
Next, rainfallMonths is a reference to an array, this isn't actually what you want to be checking against, you need to be checking against one it's values or elements, for example...
while (rainfallMonths[x] < 0);
And if none of that made sense...
public static double[] getRainFall()
{
double[] rainfallMonths = new double[12];
double[] rainfall = new double[12];
for(int x = 0; x < rainfallMonths.length; x++)
{
double input = 0;
System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
do {
rainfallMonths[x] = keyboard.nextDouble();
rainfall[x] = rainfallMonths[x];
if(input < 0)
{
System.out.println("Input is Invalid");
}
} while (rainfallMonths[x] < 0);
}
for(int count = 0; count < rainfallMonths.length; count++)
{
System.out.println("Rainfall Month #" + (count + 1) + ": " + rainfall[count]);
}
return rainfall;
}
You might want to take a refresher on Arrays which should help ;)
double temp = -1;
for(int x = 0; x < rainfallMonths.length; x++)
{
System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
temp = keyboard.nextDouble();
if(temp < 0)
{
System.out.println("Input is Invalid");
x--; //if you want the user to maybe try to repeat this month again?
}
else
{
rainfallMonths[x] = keyboard.nextDouble();
rainfall[x] = rainfallMonths[x];
}
}
I have this program that takes user input and displays the number of times each integer is entered. I pretty much have it down pat but need another loop to omit the shown occurrence of 0. In other words any number with 0 in it cannot be read, also for some reason i am getting two outputs from the same number in my program. For example, if I enter 3,3 I will get 3 occurs 1 time and 3 occurs 2 times as output. The 2 times one being correct and the first one being incorrect.
public class Six_Three {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("enter integers between 1 and 100: ");
int[] num = new int[100];
int data = input.nextInt();
while ((data = input.nextInt()) != 0) {
num[data]++;
}
for (int i = 1; i < 100; ++i) {
if (num[i] > 0)
System.out.println(i + " occurs " + num[i] + " times ");
}
}
You need two separate loops: the first to gather the information, and the second to print the results:
int data = 0;
while ((data = input.nextInt()) != 0)
{
num[data]++;
}
for (int i = 0; i < 100; ++i)
{
if (num[i] != 0) { /* print num[i] */ }
}
Just loop over the num array after your while loop to print the counts.
for (int index = 0; index < num.length; index++) {
if (num[index] != 0)
System.out.println(data + " occurs " + num[data] + " time(s).");
}
You are printing an output every time an integer is read. Your program is behaving as expected.
To get what you want, you need to scan all the input before you produce any output.
Try this instead:
while (data != 0){
data = input.nextInt();
num[data]++;
}
for (int i = 1; i < 100; ++i) { // your version is 0...99, else array index out of bounds
if (num[i] > 0)
System.out.println(i + " occurs " + num[i] + " times ");
}
The way you write it the last number has to be 0 to make the scanning stop. It might be a good idea to check if there's another int available and use that as a condition for the scanning loop. That way your program can accept any integer.
while (input.hasNextInt()){
num[input.nextInt()]++;
}
it's so simple
int data = 0;
int[] num = new int[100];
int i = 0;
while (i < num.length) {
if ((data = input.nextInt()) == 0)
break;
num[i] = data;
i++;
}
for (i = 0; i < 100; ++i) {
int times = 0;
if (num[i] != 0) {
for (int j = 0; j < 100; j++) {
if (num[j] == 0) {
break;
} else if (num[i] == num[j]) {
times++;
}
}
System.out.println(num[i] + " occurs " + times + " times ");
} else {
break;
}
}