|
|
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.
|
|

06-13-2008, 03:12 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
|
[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:
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
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);
}
}
|
|

06-13-2008, 03:35 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 789
|
|
|
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.
|
|

06-13-2008, 03:55 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
|
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
|
|

06-13-2008, 05:03 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 899
|
|
|
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.
|
|

06-13-2008, 05:21 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
Thanks Fubarable,
That did the job!
Thanks for your help
Final code below
Best regards,
Winston
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);
}
}
|
|

06-13-2008, 06:49 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
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.
|
|

06-13-2008, 09:10 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
|
Thanks Sukatoa
Winston
|
|

09-17-2008, 12:23 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 2
|
|
|
Hi GilaMonster
I have had to tested it the Final code.but it didn't work. pleasure you to check it.
|
|

09-17-2008, 12:31 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

09-17-2008, 12:43 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 2
|
|
|
Hi GilaMonster
I'm so sorry about that. I'v tested it something wrong.
sorry again !
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|