Reputation: 35
the instructions are: Enter an integer (32 bit) and sum its digits, you may be given a negative or positive integer. When reading in a negative integer, sum the digits but ignore the - sign.
this is what I have. Somehow I can't get it to ignore the negative signs when the integer is negative.
import java.util.Scanner;
public class Exercise2_6M {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Enter amount
System.out.print("Enter an integer:");
int integer = input.nextInt();
if (integer < 0)
integer = -integer;
// Calculations
int rinteger = Math. abs (integer);
int lastinteger = integer % 10;
int X = integer / 10;
int secondinteger= X % 10;
int firstinteger = X /10;
int finalinteger = firstinteger + secondinteger + lastinteger;
// Display results
System.out.println("Sum all digits in " + rinteger + " is " + finalinteger);
}
}
Upvotes: 0
Views: 2429
Reputation: 1468
To ignore the negative sign:
int integer = Math.abs(input.nextInt());
Edit: I didn't notice that your code already did this. To make the code show the negative sign at the end, get rid of the following lines:
if (integer < 0)
integer = -integer;
since you're already doing
int rinteger = Math. abs (integer);
Then, replace all instances other than the fist of integer
in the calculations sections with rinteger, like this
// Calculations
int rinteger = Math. abs (integer);
int lastinteger = rinteger % 10;
int X = rinteger / 10;
int secondinteger= X % 10;
int firstinteger = X /10;
int finalinteger = firstinteger + secondinteger + lastinteger;
Finally, when displaying the results, use integer instead of rinteger to restore the minus sign, or just put in a minus sign manually if integer<0
.
Your code is also flawed if integer is more than 3 digits, as others have pointed out, but that's a separate issue.
To add more than 3 digits, you'll need a loop. Try this:
import java.util.Scanner;
public class Exercise2_6M
{
public static void main(String[] args)
{
// Create a Scanner
Scanner input = new Scanner(System.in);
// Enter amount
System.out.print("Enter an integer: ");
int integer = input.nextInt();
// Calculations
int rinteger = Math. abs (integer);
int sum = 0;
int i=0;
//loop through each digit (starting from the least significant) until the end of the number
while(rinteger / Math.pow(10,i) > 0)
{
sum+=getDigit(rinteger,i);
i++;
}
// Display results
System.out.println("Sum all digits in " + integer + " is " + sum);
}
public static int getDigit(int num, int power)
{
return (num % (int)Math.pow(10,power+1)) / (int)Math.pow(10,power);
}
}
Upvotes: 1
Reputation: 82559
When I run it, it ignores the negatives just like it's supposed to. There are two problems I see.
Here's my test outputs:
The first is good, second is good, third should say "Sum all digits in -4 is 4", and last should say "Sum all digits in 12345 is 15".
C:\Documents and Settings\glowcoder\My Documents>java Exercise2_6M
Enter an integer:5
Sum all digits in 5 is 5
C:\Documents and Settings\glowcoder\My Documents>java Exercise2_6M
Enter an integer:100
Sum all digits in 100 is 1
C:\Documents and Settings\glowcoder\My Documents>java Exercise2_6M
Enter an integer:-4
Sum all digits in 4 is 4
C:\Documents and Settings\glowcoder\My Documents>java Exercise2_6M
Enter an integer:12345
Sum all digits in 12345 is 132
Upvotes: 4
Reputation: 1960
I can't recall specifically, but integer might be a reserved java keyword, so you might want to change that variable name (i.e. inputInt or something). The thought behind your code looks sounds... but instead of:
integer = -integer;
try:
inputInt = (-1)*inputInt;
That should do the trick.
Upvotes: -1