0

I want to write a program witch has something to do with 3x+1, but that's not important. First it has to figure out if the given number is odd or even. This is done via switch statement:

switch(input % 2)
{
    case 0:   //even
        input = input / 2;
        count++;
        break;
    case 1:   //uneven
        input = 3 * input + 1;
        count++;
        break;
}
    
if(input == 1)
{
    System.out.println("reached 4, 2, 1 loop.\nTook " + count + " steps.");
    return;
} 
tryNumber(input);

Even numbers are divided by 2, uneven/odd ones are multiplied by 3 and incremented by 1.

So the problem is that it prints numbers which are completely wrong, for example using 2:

Please put in a number: 
2
50
25
76
38
19
58
29
88
44
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
reached 4, 2, 1 loop.
Took 24 steps.

Where is the mistake? Thanks for any replies.

whole code:

package de.craftingdani.math.main;

import java.io.IOException;

public class Main
{
    static long count = 0;
    
    public static void main(String[] args)
    {
        try
        {
            System.out.println("Please put in a number: ");
            tryNumber(System.in.read());
        }
        catch(Exception e)
        {
            System.err.println(" Error, please give a valid number");
            e.printStackTrace();
        }
    }

    public static void tryNumber(int input) throws InterruptedException, IOException
    {
        Thread.sleep(100);
        System.out.println(input);
        switch(input % 2)
        {
            case 0:   //even
                input = input / 2;
                count++;
                break;
            case 1:   //odd
                input = 3 * input + 1;
                count++;
                break;
        }
        
        if(input == 1)
        {
            System.out.println("reached 4, 2, 1 loop.\nTook " + count + " steps.");
            return;
        }
        tryNumber(input);
    }
}
  • 1
    What's the complete code? Are you using multiple threads for some reason? – Dici Dec 05 '21 at 19:46
  • Should I edit the Question? No, its just a main class and 2 methods (main and this one) and no more threads – CraftingDani Dec 05 '21 at 19:47
  • 2
    Yes, please [edit] the question and add a [MRE]. – Turing85 Dec 05 '21 at 19:48
  • 1
    Yes, please edit with the full code if it's not too long (I'm guessing it's short in your case). – Dici Dec 05 '21 at 19:48
  • @CraftingDani You might want to check https://stackoverflow.com/questions/20257346/strange-return-value-when-asking-for-int-input/20257393#20257393 or https://stackoverflow.com/questions/15446689/what-is-the-use-of-system-in-read/15446818 – Progman Dec 05 '21 at 19:51
  • 1
    You don't parse the initial value correctly. You take a character and use it as an int. Replace `System.in.read()` by `new Scanner(System.in).nextInt()` and you'll get the correct output. – Dici Dec 05 '21 at 19:59
  • System.in.read() reads the next byte of data. When you enter "2" it recieves the byte that represents the character 2. It does not receive the value two. You need to read a string, and parse it as an integer. – matt Dec 05 '21 at 19:59
  • I see! thank u all – CraftingDani Dec 05 '21 at 20:01

1 Answers1

1

The answer to the original question, as noted by @Dici and @matt in the comments area, is that the input is being interpreted as chars instead of integers.

I am posting this so that the OP can mark this question as "solved" and there won't be a continuous stream of people spending time on a question that has already been solved.

There is a reason we are asked by StackOverflow to limit the comments on the original question to requests for clarification, and to post answer in the answers area. It is courteous to the many volunteers who spend some of their time hoping to help people that actually still need it. It is also courteous to people looking for answers, to not have them buried in the comments.

If someone else posts the answer, including the OP, I'm good with deleting this, as long as there is some answer that is marked as the solution.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41