gpt4 book ai didi

java - 为什么我的Integer.parseInt无法正常工作?

转载 作者:行者123 更新时间:2023-12-02 11:14:35 26 4
gpt4 key购买 nike

分配是创建一个从文件读取的输出,并输出程序中声明的信息。在我返回并添加PatientNumber字符串和第一个处理PatientNumber的for循环之前,它运行得很好。现在显示的错误是Integer.parseInt行不起作用。发生了什么?

package readfile;

import java.io.*;
public class ReadFile {

public static void main(String[] args)
{
String[] patientNumber = new String [15];
String[] patientFN = new String[15];
String[] patientLN = new String[15];
int[] patientBP = new int[15];
String lastnameBP = " ";
String all=" ";
int x = 0;
String Heading1 = "Patient #";
String Heading2 = "First Name";
String Heading3 = "Last Name";
String Heading4 = "Patient BP";
String Underl = "--------------------------------------------";
System.out.printf("%10s %10s %10s %10s", Heading1,Heading2,Heading3,Heading4);
System.out.println();
System.out.println(Underl);



String fileName="patient.txt";
//Name of the file with precise directory
//String fileName="patient.txt";
try{

//Create object of FileReader
FileReader inputFile = new FileReader(fileName);

//Instantiate the BufferedReader Class
BufferedReader bufferReader = new BufferedReader(inputFile);

//Variable to hold the one line data
String line;


// Read file line by line and print on the console
while ((line = bufferReader.readLine()) != null) {

//assigning patient number, first name, last name, and BP
for(int i=0; i<line.length(); i++){
if(line.charAt(i) == ' ')
{
patientNumber[x] = (line.substring (0,i));
all = line.substring(i+1, line.length());
}
}

for(int i=0; i<all.length(); i++)
{
if(all.charAt(i) == ' ')
{
patientFN[x] = all.substring(0,i);
lastnameBP = all.substring(i+1, all.length());
break; //breaking loop

}
}
for(int i =0; i < lastnameBP.length();i++) {
if(lastnameBP.charAt(i) == ' ') {
patientLN[x]= lastnameBP.substring(0, i);
patientBP[x] = Integer.parseInt(lastnameBP.substring(i + 1, lastnameBP.length()));
break;
}
}
x++;
}
//Close the buffer reader
bufferReader.close();
}catch(IOException e){
//At the top print your titles with format characters
// Each column is 10 Characters and left justified ("%-10s")
System.out.println("Error while reading file line by line:" + e.getMessage());
}
for(int k=0; k< 15; k++) {
System.out.printf("&-10s", patientNumber[k]);
System.out.printf("%-10s", patientFN[k]);
System.out.printf("%-10s", patientLN[k] );
System.out.printf("%-10s", patientBP[k] );
System.out.println();
}
}
}

这是错误消息:
Patient # First Name  Last Name Patient BP
--------------------------------------------
Exception in thread "main" java.lang.NumberFormatException: For input
string: ""
at

java.lang.NumberFormatException.forInputString
(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at readfile.ReadFile.main(ReadFile.java:74)
C:\Users\wking\AppData\Local\NetBeans\Cache\8.2\executor-
snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

最佳答案

您似乎在查找要解析的数字时遇到了麻烦。我可以建议您将主循环简化为以下内容:

while ((line = bufferReader.readLine()) != null)  {
String[] parts = line.trim().split(" ");
patientNumber[x] = parts[0];
patientFN[x] = parts[1];
patientLN[x] = parts[2];
patientBP[x] = Integer.parseInt(parts[3]);
x++;
}

关于java - 为什么我的Integer.parseInt无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47419775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com