Hi everyone-new here!!!i need some help:]

Hi, i am a beginner in java and i need some help.

my program should take 2 integers and find their range and output the biggest number(sum of his chars)

btw i dont have compile error but after i put the integer nothing come out from my methods.

sorry for my bad english and thanks for any help

Code :

String number1=JOptionPane.showInputDialog(null,"enter first number");
int intnum1=Integer.parseInt(number1);
String number2=JOptionPane.showInputDialog(null,"enter second number");
int intnum2=Integer.parseInt(number2);
JOptionPane.showMessageDialog(null,"the sum of the biggest number is"+range(intnum1,intnum2));
}
public static int range(int num1,int num2) {
int l=0;
if (num1>num2) {
int temp=num2;
num2=num1;
num1=temp;}
for (int i=num1;num1<=num2;i++){
int sum=0;
while (i!=0) {
sum+=i%10;
i=i/10;
}
l=m(sum);
}return l;}
public static int m(int sum){
int count=0;
if (sum>=count) count=sum;
return count;}

Re: Hi everyone-new here!!!i need some help:]

I do not understand what you are trying to do. Please provide sample input and sample output. ie if user enters 5 and 21 what should the output be?

Re: Hi everyone-new here!!!i need some help:]

for example if i put 10 and 20.

it will output 19.since 19 is the biggest number from 10 to 20.

the sum of his digits i mean

Re: Hi everyone-new here!!!i need some help:]

Quote:

Originally Posted by

**eyalfish**
the sum of his digits i mean

Sum of what digits? 19 is simply (20 - 1). I still do not understand the objective.

Re: Hi everyone-new here!!!i need some help:]

for example i put 10 and 20.i check the range of the numbers from 10 to 20 include.

and i need to output the number that his sum of his digits is the highest from all the numbers from 10 to 20.

--- Update ---

19 is 9+1=10

and 10 is the biggest sum of digits from 10 to 20.

Re: Hi everyone-new here!!!i need some help:]

Ahh!

As far as I can see the m method is totally pointless especially when it does not do anything.

Replace this line with an if statement that compares l and sum to see which value is greater.

Re: Hi everyone-new here!!!i need some help:]

it doesnt work for me;/

but why cant i do this with the m(sum) method.it should work and i dont have compile error

but something isnt work out.

thanks anyway

Re: Hi everyone-new here!!!i need some help:]

Your m method does not work as it compares each sum to 0 and not the previously highest sum. The other reason it doesn't work is that all sums will be greater than 0 so all it does is pass in a value and then return it. Your l variable will just hold the last sum that was calculated and not the highest.

Also, user more meaningful variable and method names, l and m provide no details to their purpose.

Re: Hi everyone-new here!!!i need some help:]

thanks a lot

--- Update ---

i tried what you suggest me to do .and got rid of the m method .but i got red mark under the return count,

how can i solve it?thanks again

Code :

public static int range(int num1,int num2) {
int l=0;
if (num1>num2) {
int temp=num2;
num2=num1;
num1=temp;}
for (int i=num1;num1<=num2;i++){
int sum=0;int count=0;
while (i!=0) {
sum+=i%10;
i=i/10;
}
if (sum>=count) count=sum;
}return count;}

Re: Hi everyone-new here!!!i need some help:]