2 Replies - 434 Views - Last Post: 10 November 2011 - 02:28 PM Rate Topic: -----

#1 shannon_p   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-November 11

I have a question and I am really confused

Posted 10 November 2011 - 02:21 PM

shopping [] shop = new shopping[100];
        PrintWriter ofile = null;
        String pname;
        double pprice;
        int pbarcode;
        int pquantity;
        int cmd;
        boolean done;
        String resp = JOptionPane.showInputDialog( "Choose from:" +
                "\n1: Add new products\n2: Shop\n3: Check out ");
        cmd = Integer.parseInt(resp);
        switch(cmd){
        case 1: done = false;
                int i = 0;
                while(!done){
                    pname = JOptionPane.showInputDialog("Enter the product name");
                    pquantity = JOptionPane.showInputDialog("Enter the quantity of that product");
                    shop p = new shop(pname);



So I am trying to write a program that allows you to go shopping. I am having a problem where I have pquantity = JOptionPane... It keeps telling me to use and int not a string but I have pquantity as an int. HELP?!

This post has been edited by smohd: 10 November 2011 - 02:27 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: I have a question and I am really confused

#2 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: I have a question and I am really confused

Posted 10 November 2011 - 02:26 PM

Ok so pquantity is an int, which is right. However, JOptionPane.... returns a string.

Use Integer.parseInt(JOptionPane.showInput...)

edit: also please put your code between code tags in the future. I'd edit it for you, but I'm powerless in the Java forum.

This post has been edited by xclite: 10 November 2011 - 02:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: I have a question and I am really confused

Posted 10 November 2011 - 02:28 PM

Here pquantity = JOptionPane.showInputDialog("Enter the quantity of that product");, JOptionPane.showInputDialog() returns a String. The pquantity variable is an int. So you have to parse the String to an int before assigning it to pquantity.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1