PDA

View Full Version : Problems with compile errors


shadowls
07-30-2009, 07:04 PM
I'm using jGrasp and new at Java programming.

My code is:

/* CIS 406 – Section 3016 – Java Programming I
Program 3 – Java Loops and Switch Statement
Olivia Riggs
July 27, 09
*/

import javax.swing.*;
import java.text.*;

class Program3 {

public static void main (String[] args) {
Program3 prog = new Program3( );
prog.start();
}

public void start( ) {

int prod;
int quan;
double price;
double lineAmount = (price*quan);
double orderAmount = (lineAmount+orderAmount);


prod = JOptionPane.showInputDialog(null, "Enter Product No.(1-5) or -1 to Quit:");

switch (prod) {

case 1: price = 2.98;
break;

case 2: price = 4.50;
break;

case 3: price = 9.98;
break;

case 4: price = 4.49;
break;

case 5: price = 6.87;
break;
}

while (prod != -1) {

quan = JOptionPane.showInputDialog(null, "Enter Quantity (1-5) or -1 to Quit:");

}

if (prod == -1) {

JOptionPane.showMessageDialog("Total", "Your total is" + orderAmount);

}

while (quan != -1) {

DecimalFormat df = new DecimalFormat("0.00");
JOptionPane.showMessageDialog("Order Subtotal Amount", "Product No" + "Quantity" + "Line Amount" + "Order Amount" + "\n\n"
+ prod + " " + quan + " " + df.format(lineAmount) + " " + df.format(orderAmount) );

}


if (quan == -1) {

JOptionPane.showMessageDialog("Total", "Your total is" + orderAmount);

}

prod = JOptionPane.showInputDialog(null,"Enter Product No.(1-5) or -1 to Quit:");

{

System.exit( 0 );

}
}
}


These are the errors I'm getting when I compile the code:


----jGRASP exec: javac -g C:\Users\Shadow\Desktop\CIS406\Program3.java

Program3.java:26: incompatible types
found : java.lang.String
required: int
prod = JOptionPane.showInputDialog(null, "Enter Product No.(1-5) or -1 to Quit:");
^
Program3.java:48: incompatible types
found : java.lang.String
required: int
quan = JOptionPane.showInputDialog(null, "Enter Quantity (1-5) or -1 to Quit:");
^
Program3.java:54: cannot find symbol
symbol : method showMessageDialog(java.lang.String,java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("Total", "Your total is" + orderAmount);
^
Program3.java:61: cannot find symbol
symbol : method showMessageDialog(java.lang.String,java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("Order Subtotal Amount", "Product No" + "Quantity" + "Line Amount" + "Order Amount" + "\n\n"
^
Program3.java:69: cannot find symbol
symbol : method showMessageDialog(java.lang.String,java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("Total", "Your total is" + orderAmount);
^
Program3.java:73: incompatible types
found : java.lang.String
required: int
prod = JOptionPane.showInputDialog(null,"Enter Product No.(1-5) or -1 to Quit:");
^
6 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


I know these may be simple errors, but I have no idea how to fix them. Any help or information on what I'm doing wrong would be wonderful. Thanks in advance.

ckeyrouz
07-30-2009, 07:30 PM
this is the corrected code for compilation, did not test it if it works:
import javax.swing.*;
import java.text.*;

class Program3 {

public static void main (String[] args) {
Program3 prog = new Program3( );
prog.start();
}

public void start( ) {

int prod = 0;
int quan = 0;
double price = 0;
double lineAmount = (price*quan);
double orderAmount = 0;
orderAmount = (lineAmount+orderAmount);


prod = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter Product No.(1-5) or -1 to Quit:")).intValue();

switch (Integer.valueOf(prod).intValue()) {

case 1: price = 2.98;
break;

case 2: price = 4.50;
break;

case 3: price = 9.98;
break;

case 4: price = 4.49;
break;

case 5: price = 6.87;
break;
}

while (prod != -1) {

quan = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter Quantity (1-5) or -1 to Quit:","Total")).intValue();

}

if (prod == -1) {

JOptionPane.showMessageDialog(null, "Your total is" + orderAmount,"Total",JOptionPane.INFORMATION_MESSAGE);

}

while (quan != -1) {

DecimalFormat df = new DecimalFormat("0.00");
JOptionPane.showMessageDialog(null, "Product No" + "Quantity" + "Line Amount" + "Order Amount" + "\n\n"
+ prod + " " + quan + " " + df.format(lineAmount) + " " + df.format(orderAmount) ,"Order Subtotal Amount",JOptionPane.INFORMATION_MESSAGE);

}


if (quan == -1) {

JOptionPane.showMessageDialog(null, "Your total is" + orderAmount,"Total",JOptionPane.INFORMATION_MESSAGE);

}

prod = Integer.valueOf(JOptionPane.showInputDialog(null,"Enter Product No.(1-5) or -1 to Quit:")).intValue();

{

System.exit( 0 );

}
}
}


many many problems:
JOptionPane.showMessageDialog returns String not int.
you used it with String String while it takes Component, Object, String, int

calculation at the start of the method without variables being initialized.

shadowls
07-30-2009, 08:23 PM
Thank you, the program now compiles, but it is not work the way it should. It is now getting stuck in a loop.

I changed the code a bit:

import javax.swing.*;
import java.text.*;

class Program3 {

public static void main (String[] args) {
Program3 prog = new Program3( );
prog.start();
}

public void start( ) {

int prod = 0;
int quan = 0;
double price = 0;
double lineAmount = (price*quan);
double orderAmount = 0;
orderAmount = (lineAmount+orderAmount);


prod = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter Product No.(1-5) or -1 to Quit:")).intValue();

switch (Integer.valueOf(prod).intValue()) {

case 1: price = 2.98;
break;

case 2: price = 4.50;
break;

case 3: price = 9.98;
break;

case 4: price = 4.49;
break;

case 5: price = 6.87;
break;
}

while (prod != -1) {

quan = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter Quantity (1-5) or -1 to Quit:")).intValue();

}

if (prod == -1) {

JOptionPane.showMessageDialog(null, "Your total is" + orderAmount,"Total",JOptionPane.INFORMATION_MESSAGE);
System.exit( 0 );
}

else

while (quan != -1) {

DecimalFormat df = new DecimalFormat("0.00");
JOptionPane.showMessageDialog(null, "Product No" + "Quantity" + "Line Amount" + "Order Amount" + "\n\n"
+ prod + " " + quan + " " + df.format(lineAmount) + " " + df.format(orderAmount) ,"Order Subtotal Amount",JOptionPane.INFORMATION_MESSAGE);

}


if (quan == -1) {

JOptionPane.showMessageDialog(null, "Your total is" + orderAmount,"Total",JOptionPane.INFORMATION_MESSAGE);
System.exit( 0 );
}

else {


prod = Integer.valueOf(JOptionPane.showInputDialog(null,"Enter Product No.(1-5) or -1 to Quit:")).intValue();


}

}
}

The program gets stuck after the "Enter Quantity (1-5) or -1 to Quit:" window, when you enter a number it just reloads the "Enter Quantity (1-5) or -1 to Quit:" window. You can use the cancel or x button to get out of the program. How do I fix this?

Thanks again.

ckeyrouz
07-30-2009, 08:48 PM
import javax.swing.*;
import java.text.*;

class Program3 {

public static void main (String[] args) {
Program3 prog = new Program3( );
prog.start();
}

public void start( ) {

int prod = 0;
int quan = 0;
double price = 0;
double lineAmount = 0;
double orderAmount = 0;


prod = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter Product No.(1-5) -1 to Quit:")).intValue();

switch (prod) {

case 1: price = 2.98;
break;

case 2: price = 4.50;
break;

case 3: price = 9.98;
break;

case 4: price = 4.49;
break;

case 5: price = 6.87;
break;
}

while (prod != -1)
{



quan = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter Quantity (1-5) or -1 to Continue:")).intValue();



// if (prod == 0) {
//
// JOptionPane.showMessageDialog(null, "Your total is" + orderAmount,"Total",JOptionPane.INFORMATION_MESSAGE);
//
// }

if (quan != -1)
{
lineAmount = price*quan;
orderAmount += lineAmount;
DecimalFormat df = new DecimalFormat("0.00");
JOptionPane.showMessageDialog(null, "Product No" + "Quantity" + "Line Amount" + "Order Amount" + "\n\n"
+ prod + " " + quan + " " + df.format(lineAmount) + " " + df.format(orderAmount) ,"Order Subtotal Amount",JOptionPane.INFORMATION_MESSAGE);

}


if (quan == -1)
{
JOptionPane.showMessageDialog(null, "Your total is\t" + orderAmount,"Total",JOptionPane.INFORMATION_MESSAGE);

}

prod = Integer.valueOf(JOptionPane.showInputDialog(null,"Enter Product No.(1-5) -1 to Quit:")).intValue();
// orderAmount = 0;
// lineAmount = 0;
}
{

System.exit( 0 );

}
}
}

shadowls
07-31-2009, 12:47 AM
Thanks.

ckeyrouz
07-31-2009, 04:43 AM
If my post really helped you and worked for you, please click on the button "Thank User for this helpful post", am trying to increase my credits for this beautiful and helpful forum, but if you don't feel so, don't do it.

Thanks in advance.

shadowls
07-31-2009, 03:06 PM
No problem. I only have one last question. How do I get JOptionPane.showMessageDialog(null, "Your total is " + orderAmount,"Total",JOptionPane.INFORMATION_MESSAGE); to show up after -1 is entered? Do I need more { }'s or do I need else statements?

I figured it out and I feel blond for not seeing it. Thank you so much.