Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-09-2007, 06:46 PM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
Can't convert java.lang.String to int.
Hi could you help me this script. I got a error I don't know why.
I can't run this code

Code:
import javax.swing.JOptionPane; public class Ito { public static void main(String[] args) { int number = JOptionPane.showInputDialog("Enter number: "); switch(number) { case 0: System.out.println("you typed zero"); break; case 1: System.out.println("you typed one"); break; default: System.out.println("you didn't type zero or one"); break; } System.exit(0); }}
Here is error:

Code:
A:Ito.java:7: Incompatible type for declaration. Can't convert java.lang.String to int. int number = JOptionPane.showInputDialog("Enter number: "); ^ 1 error Tool completed with exit code 1
Could you help me.
Thanks
Albert
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-13-2007, 04:16 PM
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
The error message says it all. The showInputDialog method returns a string so you cannot assign it to an integer datatype. What you would need to do is convert it to an integer first.
Code:
import javax.swing.JOptionPane; public class ito { public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter number: "); int number = Integer.parseInt(input); switch(number) { case 0: System.out.println("you typed zero"); break; case 1: System.out.println("you typed one"); break; default: System.out.println("you didn't type zero or one"); break; } System.exit(0); }}
Of course it would probably be a good idea to add in some code to validate the input to ensure it actually is a number that they entered and also the required number of digits.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-13-2007, 06:05 PM
Member
 
Join Date: Jul 2007
Posts: 3
satya123 is on a distinguished road
Code:
import javax.swing.JOptionPane; public class Ito { public static void main(String[] args) { String pn = JOptionPane.showInputDialog("Enter number: "); int number=Integer.parseInt(pn); switch(number) { case 0: System.out.println("you typed zero"); break; case 1: System.out.println("you typed one"); break; default: System.out.println("you didn't type zero or one"); break; } System.exit(0); }}

Now this code won't give that error i think....just parsed the returned string uing Integer.parseInt() wrapper class function.....
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
<variable>(Java.lang.string) in <classname> cannot be applies to () inksmithy New To Java 5 01-13-2008 11:36 PM
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String) baltimore New To Java 1 08-06-2007 08:45 AM
Error: cannot be applied to (java.lang.String) carl New To Java 1 08-05-2007 07:33 AM
Cast Error Caught (change) Class is really: java.lang.String barney Advanced Java 1 08-02-2007 05:07 PM
how to convert String number to int gabriel New To Java 3 08-02-2007 05:46 AM


All times are GMT +3. The time now is 03:07 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org