import java.util.*;
class abc
{
public static void main(String[] args)
{
int sum=0,n=4,k=10,i,j;
char ch[] = new char[10];
char ans[] = new char[19];
Scanner sc = new Scanner(System.in);
try1 abc=new try1();
String a,b,c,d;
a= sc.nextLine();
b= sc.nextLine();
c= sc.nextLine();
d= sc.nextLine();
int num1 = (int) Long.parseLong(a,16);
int num2 = (int) Long.parseLong(b,16);
int num3 = (int) Long.parseLong(c,16);
int num4 = (int) Long.parseLong(d,16);
sum = num1 + num2 + num3 + num4;
String sumstart = Integer.toHexString(sum);
System.out.println(sumstart);
String temp=sumstart;
ch = temp.toCharArray();
for (i=1, j=0;i<5;i++,j++)
{
ans[j] = ch[i];
}
ans[j]='\0';
System.out.println(ans);
String anssing = Integer.toHexString(ch[0]);
int num5 = (int) Long.parseLong(anssing,16);
String ans1 = new String(ans);
System.out.println("ans 1 "+ans1);
int num6 =abc.convert(ans1);
System.out.println("num 6 vaala "+num6);
int ans2 = num5 + num6;
String hex4 = Integer.toHexString(ans2);
System.out.println("hex4 vaala "+hex4);
}
}
class try1
{
int convert(String a)
{
int num5 = Integer.parseInt("3f18",16);
System.out.println(num5);
return num5;
}
}
考慮任何輸入,例如:NumberFormatException異常而將字符串轉換爲int
8fc6
8fc6
8fc6
8fc6
輸出:
Numberformatexception
我已經存儲在字符數組的十六進制數,然後我將它轉換到字符串,現在我試圖將其轉換爲一個整數,但我收到NumberFormatException。任何人都可以幫我解決它嗎?
爲什麼你解析來'long',然後轉換爲'int'?爲什麼不從一開始就不用'Integer.parseInt()'? – QBrute
我運行了代碼,但沒有得到任何異常。儘管如此,輸出結果與亂碼沒有任何區別。 – Sweeper
@QBrute也不能與int一起工作。 –