Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-13-2008, 02:12 AM
GilaMonster's Avatar
Member
 
Join Date: Jun 2008
Posts: 9
Rep Power: 0
GilaMonster is on a distinguished road
Default [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
  #2 (permalink)  
Old 06-13-2008, 02:35 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default
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, 02:55 AM
GilaMonster's Avatar
Member
 
Join Date: Jun 2008
Posts: 9
Rep Power: 0
GilaMonster is on a distinguished road
Default
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, 04:03 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,481
Rep Power: 8
Fubarable is on a distinguished road
Default
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, 04:21 AM
GilaMonster's Avatar
Member
 
Join Date: Jun 2008
Posts: 9
Rep Power: 0
GilaMonster is on a distinguished road
Thumbs up
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, 05:49 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Default
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-13-2008, 08:10 AM
GilaMonster's Avatar
Member
 
Join Date: Jun 2008
Posts: 9
Rep Power: 0
GilaMonster is on a distinguished road
Default
Thanks Sukatoa

Winston
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-17-2008, 11:23 AM
Member
 
Join Date: Sep 2008
Posts: 2
Rep Power: 0
awei is on a distinguished road
Default
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, 11:31 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Why did you say that? What happen when you run the code?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-17-2008, 11:43 AM
Member
 
Join Date: Sep 2008
Posts: 2
Rep Power: 0
awei is on a distinguished road
Default
Hi GilaMonster
I'm so sorry about that. I'v tested it something wrong.
sorry again !
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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 08:32 PM
[SOLVED] curiosity about String type variable monir6464 Advanced Java 1 04-08-2008 12:13 PM
Type Casting Help rhm54 New To Java 2 02-07-2008 01:06 PM
How to cast an Object into a specific type (Integer/String) at runtime mailtogagan@gmail.com Advanced Java 2 12-03-2007 02:04 PM
Cast Error Caught (change) Class is really: java.lang.String barney Advanced Java 1 08-02-2007 05:07 PM


All times are GMT +2. The time now is 12:40 AM.



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