Results 1 to 15 of 15

Thread: Parsing

  1. #1
    Matt Sakko is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default Parsing

    Hello.
    Firstly I am Matt and I decided to have a look at Java today.
    I started something very simple.
    Unfortunately I am unable to parse a string into an integer.
    What have I done wrong?
    Any help would be appreciated.


    Java Code:
    //First
       import javax.swing.*;
        public class MainClass {
       
           public static void main(String[] args) {
          
             String withdraw = JOptionPane.showInputDialog ("Please enter the amount you wish to withdraw.");
             Integer.parseInt(withdraw);
             int balance = 850;
    			if (withdraw > balance)
    			{ JOptionPane.showMessageDialog (null, "Your current balance is:"+(balance)+".  The amount you have requested exceeds the amount in your account.");
             }
    			else {withdraw < balance)
    			{
             JOptionPane.showMessageDialog (null, "You have withdrawn "+(withdraw)+" from your account.");
             JOptionPane.showMessageDialog (null, "You currently have" +(balance - withdraw)+" in your account.");
    			}
          }
       }
    Regards, Matt.
    Last edited by Matt Sakko; 03-11-2009 at 09:37 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Integer.parseInt(...) doesn't convert the string into an int, as the String still stays a String. No it returns an int based on the value of the String, and you have to place that returned value into an int variable:

    Java Code:
    int intVariable = Integer.parseInt("203");
    Now you can use this inVariable in your equations that require an int.

  3. #3
    Matt Sakko is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    Okay well thanks for the response but I am not so sure if it works.

    Java Code:
    //First
       import javax.swing.*;
        public class MainClass {
       
           public static void main(String[] args) {
          
             String withdraw = JOptionPane.showInputDialog ("Please enter the amount you wish to withdraw.");
             int intVariable = Integer.parseInt("203");
             int balance = 850;
    			if (withdraw > balance);
    			{ JOptionPane.showMessageDialog (null, "Your current balance is:"+(balance)+".  The amount you have requested exceeds the amount in your account.");
             }
    			else (withdraw < balance);
    			{
             JOptionPane.showMessageDialog (null, "You have withdrawn "+(withdraw)+" from your account.");
             JOptionPane.showMessageDialog (null, "You currently have" +(balance - withdraw)+" in your account.");
    			}
    			}
       }

    It gives me the following errors:


    Java Code:
    MainClass.java:13: 'else' without 'if'
    			else (withdraw < balance);
    			^
    MainClass.java:13: not a statement
    			else (withdraw < balance);
    This makes no sense.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're cutting and pasting my code without thinking. Please look at what I say, and use the code as an example, not as something to be used without thought in your program. I mean does it make any sense for you to parse the String "203" every time? The idea is for you to parse your String, and use the int returned in your if statement.

  5. #5
    Matt Sakko is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    I thought that 203 was part of the syntax.
    When I tried to do it with mine, no success.
    It also says that I have an else statement without an if statement.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Java Code:
    if (withdraw > balance)[B][COLOR="Red"];[/COLOR][/B]
    .
    .
    .
    else (withdraw < balance)[B][COLOR="red"];[/COLOR][/B]
    You don't put semicolons ";" after if and else statements (same for "for" loop).

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Matt Sakko is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks. Okay so basically my code looks like this:


    Java Code:
    //First
       import javax.swing.*;
        public class MainClass {
       
           public static void main(String[] args) {
          
             String withdraw = JOptionPane.showInputDialog ("Please enter the amount you wish to withdraw.");
             int intVariable = Integer.parseInt(withdraw);
             int balance = 850;
    			if (withdraw > balance)
    			{ JOptionPane.showMessageDialog (null, "Your current balance is:"+(balance)+".  The amount you have requested exceeds the amount in your account.");
             }
    			else (withdraw < balance)
    			{
             JOptionPane.showMessageDialog (null, "You have withdrawn "+(withdraw)+" from your account.");
             JOptionPane.showMessageDialog (null, "You currently have" +(balance - withdraw)+" in your account.");
    			}
    			}
       }

    Well now this is the problem:
    MainClass.java:13: not a statement
    else (withdraw < balance)
    ^
    MainClass.java:13: ';' expected
    else (withdraw < balance)
    ^
    2 errors




    I am trying my best to do this myself but...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    withdrawn is a string and you can't compare a String to a number. That's the whole reason that you extract the int value out of the String by parsing it in the first place.

    So you should use the int value that's returned in your inequality statements, not the string.

  9. #9
    Matt Sakko is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    I tried using an int in the first place. How can I have an input Dialog box which is not declared as a string. I thought I would get the number and parse it...

  10. #10
    Matt Sakko is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    Okay sorry to double post and thank to all these people for the help.
    After a bit of time behind the screen, I was able to correct my mistakes and I tried to complicate things.
    My question is can you write: (whateverTheStringIs = AWord)? Like this:


    Java Code:
    //First
       import javax.swing.*;
        public class BalanceClass {
       
           public static void main(String[] args) {
          
             int balance;
             int withdraw;
          
             balance = 850;
             String withdrawmessage = "Please enter the amount you wish to withdraw";
    		   String answer = JOptionPane.showInputDialog ("Please enter the appropriate transaction.  'balance'  or  'withdraw'");
    			if (answer = balance)
    			{ JOptionpane.showMessageDialog ("You currently have"+(balance)+"  USD in your account.");
    			}
    			else
    			{
    
    			 
             withdraw = Integer.parseInt ( JOptionPane.showInputDialog (withdrawmessage));
          
             if (withdraw > balance)
             {JOptionPane.showMessageDialog (null, "The amount you requested exceeds your current balance.  Your current balance: "+(balance)+ " USD.  Please try again");
             }
             else {
             JOptionPane.showMessageDialog (null, "You have withdrawn "+(withdraw)+" USD.  Your current balance is:  "+(balance - withdraw)+" USD");
    			}}

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    When compararing strings you have to use the equals() method:
    Java Code:
    if (myStringVariable.equals("duck"))
      {
        System.out.println ("Quack !!!");
      }
    When comparing ints, doubles, etc, use the comparation operator "==".
    Java Code:
    if (myIntVariable == 5)
      {
        System.out.println ("Jackpot !!!");
      }
    Before I go on you seriously need to get the basics of Java and programming. You can't throw code together and get it to work. For example:
    Java Code:
    if (answer = balance)
    you are trying to compare a string with an int... you have already been shown how to turn a string number into an int.
    I would strongly suggest you start here with the following link:
    The Java™ Tutorials

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    Matt Sakko is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    I cannot send any private messages but I have tried to correct it again and now I have only one error.

    Please help me. I do not see what is wrong. I am struggling to understand the tutorials.

    Java Code:
       import javax.swing.*;
        public class NewAttempt {
       
           public static void main(String[] args) {
          
             int balance;
             int withdraw;
          
             String other;
             String answer;
          	
             balance =  1000;
             answer = JOptionPane.showInputDialog ("Please enter the number linked to the prefered transaction.  1 : balance.  2 : withdraw.");
             
    
             if (answer.equals("1"))
    
             {JOptionPane.showMessageDialog (null, "Your current balance is "+(balance)+" USD");}
             else if (answer.equals("2"))
          	{withdraw = JoptionPane.showInoutDialog  ("Please enter the amount you wish to withdraw");}
    			{if (withdraw > balance)
          	{JOptionPane.showMessage ("The amount you requested exceeds your balance.  Please try again.");}
          	else (withdraw < balance);
          	{JOptionPane.showMessage ("You have withdrawn: "+(withdraw)+" USD.  Your current balance is: "+(balance - withdraw)+" USD");}}
          		
          
           
          }
          }

  13. #13
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    u have some syntax errors

    Java Code:
     [B]JoptionPane.showInoutDialog[/B][/B]
    than ur mistake is that u want to parse String value to Integer

    Java Code:
    withdraw = JoptionPane.showInoutDialog  ("Please enter the amount you wish to withdraw");}
    ("withdraw" is integer,, not string)...

    u use JOptionPane.showMessage, but i think ur intention is JOptionPane.showMessageDialog
    Last edited by tomiu; 03-14-2009 at 12:42 PM.

  14. #14
    Matt Sakko is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks Tomiu.

  15. #15
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    i'm not sure but i think this was the output u wanted to get

    Java Code:
    import javax.swing.*;
    
    public class NewAttempt {
    
           public static void main(String[] args) {
    
             int balance;
             int withdraw=0;
    
             String other;
             String answer;
    
             balance =  1000;
             answer = JOptionPane.showInputDialog ("Please enter the number linked to the prefered transaction.  1 : balance.  2 : withdraw.");
    
    
             if (answer.equals("1"))
    
             {
                 JOptionPane.showMessageDialog (null, "Your current balance is "+(balance)+" USD");
             }
    
            else if (answer.equals("2"))
          	{
                 other = JOptionPane.showInputDialog ("Please enter the amount you wish to withdraw");
    
                 JOptionPane.showMessageDialog (null, "You withdraw "+(other)+" USD");
    			 
    			 withdraw = Integer.parseInt(other);
    
    
             
            
           if (withdraw > balance)
                   {
                    JOptionPane.showMessageDialog (null,"The amount you requested exceeds your balance.  Please try again.");
    
                   }
           else if (withdraw < balance)
          	       {
                    JOptionPane.showMessageDialog (null, "You have withdrawn: "+(withdraw)+" USD.  Your current balance is: "+(balance - withdraw)+" USD");
                    }
               }
           }
    }
    See the line where i convert string to int:

    Java Code:
    withdraw = Integer.parseInt(other);

Similar Threads

  1. Xml Parsing
    By Nomad in forum XML
    Replies: 12
    Last Post: 02-22-2009, 12:19 PM
  2. xml parsing help
    By tankhardrive in forum XML
    Replies: 2
    Last Post: 02-12-2009, 12:45 AM
  3. xml parsing
    By gaurav65176 in forum XML
    Replies: 5
    Last Post: 11-12-2008, 12:07 PM
  4. Using SAXParserFactory for parsing XML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-20-2008, 10:42 AM
  5. Parsing URL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 11:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •