problem with looping
So far I've done something like this
import javax.swing.*;
publicclass arrays
{
publicstaticvoid main (String [] args)
{
String input = JOptionPane.showInputDialog("How many times do u want to enter numbers?");
int times = Integer.parseInt(input);
int [] numbers =newint [times];
for (int i=0; i<times; i++)
{
String input_1=JOptionPane.showInputDialog("Enter number "+(i+1));
numbers [i] = Integer.parseInt(input_1);
}
for (int i=0; i><times; i++)
{
JOptionPane.showMessageDialog(null,"Numbers you have entered is: "+numbers[i]);
}
for (int i=0; i><times;i++)
{
String input_2=JOptionPane.showInputDialog("What do you want to do with the number "+numbers[i]+" \n1.Add 2 \n2.Subtract 2 \n3.Multiply by 2 \n4.Quit the calculation");
int choice=Integer.parseInt(input_2);
switch (choice)
{
case 1:int display_1=numbers[i]+2;JOptionPane.showMessageDialog(null,"The number after adding 2 is :"+display_1);break;
case 2:int display_2=numbers[i]-2;JOptionPane.showMessageDialog(null,"The number after subtracting 2 is :"+display_2);break;
case 3:int display_3=numbers[i]*2;JOptionPane.showMessageDialog(null,"The number after multiplying by 2 is :"+display_3);break;
case 4:break;
default: JOptionPane.showMessageDialog(null,"Invalid entry");break;
}
}
}
}
Problem is each number only does one operation which I don't want. I want for each number will do all these operations unless I choose the 4 option to move to the next number.
Any suggestions?
Message was edited by:
Emotions>
[3293 byte] By [
Emotionsa] at [2007-11-27 6:42:44]

Is there something wrong about this part of your code or is it ok?index ><times>
Jamwaa at 2007-7-12 18:12:57 >

> Is there something wrong about this part of your code
> or is it ok?
>
>
> > index ><times
>
No way man, I dont know why when I paste my codes here, it changed to >< symbol which I couldn't change it to the right one, actually i is smaller than times
cahnge your code slightly to case 4: System.exit(0);break;
Jamwaa at 2007-7-12 18:12:57 >

Sorry but the above post solves another problem, not the one you have. The problem was what is straight the user chooses 4, it couldn't quit. Your prob was
Problem is each number only does one operation which I don't want. I want for each number will do all these operations unless I choose the 4 option to move to the next number
What exactly do you want it to do? How many operations and which ones?
Jamwaa at 2007-7-12 18:12:57 >

you need another loop inside of the for loop where you do your calculations
for (int i = 0; i < times; i++) {
boolean done = false;
while (!done) {
String input_2 = JOptionPane.showInputDialog("What do you want to do with the number " + numbers[i]
+ " \n1.Add 2 \n2.Subtract 2 \n3.Multiply by 2 \n4.Quit the calculation");
int choice = Integer.parseInt(input_2);
switch (choice) {
case 1:
int display_1 = numbers[i] + 2;
JOptionPane.showMessageDialog(null, "The number after adding 2 is :" + display_1);
break;
case 2:
int display_2 = numbers[i] - 2;
JOptionPane.showMessageDialog(null, "The number after subtracting 2 is :" + display_2);
break;
case 3:
int display_3 = numbers[i] * 2;
JOptionPane.showMessageDialog(null, "The number after multiplying by 2 is :" + display_3);
break;
case 4:
done = true;
break;
default:
JOptionPane.showMessageDialog(null, "Invalid entry");
break;
}
}
}
so for each number, you have a boolean flag that indicates when to exit the calculation loop and go on to the next number, it is false to start, and does not get set to true until the user enters a 4 to exit calculation.
~Tim
> you need another loop inside of the for loop where
> you do your calculations
>
> > for (int i = 0; i < times; i++) {
>boolean done = false;
>while (!done) {
> String input_2 = JOptionPane.showInputDialog("What
> at do you want to do with the number " + numbers[i]
> + " \n1.Add 2 \n2.Subtract 2 \n3.Multiply by 2
> y 2 \n4.Quit the calculation");
> int choice = Integer.parseInt(input_2);
>
> switch (choice) {
> case 1:
>int display_1 = numbers[i] + 2;
> JOptionPane.showMessageDialog(null, "The number
> number after adding 2 is :" + display_1);
>break;
> case 2:
>int display_2 = numbers[i] - 2;
> JOptionPane.showMessageDialog(null, "The number
> number after subtracting 2 is :" + display_2);
>break;
> case 3:
>int display_3 = numbers[i] * 2;
> JOptionPane.showMessageDialog(null, "The number
> number after multiplying by 2 is :" + display_3);
>break;
> case 4:
>done = true;
>break;
> default:
> JOptionPane.showMessageDialog(null, "Invalid
> nvalid entry");
>break;
> }
>}
> }
>
>
>
> so for each number, you have a boolean flag that
> indicates when to exit the calculation loop and go on
> to the next number, it is false to start, and does
> not get set to true until the user enters a 4 to exit
> calculation.
>
> ~Tim
It's done. I had considered about another loop inside the for loop, because I am not familiar with boolean so it was hard for me to figure out.
Thx Tim
I think the way you're going about it is wrong. You should declare two arrays, one to hold the numbers you want to use, and one to hold the operations to perfom on those numbers. Then make a for loop that asks them for a number, and then the operation they want to go after it, where when they get to the end, they select equals and it works it out for them.
Personally I think you're over complicating your problem and having the nested loops is actually making your life more difficult when you don't really need them. You said that you wanted to display all of the calculations for each value entered, so that's what I did in my example. I also added the ability to deal with problems. For example, if the user enters something other than a number or if they click cancel instead of ok.
package com.fw.sandbox.loopingexample;
import javax.swing.*;
import java.util.*;
public class LoopingExample
{
public static void main(String[] args)
{
ArrayList inValues = new ArrayList() ;
int collectValues = 0 ;
while(collectValues == 0){
try{
inValues.add(new Double(JOptionPane.showInputDialog(null, "Please enter a number", "Looping Example", JOptionPane.QUESTION_MESSAGE))) ;
}
catch(Exception e){
handleException(e) ;
}
StringBuffer buffer = new StringBuffer("Current values are\n[") ;
buffer.append(getStringOfValues(inValues)).append("]\nContinue collecting values") ;
collectValues = JOptionPane.showOptionDialog(null, buffer.toString(), "Looping Example", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null) ;
}
StringBuffer outBuffer = new StringBuffer("Results\n\n") ;
Iterator iterator = inValues.iterator();
while(iterator.hasNext()){
double dVal = ((Double) iterator.next()).doubleValue() ;
outBuffer.append(dVal).append("+2=").append((dVal+2)).append(" | ") ;
outBuffer.append(dVal).append("-2=").append((dVal-2)).append(" | ") ;
outBuffer.append(dVal).append("*2=").append((dVal*2)).append(" | ") ;
outBuffer.append(dVal).append("/2=").append((dVal/2)).append("\n") ;
}
outBuffer.append("\nThank you for using LoopingExample") ;
JOptionPane.showMessageDialog(null, outBuffer.toString(), "Looping Example", JOptionPane.INFORMATION_MESSAGE) ;
}
private static void handleException(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "Looping Example - Error", JOptionPane.ERROR_MESSAGE) ;
}
private static String getStringOfValues(List inList){
Iterator iterator = inList.iterator() ;
StringBuffer buffer = new StringBuffer() ;
while(iterator.hasNext()){
buffer.append(iterator.next().toString()) ;
buffer.append((iterator.hasNext() ? ", " : "")) ;
}
return buffer.toString() ;
}
}
PS
*Note* I also used an ArrayList instead of an array so I didn't have to ask ahead of time how many they wanted.
I just like Switch Case and I am a bit confused in LOOP so that I just wanted to create a simple program in order to help myself to undestand more about LOOP.That's it.Anyway, thanks all you guys