Results 1 to 5 of 5
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default How to use if/else with parseint

    Hi,
    A small problem that I have tried a few alternatives to solve.

    Here is the code:
    Java Code:
    public class testIfElse {
    	public static void main(String[] argv) {
    		
    		String cstring;
    		cstring = "123456";
    		if (cstring != "")
    		{
    			int credit = Integer.parseInt(cstring);
    		}else{
    			int credit = 0;
    		}
    		System.out.println(credit);
    	}
    }
    If the string is not blank, print out the result as a Int.
    If the string is blank, print a zero.
    How can I do this?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to use if/else with parseint

    Don't use == (or !=) with String. Use the equals() method instead. This has been discussed in great detail on these forums and on the internet, so I recommend doing a search of something like "using == with Objects".
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: How to use if/else with parseint

    I have tried this also:
    Java Code:
    public class testIfElse {
    	public static void main(String[] argv) {
    		String cstring = "123456";
    		if (!"".equals(cstring))
    		{
    			int credit = Integer.parseInt(cstring);
    		}else{
    			int credit = 0;
    		}
    		System.out.println(credit);
    	}
    }
    The error is this: "credit cannot be resolved to a variable"

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to use if/else with parseint

    ... also don't define local variables 'credit' in both the if-part as well as the else-part; they are different variables and above all, they'll be gone after that if-else statement has finishe executing; the remedy is simple: define it before your if-else statement.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: How to use if/else with parseint

    Sorry for that really stupid question. It was so obvious!!

    Here is the correct code:
    Java Code:
    public class testIfElse {
    	public static void main(String[] argv) {
    		String cstring = "123456";
    		int credit;
    		if (!"".equals(cstring))
    		{
    			credit = Integer.parseInt(cstring);
    		}else{
    			credit = 0;
    		}
    		System.out.println(credit);
    	}
    }

Similar Threads

  1. error w/ parseInt
    By katiebear128 in forum New To Java
    Replies: 2
    Last Post: 11-02-2011, 02:26 AM
  2. parseInt and getText
    By esallender in forum New To Java
    Replies: 2
    Last Post: 01-14-2011, 03:16 PM
  3. parseInt
    By trefoil in forum New To Java
    Replies: 4
    Last Post: 09-09-2009, 07:12 PM
  4. parseInt() vs. intValue()
    By JavaPilot in forum New To Java
    Replies: 5
    Last Post: 02-04-2009, 08:39 AM
  5. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:31 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
  •