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 9 of 9

Thread: Help

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help

    I'm trying learn how to use methods, what I'm doing is I want to separate 2 numbers inside an array using methods, but i keep getting the same first number. can somebody tell me what I'm doing wrong here, thank you
    here is my code:
    public static void main(String args[]) {
            int input;
     
            System.out.print("Enter a number: ");
            input = console.nextInt();
             int[] series = {input};
             int[] series2 = {input};
       for (int i = 0; i < series.length; i++ ) {
            series[i] = separate(series[i]);     //separate first digit
            series2[i] = separate2(series[i]);  // separate second digit
            System.out.println(series[i] + " " + series2[i]);
        }
     
        }
     
        public static int separate(int n) { //this suppose to get the 1st digit of the number
            int result = n;
            result = result / 10;
     
            return result;
        }
          public static int separate2(int x) { //this suppose to get the 2nd digit of the number
           int result2 = x;
           result2 = result2 % 10;
     
            return result2;
        }
    output:
    Enter a number: 65
    6
    6<<< this should be 5
    Last edited by ichooseyou; March 13th, 2018 at 08:24 PM. Reason: added output and comments

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,559
    Thanks
    52
    Thanked 2,317 Times in 2,273 Posts

    Default Re: Help

    i keep getting the same first number
    Can you copy and paste here the contents of the console that shows what you are talking about?
    Add some comments describing the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help

    Quote Originally Posted by Norm View Post
    Can you copy and paste here the contents of the console that shows what you are talking about?
    Add some comments describing the problem.
    Fixed, sorry about that new in here

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,559
    Thanks
    52
    Thanked 2,317 Times in 2,273 Posts

    Default Re: Help

    Ok, that's a start.
    Now please add comments to the output saying what is wrong with it.
    For example:
    line 1
    line 3 <<<<< this should say line 2
    line 3
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help

    Fixed. can someone tell me what is wrong with my code, thank you

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,559
    Thanks
    52
    Thanked 2,317 Times in 2,273 Posts

    Default Re: Help

    Try debugging the code to see if you can find the problem.

    Add some print statements that show the values of the variables as they are used and changed.
    For example print the values passed to each of the methods: n and x so you can see what values the methods are receiving.
    Seeing those values will help you find the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help

    Quote Originally Posted by Norm View Post
    Try debugging the code to see if you can find the problem.

    Add some print statements that show the values of the variables as they are used and changed.
    For example print the values passed to each of the methods: n and x so you can see what values the methods are receiving.
    Seeing those values will help you find the problem.
    can you give me a clue on what to do, i tried but i think I'm missing something

  8. #8
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    183
    My Mood
    Asleep
    Thanks
    5
    Thanked 13 Times in 13 Posts

    Default Re: Help

    You are taking wrong parameter for series2[i].
    series[i] = separate(series[i]);     //separate first digit
    series2[i] = separate2(series[i]);  // separate second digit
    Should be
    series[i] = separate(series[i]);
    series2[i] = separate2(series2[i]);  // correct
    Last edited by John Joe; March 15th, 2018 at 06:14 AM.
    Whatever you are, be a good one

  9. The Following User Says Thank You to John Joe For This Useful Post:

    ichooseyou (March 15th, 2018)

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,559
    Thanks
    52
    Thanked 2,317 Times in 2,273 Posts

    Default Re: Help

    @John Joe - You could have let the OP try debugging the code before giving the answer.
    Can you explain to the OP how you found the problem?

    It's important to know how to debug code and find the problem by yourself.

    --- Update ---

    @John Joe - You could have let the OP try debugging the code before giving the answer.
    Can you explain to the OP how you found the problem?

    It's important to know how to debug code and find the problem by yourself.

    --- Update ---

    i tried but i think I'm missing something
    Please post the code that shows what you did and why you think you are missing something.
    If you don't understand my answer, don't ignore it, ask a question.