# problems

Ian Dudek

Greenhorn

Posts: 9

posted 9 years ago

I have been working on this project for a bit now and I had a few questions because I am stumped.

I have it set up as a loop. Every time you would not select a one of the proper numbers it would error out. So what I did was create an error box to appear when you don't select the right one (right below). It won't pop up until after you selected and entered in a number then it it will pop up every time you select something. I think it has something to do with my placing of the code. One of my friends was saying it should break before it loops but if it breaks then the loop stops. Is there a way to have it break then start back in the loop?

"if(choice!=1||choice!=2||choice!=3||choice!=4||choice!=5)

JOptionPane.showMessageDialog(null,"Wrong option entered", " error",

JOptionPane.ERROR_MESSAGE);"

Next every time you choose to select a number it will error out. I have no clue how to fix this problem.

// This program is a loop that will calculate the area of shapes

// Written by Ian Dudek

import javax.swing.JOptionPane;

public class app1{

public static void main(String args[])

{

String first,second;

double choice ;

double radius,width,area, length;

//intialize the string

String value=" ";

value =JOptionPane.showInputDialog("Please chose one of the options:"+"\n" +

"a)Enter 1 to calculate the area of the Circle"+ "\n"+

"b)Enter 2 to calculate the area of the Triangle"+ "\n"+

"c)Enter 3 to calculate the area of the Square"+ "\n"+

"d)Enter 4 to calculate the area of the Rectangle"+"\n"+

"e)Enter 5 to calculate the area of the Cube"+"\n"+

"f)Enter 6 to exit the program");

choice=Double.parseDouble(value);

// while option is not 6 continue

while(choice!=6){

//if selected number calculate the area of circle

if(choice==1){

first = JOptionPane.showInputDialog("Enter the value of radius");

radius = Double.parseDouble(first);

area = Math.PI*radius*radius;

//print out the result

JOptionPane.showMessageDialog(null,"The area of the Circle: "+area,"result",

JOptionPane.INFORMATION_MESSAGE);

}

//If selected number is 2 calculate the area of triangle and prints result

else if(choice==2){

first=JOptionPane.showInputDialog("Enter the value of lenght");

second=JOptionPane.showInputDialog("Enter the value of width");

length = Double.parseDouble(first);

width=Double.parseDouble(second);

area= (width*length)/2;

JOptionPane.showMessageDialog(null,"The area of triangle: "+ area,"result",

JOptionPane.INFORMATION_MESSAGE);

}

//If selected number is 3 calculate the area of square and prints result

else if(choice==3){

first = JOptionPane.showInputDialog("Enter the value of length");

length = Double.parseDouble(first);//ge string into integer

area=length*length;

JOptionPane.showMessageDialog(null,"The area of the square: "+ area," result",

JOptionPane.INFORMATION_MESSAGE);

}

//If selected number is 4 calculate the area of rectangle and prints result

else if(choice==4){

first=JOptionPane.showInputDialog("Enter the value of length");

second=JOptionPane.showInputDialog("Enter the value of width");

length=Double.parseDouble(first);

width=Double.parseDouble(second);

area=width*length;

JOptionPane.showMessageDialog(null,"The area of the rectangle: "+ area,"result",

JOptionPane.INFORMATION_MESSAGE);

}

//If selected number is 5 calculat the area of cube and prints result

else if(choice==5) {

first=JOptionPane.showInputDialog("Enter the value of length");

length=Double.parseDouble(first);

area=6*length;

JOptionPane.showMessageDialog(null,"The area of the cube: "+ area,"result",

JOptionPane.INFORMATION_MESSAGE);

}

value =JOptionPane.showInputDialog("Please chose one of the options:"+"\n" +

"a)Enter 1 to calculate the area of the Circle"+ "\n"+

"b)Enter 2 to calculate the area of the Triangle"+ "\n"+

"c)Enter 3 to calculate the area of the Square"+ "\n"+

"d)Enter 4 to calculate the area of the Rectangle"+"\n"+

"e)Enter 5 to calculate the area of the Cube"+"\n"+

"f)Enter 6 to exit the program");

choice=Double.parseDouble(value);

// If choice is not 1-5 a error message will pop up with an error

if(choice!=1||choice!=2||choice!=3||choice!=4||choice!=5)

JOptionPane.showMessageDialog(null,"Wrong option entered", " error",

JOptionPane.ERROR_MESSAGE);

//end of while loop if number selected is 6

}

System.out.println("Program terminated\n");

System.exit(0);

}

}

I have it set up as a loop. Every time you would not select a one of the proper numbers it would error out. So what I did was create an error box to appear when you don't select the right one (right below). It won't pop up until after you selected and entered in a number then it it will pop up every time you select something. I think it has something to do with my placing of the code. One of my friends was saying it should break before it loops but if it breaks then the loop stops. Is there a way to have it break then start back in the loop?

"if(choice!=1||choice!=2||choice!=3||choice!=4||choice!=5)

JOptionPane.showMessageDialog(null,"Wrong option entered", " error",

JOptionPane.ERROR_MESSAGE);"

Next every time you choose to select a number it will error out. I have no clue how to fix this problem.

// This program is a loop that will calculate the area of shapes

// Written by Ian Dudek

import javax.swing.JOptionPane;

public class app1{

public static void main(String args[])

{

String first,second;

double choice ;

double radius,width,area, length;

//intialize the string

String value=" ";

value =JOptionPane.showInputDialog("Please chose one of the options:"+"\n" +

"a)Enter 1 to calculate the area of the Circle"+ "\n"+

"b)Enter 2 to calculate the area of the Triangle"+ "\n"+

"c)Enter 3 to calculate the area of the Square"+ "\n"+

"d)Enter 4 to calculate the area of the Rectangle"+"\n"+

"e)Enter 5 to calculate the area of the Cube"+"\n"+

"f)Enter 6 to exit the program");

choice=Double.parseDouble(value);

// while option is not 6 continue

while(choice!=6){

//if selected number calculate the area of circle

if(choice==1){

first = JOptionPane.showInputDialog("Enter the value of radius");

radius = Double.parseDouble(first);

area = Math.PI*radius*radius;

//print out the result

JOptionPane.showMessageDialog(null,"The area of the Circle: "+area,"result",

JOptionPane.INFORMATION_MESSAGE);

}

//If selected number is 2 calculate the area of triangle and prints result

else if(choice==2){

first=JOptionPane.showInputDialog("Enter the value of lenght");

second=JOptionPane.showInputDialog("Enter the value of width");

length = Double.parseDouble(first);

width=Double.parseDouble(second);

area= (width*length)/2;

JOptionPane.showMessageDialog(null,"The area of triangle: "+ area,"result",

JOptionPane.INFORMATION_MESSAGE);

}

//If selected number is 3 calculate the area of square and prints result

else if(choice==3){

first = JOptionPane.showInputDialog("Enter the value of length");

length = Double.parseDouble(first);//ge string into integer

area=length*length;

JOptionPane.showMessageDialog(null,"The area of the square: "+ area," result",

JOptionPane.INFORMATION_MESSAGE);

}

//If selected number is 4 calculate the area of rectangle and prints result

else if(choice==4){

first=JOptionPane.showInputDialog("Enter the value of length");

second=JOptionPane.showInputDialog("Enter the value of width");

length=Double.parseDouble(first);

width=Double.parseDouble(second);

area=width*length;

JOptionPane.showMessageDialog(null,"The area of the rectangle: "+ area,"result",

JOptionPane.INFORMATION_MESSAGE);

}

//If selected number is 5 calculat the area of cube and prints result

else if(choice==5) {

first=JOptionPane.showInputDialog("Enter the value of length");

length=Double.parseDouble(first);

area=6*length;

JOptionPane.showMessageDialog(null,"The area of the cube: "+ area,"result",

JOptionPane.INFORMATION_MESSAGE);

}

value =JOptionPane.showInputDialog("Please chose one of the options:"+"\n" +

"a)Enter 1 to calculate the area of the Circle"+ "\n"+

"b)Enter 2 to calculate the area of the Triangle"+ "\n"+

"c)Enter 3 to calculate the area of the Square"+ "\n"+

"d)Enter 4 to calculate the area of the Rectangle"+"\n"+

"e)Enter 5 to calculate the area of the Cube"+"\n"+

"f)Enter 6 to exit the program");

choice=Double.parseDouble(value);

// If choice is not 1-5 a error message will pop up with an error

if(choice!=1||choice!=2||choice!=3||choice!=4||choice!=5)

JOptionPane.showMessageDialog(null,"Wrong option entered", " error",

JOptionPane.ERROR_MESSAGE);

//end of while loop if number selected is 6

}

System.out.println("Program terminated\n");

System.exit(0);

}

}

posted 9 years ago

You should use "&&" instead of "||".

The logic is not clean either. You'd better make it more readable, like:

1. use do/while

2. read for a number

3. if 6 break

4. check number

5. do something according to the value

The logic is not clean either. You'd better make it more readable, like:

1. use do/while

2. read for a number

3. if 6 break

4. check number

5. do something according to the value

[My Blog]
*All roads lead to JavaRanch*

Campbell Ritchie

Sheriff

Posts: 48910

58

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 9 years ago

Was Satou's correction to use && instead of || clear? It's a fundamental bit of boolean algebra, one of DeMorgan's Laws. My college didn't have a CS department, so I got this in a Philosophy class on logic:

The opposite of (A or B) is (!A and !B)

The opposite of (A and B) is (!A or !B)

or

not (P and Q) = (not P) or (not Q)

not (P or Q) = (not P) and (not Q)

In your program, a good result is (choice==1 or choice==2). We're testing for a bad result, which is the opposite of the good result, which is (choice!=1 and choice!=2).

The opposite of (A or B) is (!A and !B)

The opposite of (A and B) is (!A or !B)

or

not (P and Q) = (not P) or (not Q)

not (P or Q) = (not P) and (not Q)

In your program, a good result is (choice==1 or choice==2). We're testing for a bad result, which is the opposite of the good result, which is (choice!=1 and choice!=2).

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi