Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Using char at to split up a interger

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using char at to split up a interger

    I am trying to split up a four digit integer so i can perform math on each integer to "encrypt" it. This is what i have so far.

    import javax.swing.JOptionPane;
     
    public class Project5e
    {
    	public static void main(String[] args)
    	{
    	String input;
     
    	int number1;
    	int number2;
    	int number3;
    	int number4;
     
     
    	input = JOptionPane.showInputDialog("What is your four digit number?");
    	number1 = (int)(input.charAt(0));
    	number2 = (int)(input.charAt(1));
    	number3 = (int)(input.charAt(2));
    	number4 = (int)(input.charAt(3));
     
    	System.out.print(number1);
    	}
    }

    i know i only have number1 in the output......that is just to test. When i run that and put 1234 for the input i get the number 49 for number 1. Any help is appreciated. I dont know if my current code is just wrong or if i am approaching the entire thing totally wrong.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Using char at to split up a interger

    49 is the ascii value for the char "1". Java is assuming you want to convert the primitive char into the ascii value of its content.

    Your best bet would be to either to read the whole number in as a string, then convert it,. or to convert from char to a String then to an Integer for each char.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using char at to split up a interger

    Ok, so if i read in the entire int 1234 and put it into a string and then convert it. How do i split it up into 4 separate variables so i can apply a algorithm to each individual number?

    Edit: is there a way to use the "char.at" command but for integers?
    Last edited by mooncowtime; October 25th, 2011 at 08:20 PM.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using char at to split up a interger

    You use the charAt method to get the char. You can then use a method in the Character class to convert the char '1' to the int 1.
    Improving the world one idiot at a time!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Using char at to split up a interger

    Other than all of the above suggestions, you can separate the four digit integer like;
    int x=1234;
     
    while(x/10!=0){
    int temp=x%10;
    x=x/10;
    System.out.println(temp);
    }
    System.out.println(x);

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using char at to split up a interger

    this should help you. it asks user to type in 4 digits which is first stored as a string.

    then the parseInt and charAt etc. applies the first character of the string to variable 'number1', the 2nd character of the string to variable 'number2' etc. etc.

    import java.util.Scanner;
     
    public class StringtoInt {
     
        public static void main(String[] args) {
     
            String input;
     
            int number1, number2, number3, number4;
     
            Scanner scan = new Scanner(System.in);
     
            System.out.println("Enter 4 digits: ");
            input = scan.nextLine();
     
            number1 = Integer.parseInt(String.valueOf(input.charAt(0)));
            number2 = Integer.parseInt(String.valueOf(input.charAt(1)));
            number3 = Integer.parseInt(String.valueOf(input.charAt(2)));
            number4 = Integer.parseInt(String.valueOf(input.charAt(3)));
     
        }
    }

    Hope this helps.

Similar Threads

  1. Java split problem..
    By arch in forum Java SE APIs
    Replies: 5
    Last Post: August 11th, 2011, 07:48 AM
  2. ParsingTo Interger Issue
    By gamsa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 19th, 2011, 05:14 PM
  3. Help me split and then join a file
    By babbupandey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 13th, 2010, 12:20 PM
  4. MVC - Split the Java servlet(help needed)
    By kamweshi in forum Java Servlet
    Replies: 1
    Last Post: October 18th, 2010, 09:02 AM
  5. how to display combination of char and interger
    By bsrk315 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 22nd, 2010, 08:09 AM