Java Int help

Up all nigh trying to figure this out

Page 1 of 1

2 Replies - 550 Views - Last Post: 02 February 2010 - 07:02 AM Rate Topic: -----

#1 emitremmit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-December 09

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.

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);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Int help

#2 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

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.

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

Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

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]);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1