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

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

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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;}

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default 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?
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

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

    Quote Originally Posted by eyalfish View Post
    the sum of his digits i mean
    Sum of what digits? 19 is simply (20 - 1). I still do not understand the objective.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default 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.
    l=m(sum);
    Replace this line with an if statement that compares l and sum to see which value is greater.
    Improving the world one idiot at a time!

  7. #7
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default 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.
    Improving the world one idiot at a time!

  9. #9
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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;}

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

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

    Read reply #6 again.
    Improving the world one idiot at a time!