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! :)