Jump to content

Java Help


ilikemoneygreen

Recommended Posts

What am I doing wrong? i cant seem to get the second dialog box to popup to enter the value. I could get it to work just in the command prompt using System.out.print but its not working out with the dialog box.

Code:

/**Area of rectangle *//* ilikemoneygreen
* CIS163 29843
* August 24, 2012
*/
import java.util.Scanner; // scanner package
import javax.swing.JOptionPane;
public class RectangleArea {
public static void main(String[] args) {
//input the length and width
Scanner input = new Scanner(System.in);


//prompt user for length
JOptionPane.showInputDialog(null, "Enter the length : " , "Length" , JOptionPane.INFORMATION_MESSAGE);
double length = input.nextDouble();
//Prompt user for Width
JOptionPane.showInputDialog(null, "Enter the width : " , "Width" , JOptionPane.INFORMATION_MESSAGE);
double width = input.nextDouble();


//calculate area
double area = length * width;
//Display the results
System.out.println("The area of a Rectangle with a width of " + width + " and a length of " + length + " is " + area);
}
}

If you help me, ill give you a cyber hug.

Link to comment
Share on other sites

This is clearly an assignment/lab work so I cannot give you the answer but I can give you a pointer. Think carefully about the inputs your program is requesting, it is asking for 4 different inputs in this order:

  1. Dialog box
  2. Command prompt
  3. Dialog box
  4. Command prompt

Use your program as such.

Link to comment
Share on other sites

I surpassed my actual homework, that was just helloworld, which i aced i think. Im working a chapter ahead because it seems pretty fun. im creating extra programs for me to do. The book showed the area for a circle but only using input from a command prompt. i thought it would be cool to have the dialog box to enter in so im attempting the impossible. I think my problem is the book mentioned the JOptionPane in a sidenote, but im not sure how it all works. Im thinking with your hint but im not sure what you mean about command prompt. i got the 1 and 3. and i know i have to enter the inputs which i also declare a variable at the same time. i guess im getting lost on the command prompt part? should i be declaring my variable earlier?

//Dialog box for length

JOptionPane.showInputDialog(null, "Enter the length : " , "Length" , JOptionPane.INFORMATION_MESSAGE);

double length = input.nextDouble();

//Dialog box for width

JOptionPane.showInputDialog(null, "Enter the width : " , "Width" , JOptionPane.INFORMATION_MESSAGE);

double length = input.nextDouble();

Link to comment
Share on other sites


[COLOR=#3E3E3E][I]//Dialog box for length[/I][/COLOR]
[COLOR=#3E3E3E][I]JOptionPane.showInputDialog(null, "Enter the length : " , "Length" , JOptionPane.INFORMATION_MESSAGE);
[/I][/COLOR][COLOR=#008000]//Command prompt input for length[/COLOR]
[COLOR=#3E3E3E][I]double length = input.nextDouble();[/I][/COLOR]
[COLOR=#3E3E3E][I]//Dialog box for width[/I][/COLOR]
[COLOR=#3E3E3E][I]JOptionPane.showInputDialog(null, "Enter the width : " , "Width" , JOptionPane.INFORMATION_MESSAGE);
[/I][/COLOR][COLOR=#008000]//Command prompt input for width[/COLOR]
[COLOR=#3E3E3E][I]double width = input.nextDouble();
[/I][/COLOR]

Your code still includes the commands required for input from the command prompt. Therefore after you use your first dialog box, your program then waits for you to input a value using the command prompt.

Link to comment
Share on other sites


public static void main(String[] args) {
//prompt user for length
String inputValue = JOptionPane.showInputDialog(null, "Enter the length : " , "Length" , JOptionPane.INFORMATION_MESSAGE);
double length = Double.parseDouble(inputValue);
//Prompt user for Width
inputValue = JOptionPane.showInputDialog(null, "Enter the width : " , "Width" , JOptionPane.INFORMATION_MESSAGE);
double width = Double.parseDouble(inputValue);

//calculate area
double area = length * width;
//Display the results
System.out.println("The area of a Rectangle with a width of " + width + " and a length of " + length + " is " + area);
}

Link to comment
Share on other sites

the code for entering the command prompt is

 import java.util.Scanner; // scanner package
public class area3 {
public static void main(String[] args) {
//Scanner object creation
Scanner input = new Scanner(System.in);
//prompt user for legth
System.out.print("Enter the lenth : ");
double length = input.nextDouble();
//prompt user for width
System.out.print ("Enter the Width : ");
double width = input.nextDouble();

//calculate area
double area = length * width;
//Display the results
System.out.println("The area of a Rectangle with a width of " + width + " and a length of " + length + " is " + area);
}
}

i think ive got that worked out since it runs nicely.

but then i thought that the system.out.print forcing it to go through the command prompt. So with it doing JOptionPane should it run just in the dialog box? The first dialog box has an input area so it appeared to be working. but you are SO right, its waiting for the for me to enter the numbers in the command prompt. my problem lies within getting the JOptionPane to accept the input and deleting the inputs for the command prompt. Thank You. now i just gotta do it.

Link to comment
Share on other sites


public static void main(String[] args) {
//prompt user for length
String inputValue = JOptionPane.showInputDialog(null, "Enter the length : " , "Length" , JOptionPane.INFORMATION_MESSAGE);
double length = Double.parseDouble(inputValue);
//Prompt user for Width
inputValue = JOptionPane.showInputDialog(null, "Enter the width : " , "Width" , JOptionPane.INFORMATION_MESSAGE);
double width = Double.parseDouble(inputValue);

//calculate area
double area = length * width;
//Display the results
System.out.println("The area of a Rectangle with a width of " + width + " and a length of " + length + " is " + area);
}

That works !

String inputValue = and Double.parseDouble(inputValue); thats so cool! Thanks. You both get Cyber hugs! [ATTACH=CONFIG]31837[/ATTACH]

Link to comment
Share on other sites

So with it doing JOptionPane should it run just in the dialog box? The my problem lies within getting the JOptionPane to accept the input and deleting the inputs for the command prompt. Thank You. now i just gotta do it.

Exactly, I am glad you understand what was wrong with your first attempt. Happy coding!

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...