Results 1 to 5 of 5
Thread: How to use if/else with parseint
- 06-20-2012, 05:36 PM #1
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
How to use if/else with parseint
Hi,
A small problem that I have tried a few alternatives to solve.
Here is the code:
If the string is not blank, print out the result as a Int.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 blank, print a zero.
How can I do this?
- 06-20-2012, 05:42 PM #2
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!
- 06-20-2012, 05:50 PM #3
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: How to use if/else with parseint
I have tried this also:
The error is this: "credit cannot be resolved to a variable"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); } }
- 06-20-2012, 05:50 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-20-2012, 06:43 PM #5
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: How to use if/else with parseint
Sorry for that really stupid question. It was so obvious!!
.gif)
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
-
error w/ parseInt
By katiebear128 in forum New To JavaReplies: 2Last Post: 11-02-2011, 02:26 AM -
parseInt and getText
By esallender in forum New To JavaReplies: 2Last Post: 01-14-2011, 03:16 PM -
parseInt
By trefoil in forum New To JavaReplies: 4Last Post: 09-09-2009, 07:12 PM -
parseInt() vs. intValue()
By JavaPilot in forum New To JavaReplies: 5Last Post: 02-04-2009, 08:39 AM -
Integer.parseInt?
By Exhonour in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks