Results 1 to 4 of 4
Like Tree3Likes
  • 2 Post By Diargg
  • 1 Post By shamssss.169

Thread: Homework Help

  1. #1
    EZ-Ed is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Homework Help

    Hello everyone,
    I'm having issues with one of my programs and need some help. The purpose of the program is to summarize digits into an int using a method. My code works for positive numbers however, when I input a negative number, the result is positive. I've tried several if statements but cannot seem to get it working. Any suggestions? Here is my code:

    Java Code:
    public class Ex5_2SumDigit {
      public static void main(String[] args) {
      
      String integerString;
      int integer;
      	integerString = JOptionPane.showInputDialog("Please enter an integer: ");
    	integer = Integer.parseInt(integerString);
      
    
       JOptionPane.showMessageDialog (null, "The sum of digits for " + integer +
          " is " + sumDigits(integer));
      }
    
      public static int sumDigits(long n) {
        int temp = (int)Math.abs(n);
        int sum = 0;
    	
      		while (temp != 0) {
      		int remainder = temp % 10;
          sum += remainder;
          temp = temp / 10;
       	 
    	 }
    	 	
        return sum;
      }

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Homework Help

    What does
    Java Code:
    int temp = (int)Math.abs(n);
    do to negative numbers?
    Fubarable and EZ-Ed like this.

  3. #3
    shamssss.169 is offline Member
    Join Date
    Apr 2012
    Location
    Texas USA
    Posts
    2
    Rep Power
    0

    Default Re: Homework Help

    Its because of the abs() method your code is not working for negative numbers try the following code for SumDigits()
    public static int sumDigits(long n) {
    int temp = (int)(n);
    int sum = 0;

    while (temp != 0) {
    int remainder = temp % 10;
    sum += remainder;
    temp = temp / 10;

    }

    return sum;
    EZ-Ed likes this.

  4. #4
    EZ-Ed is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Homework Help

    I got my program to work, I was simply just overlooking a very simple mistake. I'm not sure why I put Math.abs there in the first place. Thanks for the help.

Similar Threads

  1. Need some help with my homework.
    By JacobM in forum New To Java
    Replies: 11
    Last Post: 02-04-2011, 03:42 AM
  2. Homework help
    By lackingunderstanding4now in forum New To Java
    Replies: 4
    Last Post: 11-08-2010, 04:18 AM
  3. Homework help
    By noobster in forum New To Java
    Replies: 13
    Last Post: 10-31-2010, 10:02 PM
  4. Need help with homework.
    By JavaNewbie0000 in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 04:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •