Java 为什么我的Integer.parseInt不工作?

Java 为什么我的Integer.parseInt不工作?,java,compiler-errors,integer,syntax-error,Java,Compiler Errors,Integer,Syntax Error,赋值是创建一个从文件读取并输出程序中所述信息的输出。在我返回并添加patientNumber字符串和处理patientNumber的第一个for循环之前,它工作得非常好。现在它显示了一个错误,即带有Integer.parseInt的行不工作。发生了什么事 package readfile; import java.io.*; public class ReadFile { public static void main(String[] args) { String[] pati

赋值是创建一个从文件读取并输出程序中所述信息的输出。在我返回并添加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();
 }    
}   
}

看起来您在查找要分析的数字时遇到了问题。我建议您将主循环简化为以下内容:

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++;
}

您试图解析为整数的字符串的值是多少?提示:该值不代表整数。@大卫,我不知道你在问什么。在我编辑它之前,它使用了该值。编辑不应该对这部分产生任何影响。它在异常中指出,空字符串不能被解析为整数。你会期待什么?@WJK:我想问的是。。。您试图解析为整数的字符串的值是多少?例如,如果值是
“1”
,则可以将其解析为整数
1
。现在格式化的错误消息告诉您该值为
“”
。您希望它是什么整数值?
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++;
}