0

I am trying to add four integers ie 4+3+2+1 but i get the value 202

class Task3{
public static void main (String args[]){
    String x=(args[0]);
    int I = Integer.parseInt (x);   
    char c1 = x.charAt(0);
    char c2 = x.charAt(1);
    char c3 = x.charAt(2);
    char c4 = x.charAt(3);
    System.out.println("First and last digit is: " + c1 +"," + c4);
    if (c1 > c4)
        System.out.println("The first digit is larger");
    else
        System.out.println("The second digit is larger");
    int sum = c1 + c2 + c3 + c4;
    System.out.println(sum);
}

}

5
  • 3
    Because this is the sum of their ASCII values. Commented Sep 16, 2014 at 13:26
  • 2
    remember you sum the values of '4' '3' '2' and '1' ascii and not their value (4 3 2 or 1). parse it before sum Commented Sep 16, 2014 at 13:26
  • 1
    ohhh!! you are trying to add chars to get the integer value great. Commented Sep 16, 2014 at 13:26
  • you know what c1+c2+c3+c4 add up to?. They will give you ascii values of characters. Commented Sep 16, 2014 at 13:27
  • @MarounMaroun Not ASCII values; UTF-16 code units values. Commented Sep 16, 2014 at 16:20

6 Answers 6

2

replace char c1 = x.charAt(0); with Character.getNumericValue(x.charAt(0))

Sign up to request clarification or add additional context in comments.

Comments

2

This is because you are adding numeric values of UNICODE code points, not digits represented by the corresponding characters.

In order to get a digit from a character code, call Character.digit(c1, 10) (ten indicates that you want a decimal digit).

int c1 = Character.digit(x.charAt(0), 10);
int c2 = Character.digit(x.charAt(1), 10);
int c3 = Character.digit(x.charAt(2), 10);
int c4 = Character.digit(x.charAt(3), 10);

Comments

2

Change

int sum = c1 + c2 + c3 + c4;

to

int sum = c1 + c2 + c3 + c4 - 4 * '0';

Since c1, c2, c3, c4 are all characters, so a digit say. 4 is taken as '4' that is basically the ASCII value, so to get 4 and not '4' you need to subtract '0' from each of c1, c2, c3, c4, so 4 * '0' subtracted

2 Comments

Cool approach, but you should explain why he has to subtract 4 * '0' :)
@kocko, if you have to explain it, then it's a dirty hack. I don't know about "cool" code, but where I work, we like to see every line of code explain itself.
2

You are trying to sum up the chars ASCII codes and not the digit values. You have to retrieve the corresponding digit for each character and then evaluate you addition result:

class Task3
{
  public static void main (String args[])
  {
    String x=(args[0]);
    int I = Integer.parseInt (x);   
    char c1 = x.charAt(0);
    char c2 = x.charAt(1);
    char c3 = x.charAt(2);
    char c4 = x.charAt(3);
    int i1 = Character.digit(c1, 10);
    int i2 = Character.digit(c2, 10);
    int i3 = Character.digit(c3, 10);
    int i4 = Character.digit(c4, 10);
    System.out.println("First and last digit is: " + i1 +"," + i4);
    if (i1 > i4)
      System.out.println("The first digit is larger");
    else
      System.out.println("The second digit is larger");
    int sum = i1 + i2 + i3 + i4;
    System.out.println(sum);
  }

}

Comments

1

The reason of the wrong output others already explained it now one of the way to get the correct output

int sum =0;
while(I>0){
    int rem = I%10;
    sum+=rem;
    I = I/10;
}
System.out.println(sum);

Comments

0

I presume you are passing "4321" or similar to the program.

c1 will not be the number 1 but actually the Unicode number representing the character '1' which is actually 31 (in hexadecimal). The latest addition is adding these Unicode numbers.

See http://unicode-table.com/en/ for the list of unicode numbers to characters and also http://docs.oracle.com/javase/tutorial/i18n/text/unicode.html for more details of characters in Java.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.