작동하도록 코드를 가져 오는 데 문제가 있습니다. 저는 컴퓨터 과학 수업을위한 프로젝트를 진행하고 있으며, 파일을 읽고 수학을 수행 할 수 있도록 프로그램을 만들어야합니다. 이 작업을 시도 할 때 코드가 작동하지 않습니다. 나는 똑같은 코드를 작성한 친구와 함께 확인해 보았는데 작동하지 않았다. I 가지고 2/3,4/5 -1/6,2/4 1/1,1/1Integer.parseInt() issues
코드 :
입력 프로그램이 같은 모양을 판독하여 그 파일 .TXT 이와 같은 기록 같습니다
import javax.swing.JFileChooser;
import java.util.*;
public class ProjectTest
{
public static void main(String[] args) throws Exception
{
JFileChooser chooserRational = new JFileChooser();
int returnValRational = chooserRational.showOpenDialog(null);
if(returnValRational == JFileChooser.APPROVE_OPTION)
{
System.out.println("You chose to open this file: " + chooserRational.getSelectedFile().getName());
Scanner input = new Scanner(chooserRational.getSelectedFile());
while(input.hasNext() == true)
{
String line = input.nextLine();
String[] output = line.split(",");
String[] output1 = output[0].split("/");
String[] output2 = output[1].split("/");
String a = output1[0];
String b = output1[1];
String c = output2[0];
String d = output2[1];
int int1 = Integer.parseInt(a);
int int2 = Integer.parseInt(b);
int int3 = Integer.parseInt(c);
int int4 = Integer.parseInt(d);
System.out.println(int1 + " " + int2 + " " + int3 + " " + int4);
}
input.close();
}
}
}
바로 현 I 출력 A, B, C, D는, 코드가 완벽하게 잘 작동 완벽 값을 출력한다. 어떤 도움을 크게 감상 할 수
Exception in thread "main" java.lang.NumberFormatException: For input string: "?2"
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 ProjectTest1.main(ProjectTest1.java:33)
: 코드 Integer.parseInt(a)
를 볼 때, 그러나, 그것은 나에게 다음과 같다 오류를 제공합니다. 같은 줄에 데이터의 배수 그룹이 있기 때문에
input.hasNext() == 사실이 중복 사용 input.hasNext() 대신 –
난 그냥 내 컴퓨터에 + 붙여 넣기 코드를 복사, 그것은 (문제없이 자바 버전 "1.8을 사용할 수 있습니다. 0_05 ") – msrd0
입력 한 텍스트에"? 2 "가 포함되어 있습니까? 정수가 아닌 '? 2'문자열을 파싱하는 것처럼 보입니다. 따라서 Integer.parseInt를 호출하기 전에 문자열을 출력하고 문자열이 실제로 정수인지 확인하십시오. –