-2

This is an assignment we are having. We have to convert a unicode to a decimal value using while. I am using switch case for the inputs, so it's easy to divide each input but I am having trouble calculating the sum of all the values now.

public class Exercise { 

    public static void main (String[] args) throws Exception
    {
        int uni = 0, code = 0, dec=0, sum=0;
        System.out.println("Please write a Unicode of the form \\uxxxx");

        while ((uni = System.in.read()) != '\n') {
            code++;

            if (uni!='\\' && code == 1) {
                System.out.println("You did not write \\ correctly");   
                break;
            }

            if (uni!='u'&&code == 2) {
                System.out.println("You did not write u correctly");
                return;
            }

            if(code >=3 && code <=6)
            {
                if(uni >= '0' && uni <= '9'|| uni >= 'a'&&uni<='f')
                {
                    switch (code) {
                    case 3:
                        dec=uni*4096;
                        break;
                    case 4: 
                        dec=uni*256;
                        break;
                    case 5:
                        dec=uni*16;
                        break;
                    case 6:        
                        dec=uni*1;
                        break;
                    default:
                        Sytem.out.println("Too much values!");
                        break;
                }
            sum=sum+dec;
            }
            else
            {
                System.out.println("Wrong!!!");
                return;
            }           
         }          
     }        
    System.out.println(sum);
    }
}

As always, any help is appreciated! :)

eroruz
  • 83
  • 10
  • First of all, I would create a method that converts a single hex digit to decimal (this conversion is currently wrong in your implementation). The second step would be to recognise that every hexa digit is worth 16 times as much as the next, so you could get the value with a simple division. Thirdly, I would represent characters as `char` type, not `int`. – biziclop Nov 11 '15 at 16:17
  • Why cant you just do dec+=unicodeValue(unicode) and print it out at the end – Abhijeet Kushe Nov 11 '15 at 16:17
  • 1
    Your logic/math to try and combine the `ints` back into a valid character value is incorrect. Your **step debugger** will show you where the math is wrong. –  Nov 11 '15 at 16:19

2 Answers2

4

You need to convert the char uni to its numerical value (0-15) and use that.

int digit = 0;
if ('0' <= uni && uni <= '9') {
    digit = uni - '0';
} else ('a' <= uni && uni <= 'f') {
    ... // 10-15
} else ('A' <= uni && uni <= 'F') {
    ...
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

Just declare an int variable (maybe called "sum")

int sum = 0;

outside of the loop, and each time you calculate a new dec, add that to the sum value.

sum = sum + dec;

Then at the end of the function, return or print the value of sum.

The sum = sum + dec; has to go inside the while loop, at the end of the case statement. This is the point at which you have just set the value of dec on that run through the loop. If you leave it out of the while loop, then you will only add dec to it once, after the while loop exits, instead of adding each value to it.

EkcenierK
  • 1,429
  • 1
  • 19
  • 34
  • I see your point but it does not work correctly. I just tried a unicode value of \u0123 and it prints out 51, which is not right. The right value is 291. Any suggestions? – eroruz Nov 11 '15 at 15:56
  • Can you please update your question with the changes you made to the code? – EkcenierK Nov 11 '15 at 16:00
  • Done. I also tried writing the sum in the cases, and inside the while loop. – eroruz Nov 11 '15 at 16:03
  • It definitely will not work if you put it outside the while loop, see my updated answer for the explanation, and update your post again once you've changed it. – EkcenierK Nov 11 '15 at 16:08
  • I tried it sooner, but this is what I get. Please write a Unicode of the form \uxxxx \u0123 210003 I'll edit my question now – eroruz Nov 11 '15 at 16:11
  • You may now also want to check the other answer, as they have explained that your calculations from unicode (hex digits) to integers, are incorrect. I was about to extend my answer, but they beat me to it ;) – EkcenierK Nov 11 '15 at 16:20