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: Seperate numbers

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Seperate numbers

    So i've got a little proble with my code. The idea is to take a number for example 1234567890 and seperate its digits to : 1,2,3,4,5,6,7,8,9,0 or 0,9,8,7,6,5,4,3,2,1...For that a thought an algorithm but it doesnt work. Could u please check it!?!?!
     
    package javaapplication16;
     
    public class JavaApplication16 {
      public static void main(String[] args) {
        int x =1234567890;
        int y;
        for(int i = 1; i<10; i++){
          y = x % 10^i/10^(i-1);
          System.out.println(y);
      }
     }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Seperate numbers

    Try separating the digits out one step at a time using the % and the / operators
    instead of trying to do it all in one expression.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    nikomania (April 13th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Seperate numbers

    Yes I tried that and it worked but the code then bacame quite big...But I dont understand why it doesnt work that way also!! Thanks anyway

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Seperate numbers

    What does the code print out now?
    I dont understand why it doesnt work that way
    Try debugging the code by breaking that big expression up into single steps and print out the results at each step so you can see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Seperate numbers

    As a side note:
    int x =1234567890;
    for(int i = 1; i<10; i++)
    I can see that x is 10 digits, but the algorithm i see in my head would work for x with any number of digits

  7. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Seperate numbers

    If i do it step by step it prints 0 9 8 7 .... It works fine!

Similar Threads

  1. Text Input into seperate arrays help
    By CommisarDave in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 23rd, 2012, 02:34 PM
  2. JOptionPane display on seperate lines
    By ddonn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2012, 07:24 PM
  3. opening a new jframe using a seperate file
    By bigblondguy in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2011, 06:16 AM
  4. How to seperate several messages in a byte buffer
    By perl0101 in forum Java Networking
    Replies: 4
    Last Post: April 12th, 2011, 05:29 PM
  5. Switching between frames from a seperate class
    By kurt-hardy in forum AWT / Java Swing
    Replies: 4
    Last Post: February 14th, 2011, 03:19 AM