Hi

How can I get the maximum digit from a number entered by the user?

Example: if user inputs: 1521, the program should output 5 as maximum.

NOTE: I want it to be made using 2 methods. AND don't give me answers with arrays. :)

This is what I wrote so far..

``````public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = 0;
System.out.println("Enter a number: ");
number = input.nextInt();
System.out.print("Max is: "+max(number));
}
public static int maximum(int max){
int num = 0;
while(num != 0){
int rightDigit = num % 10;
num /= 10;
if(rightDigit > max)
rightDigit = max;
}
return max;
}``````

When I run it, whatever number I write.. I get Max: 0.

You're on the right lines, but have a closer look at
14: rightDigit = max;
and you use max as both the input parameter and as a working variable inside the method this is never a good idea. Much better to have 2 distinct variables. That will help avoid confusion like passing in a number "max", but then analysing "num" instead.

1. I don't understand how it could even compile.
On line #6 you call method max(number), however... your method is called maximum.

2. Your logic is all wrong...

``````public static int maximum(int max){
int num = 0;
while(num != 0){                // true upon entry, false for second iteration
int rightDigit = num % 10;  // will result in 0
num /= 10;                  // will result in 0
if(rightDigit > max)        // 0 is NOT > max
rightDigit = max;       // not executed
}
return max;                     // return original number entered???
}``````

1. I don't understand how it could even compile.
On line #6 you call method max(number), however... your method is called maximum.

2. Your logic is all wrong...

``````public static int maximum(int max){
int num = 0;
while(num != 0){                // true upon entry, false for second iteration
int rightDigit = num % 10;  // will result in 0
num /= 10;                  // will result in 0
if(rightDigit > max)        // 0 is NOT > max
rightDigit = max;       // not executed
}
return max;                     // return original number entered???
}``````

:D. Sorry about the method name. I was changing some names in the program to make it look better and I forgot to change the max(number) to maximum(number).
Also I am kind of lost about methods.
I don't know at the end what exactly should I return?
And if the user enters the number in the main method, how can I use it in the other method?

Your structure is basically right - you get a number from the user in main, pass it to maximum as a parameter, maximum returns the answer to main, main prints it.
You just got your variables all mixed up in the maximum method.

Think about what you are trying to accomplish in your maximum() method.
Pass the parameter in (you're doing that, but give it a different name).
Initially maxDigit = 0 because you haven't done anything yet.
Get the digits using your while loop (which you are doing), testing the digit against your maxDigit.
Return maxDigit.
It's rather simple, but we can't just GIVE you the code.
Good Luck!

hello everybody.I really need a answer for this question.I'm studying for Java programming.Hope for a answer soon.I'm VietNamese.I say English not good.Thanks

you can connect to me with link:this

Nguyen: you haven't asked a question yet, how do you want us to answer it?
If you mean the question asked by the OP, that has been answered. just read the previous replies.

If you just copy someone else's solution you will not improve your Java skills.
Try to do it yourself. That's the only way to improve.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.