import javax.swing.*;
public class Main{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String title; // Value entered by user; string type
String costStr; // Value entered by user; string type
String category;
int totalcost = 0;
double cost; // The double form of the user-entered book cost
int resp = 0; // The user's response as to whether to continue
int count =0; //count variable
// Initialize the output string to the empty string
String outputStr = "";
// Loop to input information on the user's book collection
while (true) {
// Read in book title from user as a string
title = JOptionPane.showInputDialog("Enter book title");
count++;
// Read in cost from user as a string
costStr = JOptionPane.showInputDialog("Enter book cost");
// Convert from type String to type double
cost = Double.parseDouble(costStr);
totalcost =
category = JOptionPane.showInputDialog("Enter Category number: \n" +
"1= Arts \n" +
"2= Business and Finance,\n " +
"3= Engineering, \n " +
"4= Fiction \n" +
"5= Health \n" +
"6= Mathematics \n" +
"7= Natural Science \n" +
"8= Social Science \n" +
"9= Techbnology 10= Other");
int category = Integer.parseInt(category);
// Add the user-entered values to the output string
outputStr = outputStr + count + ", ";
outputStr = outputStr + "Book title: " + title + ", ";
outputStr = outputStr + "Cost: " + cost + ", ";
outputStr = outputStr + "Category: " + category + ", ";
outputStr = outputStr + "Total Cost: " + totalcost + "\n";
//Category of book
// Get the user's choice as to whether to continue the loop
resp = JOptionPane.showConfirmDialog(null, outputStr + "\nContinue?", "Confirm",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
// Test whether the user's choice is to continue the loop or not
if (resp == JOptionPane.NO_OPTION) {
break;
}
}
// Display final output to the user\
JOptionPane.showMessageDialog(null, outputStr, "MY WINDOW OUTPUT",
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(
null, outputStr, "Final Results", JOptionPane.INFORMATION_MESSAGE);
}
}
Java Int helpUp all nigh trying to figure this out
Page 1 of 1
2 Replies - 550 Views - Last Post: 02 February 2010 - 07:02 AM
#1
Java Int help
Posted 02 February 2010 - 05:21 AM
I need to redefine "category as an integer so that i will be able to type in the category number like 1, 2, 3 etc in the input box and it returns the category description. thank you for any help you can give me.
Replies To: Java Int help
#2
Re: Java Int help
Posted 02 February 2010 - 05:52 AM
Well you could always use a static Array of some kind, that way you can index your catergory string representations
eg.
Then when you do
you can simply use something like
Hope this was helpful
eg.
int category_selected;
static final String[] categories = {
"Arts",
"Engineering",
"Fiction",
"Health",
"Mathematics",
"Natural Science",
"Social Science",
"Technology",
"Other"
};
Then when you do
int category_selected = Integer.parseInt(category);
you can simply use something like
outputStr = outputStr + "Category: " + categories[ category_selected - 1 ] + ", ";
Hope this was helpful
This post has been edited by bbq: 02 February 2010 - 05:55 AM
#3
Re: Java Int help
Posted 02 February 2010 - 07:02 AM
You don't even need to index the array (called 'SUBJECTS' below) - just get JOptionPane to render it as a combo and then get the user's choice.
JOptionPane.showInputDialog( this, "Choose subject", null, JOptionPane.PLAIN_MESSAGE, null, SUBJECTS, SUBJECTS[0]);
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|