Results 1 to 10 of 10
  1. #1
    GilaMonster's Avatar
    GilaMonster is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    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:
    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
    Java 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);
    	}
    }

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    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.

  3. #3
    GilaMonster's Avatar
    GilaMonster is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    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.

  5. #5
    GilaMonster's Avatar
    GilaMonster is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Thumbs up

    Thanks Fubarable,

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

    Best regards,
    Winston

    Java 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);
    	}
    }

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    You may now mark this thread as SOLVED. :)

    Don't forget to add an even reputation to those who helped you....
    freedom exists in the world of ideas

  7. #7
    GilaMonster's Avatar
    GilaMonster is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanks Sukatoa

    Winston

  8. #8
    awei is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi GilaMonster
    I have had to tested it the Final code.but it didn't work. pleasure you to check it.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    awei is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi GilaMonster
    I'm so sorry about that. I'v tested it something wrong.
    sorry again !

Similar Threads

  1. Putting your own type in a Set
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:32 PM
  2. [SOLVED] curiosity about String type variable
    By monir6464 in forum Advanced Java
    Replies: 1
    Last Post: 04-08-2008, 11:13 AM
  3. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 12:06 PM
  4. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 01:04 PM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •