Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 06-13-2008, 03:12 AM
GilaMonster's Avatar
Member
 
Join Date: Jun 2008
Posts: 9
GilaMonster is on a distinguished road
[SOLVED] Cast string type to int type
Hi all,

I am attempting to split a 6 digit numeric string into 2 seperate strings of 5 char's and 1 char respectively.

I am receiveing this compile error:
Quote:
TicketNumber.java:22: inconvertible types
found : java.lang.String
required: int
ticket1 = (int)ticketNumber1;
^
TicketNumber.java:26: inconvertible types
found : java.lang.String
required: int
ticket5 = (int)ticketNumber5;
Seem I cannot cast a string type to an integer type?

Full code below
Thanks
Winston
Code:
import javax.swing.JOptionPane; public class TicketNumber { public static void main(String[] args) { String ticketNumber, ticketNumber1, ticketNumber5; int ticket5, ticket1, seven = 7; //Get ticket number ticketNumber = JOptionPane.showInputDialog(null, "Please enter a 6 digit number", "Travel Tickets Company", JOptionPane.QUESTION_MESSAGE); //Get last digit of ticket number, convert to integer ticketNumber1 = ticketNumber.substring(5,1); ticket1 = (int)ticketNumber1; //Get first five digits of ticket number, convert to integer ticketNumber5 = ticketNumber.substring(1,5); ticket5 = (int)ticketNumber5; //Logic Test: Divide 5 Digit ticket number by 7. The remainder should equal the sixth digit previously truncated boolean isTestSuccessful = (ticket1 == (ticket5 % seven)); //Output results JOptionPane.showMessageDialog(null, "Original ticket # is: " + ticketNumber + "\nFive digit ticket number is: " + ticketNumber5 + "\nThe last digit is: " + ticketNumber1 + "\nDoes the five digit number divided by seven equal the last digit? " + "\n" + isTestSuccessful); System.exit(0); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-13-2008, 03:35 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 789
Nicholas Jordan is on a distinguished road
Without reading your code, you can get the character at any positon in a String object by calling .charAt(int position);

I will read your code momentarily, I am busy right now.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-13-2008, 03:55 AM
GilaMonster's Avatar
Member
 
Join Date: Jun 2008
Posts: 9
GilaMonster is on a distinguished road
Thanks Nicholas,

Using your example, how would I get a substring of 5 char's beginning at char 1?

How do I then cast from a 5 char string into a 5 digit integer?

Thanks much,
Winston
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-13-2008, 05:03 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 899
Fubarable is on a distinguished road
You can't "cast" a String to an int. Java just won't allow it. You can however parse the string with Integer.parseInt(yourString). Be sure to trap for number format exceptions here.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-13-2008, 05:21 AM
GilaMonster's Avatar
Member
 
Join Date: Jun 2008
Posts: 9
GilaMonster is on a distinguished road
Thanks Fubarable,

That did the job!
Thanks for your help
Final code below

Best regards,
Winston

Code:
import javax.swing.JOptionPane; public class TicketNumber { public static void main(String[] args) { String ticketNumber, ticketNumber1, ticketNumber5; int ticket5, ticket1, seven = 7; //Get ticket number ticketNumber = JOptionPane.showInputDialog(null, "Please enter a 6 digit number", "Travel Tickets Company", JOptionPane.QUESTION_MESSAGE); //Get last digit of ticket number, convert to integer ticketNumber1 = ticketNumber.substring(5); ticket1 = Integer.parseInt(ticketNumber1); //Get first five digits of ticket number, convert to integer ticketNumber5 = ticketNumber.substring(0,5); ticket5 = Integer.parseInt(ticketNumber5); //Logic Test: Divide 5 Digit ticket number by 7. The remainder should equal the sixth digit previously truncated boolean isTestSuccessful = (ticket1 == (ticket5 % seven)); //Output reuslts JOptionPane.showMessageDialog(null, "Original ticket # is: " + ticketNumber + "\nFive digit ticket number is: " + ticketNumber5 + "\nThe last digit is: " + ticketNumber1 + "\nIs the remainder of the five digit number divided by seven equal the last digit? " + "\n" + isTestSuccessful); System.exit(0); } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-13-2008, 06:49 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
You may now mark this thread as SOLVED.

Don't forget to add an even reputation to those who helped you....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-13-2008, 09:10 AM
GilaMonster's Avatar
Member
 
Join Date: Jun 2008
Posts: 9
GilaMonster is on a distinguished road
Thanks Sukatoa

Winston
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-17-2008, 12:23 PM
Member
 
Join Date: Sep 2008
Posts: 2
awei is on a distinguished road
Hi GilaMonster
I have had to tested it the Final code.but it didn't work. pleasure you to check it.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-17-2008, 12:31 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Why did you say that? What happen when you run the code?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-17-2008, 12:43 PM
Member
 
Join Date: Sep 2008
Posts: 2
awei is on a distinguished road
Hi GilaMonster
I'm so sorry about that. I'v tested it something wrong.
sorry again !
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
Putting your own type in a Set Java Tip java.lang 0 04-15-2008 09:32 PM
[SOLVED] curiosity about String type variable monir6464 Advanced Java 1 04-08-2008 01:13 PM
Type Casting Help rhm54 New To Java 2 02-07-2008 02:06 PM
How to cast an Object into a specific type (Integer/String) at runtime mailtogagan@gmail.com Advanced Java 2 12-03-2007 03:04 PM
Cast Error Caught (change) Class is really: java.lang.String barney Advanced Java 1 08-02-2007 06:07 PM


All times are GMT +3. The time now is 01:07 AM.


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