为什么我的Integer.parseInt不起作用?

时间:2017-11-21 18:03:47

标签: java compiler-errors integer syntax-error

分配是创建一个输出,从文件中读取并输出程序中声明的信息。在我回去之前它完全正常工作并添加了patientNumber字符串和第一个处理patientNumber的for循环。现在它显示了Integer.parseInt行无效的错误。发生了什么事?

my $txt = "INSTALLDATE=Tue Nov 07 19:35:38 UTC 2017";
print "Source: $txt\n";
my @res = $txt =~ /=(.*) UTC (.*)$/;
print "Result: $res[0] $res[1]\n";

这是错误消息:

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();
 }    
}   
}

1 个答案:

答案 0 :(得分:0)

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

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