Solved

Help NEEDED. Need some assistance with my Java code.

Posted on 2004-09-30
11
218 Views
Last Modified: 2008-01-09
I am asked a write a method that computes the sum of the digits in an integer and I am asked to use the following methhod declaration:

public staticint SumDigits(long n)

For example, sumDigits(123) returns 1+2+3 = 9

Here 's what I've gotten so far:

<code>
import java.io.*;

class Hw3Pr4 {
      public static int sumDigits(long n) throws IOException {
            BufferedReader stdin  = new BufferedReader (new InputStreamReader(System.in));
            
            int number, sum = 0;
            System.out.println("Enter an integer: ");
            
            String numberString = stdin.readLine();
            number = Integer.parseInt(numberString);
            // Sum the digits entered
            sum += number % 10;
        number /= 10;
        sum += number % 10;
        sum += number / 10;
       
        System.out.println("Sum of all digits: " + sum);
      }
}

</code>

I did not know whether to include the public static void main(String[] args) at the very first.

Please advise, am I on the right track?

Thanks
0
Comment
Question by:biloonline
  • 3
  • 3
  • 3
11 Comments
 

Author Comment

by:biloonline
ID: 12196548
I guess I have mis-used the declaration I was given.
0
 
LVL 92

Expert Comment

by:objects
ID: 12196554
you should be calculating the sun of the digits in 'n' shouldn't you (instead of prompting the user for a number)?

you'll need a main in the class you are using to test it.

0
 
LVL 92

Expert Comment

by:objects
ID: 12196566
as far as summing the digits you need to loop thru each digit, the code you have only deals with two digits.
another approach would be to convert the number to a string.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:biloonline
ID: 12196573
I guess I know how to do it, I 'll give it a shot and see how it goes.

Thanks objects for youe assistance.
0
 

Author Comment

by:biloonline
ID: 12196611
<code>


class SumDigits      {
      public static int sumDigits(long n) {

      return 0;
      }



int sum = 0;


      for (int i = digt.length(); i > 0; i--) {

            sum += Character.digit(digt.charAt(i - 1),10);

            digt = digt.substring(0,i - 1);
            
            return sum;
            
            }


public static void main(String [] args) {
      
      System.out.println("SumDigits of " + args[0] + " = " +
      SumDigits.sumDigits(Long.parseLong(args[0])));


      }
}

</code>

Am I missing something?

I am getting 2 errors:

E:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw3Pr1\SumDigits.java:13: illegal start of type
        for(int i = digt.length(); i > 0; i--) {
        ^
E:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw3Pr1\SumDigits.java:29: <identifier> expected
        }
         ^
2 errors

Thank you
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12196639
>   return 0;
>     }

those lines don't look they should be there
0
 
LVL 8

Expert Comment

by:thomas908
ID: 12197490
your code (that is "for" loop) is outside the method. put it within the method sumDigits().
0
 
LVL 8

Expert Comment

by:thomas908
ID: 12197576
i'd suggest using the approach mentioned by objects above.
0
 
LVL 8

Expert Comment

by:thomas908
ID: 12197578
Something like

class Test
{
      public static void main(String[] args)
      {
            new Test().sumDigits(args[0]);
      }
      public void sumDigits(String s)
      {
        int sum = 0;

          for(int i=0; i<s.length(); i++)
            {
           
                  sum = sum + Character.getNumericValue(s.charAt(i));
            }
            System.out.println(sum);
      }
}
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 71
Arrays.asList  VS  ArrayList 4 62
how to install java on RHEL image on EC2 4 31
runtime exception 2 30
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question