SRL Archive - Java Help and Tutorials

Help with Java Program


cal516

2015-03-17 14:43:27

All, I am new to this forum and I am hoping I came to the right spot…. I am having trouble with this simple program. What I am attempting to do is simply work with dialog boxes and user inputs. My end state for this current segment is to simply have this output happen when I run the program:

Dialog Box: Enter the customer's package (A, B, or C);

User input: A

Dialog Box: Enter the hours used:

Any overages in hours need to be added as additional charges. For example Package A has a maximum of 10 hours access. Additional hours are $2.00 per hour. So if a user input 11 hours for Package his total would be $11.95. It needs to be adjustable of course with user input for hours. I am not sure how to do this? Any input would be greatly appreciated.

Dialog Box/Message: The charges are "total with hours input."


Thank you in advance




import java.text.DecimalFormat;
import javax.swing.JOptionPane;


public class ColapietroPass4Draft
{

public static void main(String[] args)

{

char packageSelect;
final double Package_A = 9.95,
Package_B = 13.95,
Package_C = 19.95,
Package_A_Hours = 10,
Package_B_Hours = 20;

double hoursSelect;


String input = JOptionPane.showInputDialog("Enter customer's package (A,B or C):");
System.out.println();
packageSelect = input.charAt(0);


String hours = JOptionPane.showInputDialog("Enter the number of hours used):");


if (packageSelect == 'A')
{
JOptionPane.showMessageDialog(null, "The charges are " + Package_A);
}

if (packageSelect == 'B')
{
JOptionPane.showMessageDialog(null, "The charges are " + Package_B);
}

if (packageSelect == 'C')
{
JOptionPane.showMessageDialog(null, "The charges are " + Package_C);
}


System.exit(0);


}

}


Brandon

2015-03-17 15:08:24

Problem is you're mixing Swing with Scanner. Use one or the other but not both.



public class Hmk {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String res = JOptionPane.showInputDialog("Enter Package:");
if (res != null && !res.trim().isEmpty()) {
if (res.equals("A")) {
JOptionPane.showMessageDialog(null, "9.95", "Price: ", JOptionPane.INFORMATION_MESSAGE);
}
} else {
//invalid input.. user entered nothing.. do something here..
}
}
}


Also, to highlight code on the forum, use the Highlight tags:


[NoParse]
[Highlight=Java]
Put your java code here..
[/Highlight]
[/NoParse]

EyYo96

2015-03-17 15:24:30

Hey, a Scanner is for the console input. The joptionPane is a standard gui. Both are used to get a user input. It's up to you now which one you want to use. Please use the code tags next time. For java:
 and 
without spaces

import javax.swing.JOptionPane;

public class MonthlyDraft {

public static void main(String[] args) {

char packageSelect;
final double Package_A = 9.95;

String input = JOptionPane
.showInputDialog("Enter customer's package (A, B or C):");

System.out.println("You entered: " + input); // to debug the input
packageSelect = input.charAt(0);
if (packageSelect == 'A') {
JOptionPane.showMessageDialog(null, "The charges are " + Package_A);
}
System.exit(0);

}

}

cal516

2015-03-17 20:54:01

Great help!!! Thank you…I was able to work it out…..