2017-02-16 89 views
-2
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。任何人都可以幫我解決它嗎?

+0

爲什麼你解析來'long',然後轉換爲'int'?爲什麼不從一開始就不用'Integer.parseInt()'? – QBrute

+0

我運行了代碼,但沒有得到任何異常。儘管如此,輸出結果與亂碼沒有任何區別。 – Sweeper

+0

@QBrute也不能與int一起工作。 –

回答

-1

試試這個,

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 = Integer.parseInt(a,16); 
int num2 = Integer.parseInt(b,16); 
int num3 = Integer.parseInt(c,16); 
int num4 = Integer.parseInt(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; 
    } 
} 

解釋:我已經加入Integer.parseInt解析字符串輸入,以避免NumberFormatException的

+0

試試麼? (「3f18」,16)?它不工作。 –

+0

你現在得到的異常。這應該適用於給定的輸入8fc6。你應該需要讓你的邏輯正確。 – dullpointer

+1

沒有解釋的代碼轉儲不是一個好的答案。它要求讀者掃描每一行並以可視方式檢測您所更改的內容。相反,解釋你覺得應該改變,以及爲什麼。 – VGR