问题是当我输入小数输入时。例如10001.10。 它说NumberFormatException。但是,当我输入一个没有小数的数字,如" 1001110"它的工作正常。
import java.util.*;
import java.util.Arrays;
public class Binary {
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
String[] Condition = { "0", "1", "." };
//Accept First Input
String numInp1="";
System.out.print("Enter First Binary Number: ");
numInp1 = in.nextLine();
//Accept Second Input
String numInp2="";
System.out.print("Enter Second Binary Number: ");
numInp2 = in.nextLine();
//numInp1
String _Array1 []=new String[numInp1.length()];
//numInp2
String _Array2 []=new String[numInp2.length()];
//Catch error in numInp1/_Array1
boolean flag1 = false;
for(int i = 0; i < _Array1.length; i++)
{
_Array1[i] = String.valueOf(numInp1.charAt(i));
int cnt = 0;
for (int j = 0; j < Condition.length; j++)
{
if (!_Array1[i].equals(Condition[j]))
{
cnt++;
}
else
{
break;
}
if (cnt == 3)
{
flag1 = true;
}
}
}
//Catch error in numInp2//_Array2
boolean flag2 = false;
for(int i = 0; i < _Array2.length; i++)
{
_Array2[i] = String.valueOf(numInp2.charAt(i));
int cnt = 0;
for (int j = 0; j < Condition.length; j++)
{
if (!_Array2[i].equals(Condition[j]))
{
cnt++;
}
else
{
break;
}
if (cnt == 3)
{
flag2 = true;
}
}
}
//Getting which of the Array has Higher Input
if(flag2 == false && flag1 == false)
{
int HigherLength = 0;
if(_Array1.length >= _Array2.length)
{
HigherLength = _Array1.length + _Array2.length;
}
else
{
HigherLength = _Array2.length + _Array1.length;
}
//Declaring the size of higher length as the size of Equals[] Array
int Equals[] = new int[HigherLength];
int _Array1Int[] = new int[_Array1.length];
int _Array2Int[] = new int[_Array2.length];
for(int i = 0;i<_Array1.length;i++)
{
if(_Array1[i].equals("."))
{
_Array1[i] = "6";
}
_Array1Int[i] = Integer.parseInt(_Array1[i]);
}
for(int i = 0;i<_Array2.length;i++)
{
if(_Array2[i].equals("."))
{
_Array2[i] = "6";
}
_Array2Int[i] = Integer.parseInt(_Array2[i]);
}
//_________________________
int numInp1int = Integer.parseInt(numInp1);
int numInp2int = Integer.parseInt(numInp2);
int i,m,n,sum,carry=0;
for(i=Equals.length-1;i>=0;i--)
{
m=numInp1int%10;
n=numInp2int%10;
numInp1int=numInp1int/10;
numInp2int=numInp2int/10;
sum=m+n+carry;
if(sum==1)
{
Equals[i]=1;
carry=0;
}
else if(sum==2)
{
Equals[i]=0;
carry=1;
}
else if(sum==3)
{
Equals[i]=1;
carry=1;
}
else
{
Equals[i]=m+n+carry;
}
}
String Equals1[] = new String[Equals.length];
for(i=0;i<Equals.length;i++)
{
try{
Equals1[i] = String.valueOf(Equals[i]);
}
catch (NumberFormatException nfe)
{
nfe.printStackTrace();
}
if (Equals1[i].equals("6"))
{
Equals1[i] = ".";
}
System.out.print(Equals1[i]);
}
}
}
}
答案 0 :(得分:0)
在代码中执行以下更改
删除以下2行 -
int numInp1int = Integer.parseInt(numInp1);
int numInp2int = Integer.parseInt(numInp2);
替换为以下代码 -
StringBuilder strNum1 = new StringBuilder();
for (int num : _Array1Int)
{
strNum1.append(num);
}
int numInp1int = Integer.parseInt(strNum1.toString());
StringBuilder strNum2 = new StringBuilder();
for (int num : _Array2Int)
{
strNum2.append(num);
}
int numInp2int = Integer.parseInt(strNum2.toString());
答案 1 :(得分:0)
更新_Array1
和_Array2
不会更改您尝试解析为整数的numInp1int
和numInp2int
。
您应该学会解释堆栈跟踪中的错误消息。在这里,您可以清楚地看到问题所在:
Exception in thread "main" java.lang.NumberFormatException: For input string: "10001.1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at q35640801.Binary.main(Binary.java:120)
它出现在班级parseInt()
的电话Integer
中。它无法解析像10001.10