I am trying to have my code read numbers from a file, and store the lines into a string array using the .split method
after this it is to check if the elements of that array are equal to certain strings, but the if statement i use for this is ignored and the following command runs anyway.
Can someone please help me and tell me why this is happening?
My code is as follows and following the code is the exact contents of the file used, too recreate if necessary name the file "inrecords.dat"
package project04;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Project04
{
static BufferedReader fin;
static FileOutputStream fout;
static String line = "b";
static String line1;
public static void fio()
{
String [] num = new String[1000];
String [] num0 = new String[1];
try
{
fin = new BufferedReader(new FileReader("inrecords.dat"));
int cnt = 0;
try
{
while(cnt <= 29)
{
line = fin.readLine();
num0 = line.split("");
int ctr = line.length();
System.out.println(line);
System.out.println(num0[4]+num0[5]);
cnt++;
int a = 0;
while(a <= (ctr-1))
{
if(num0[a].equals("7") && num0[a+1].equals("3") && num0[a+2].equals("5"))
{
line1 = line;
System.out.println(line1);
}
a++;
}
}
}
catch (IOException | ArrayIndexOutOfBoundsException a)
{
System.out.println("Error 1");
}
try
{
fin.close();
}
catch(IOException a)
{
System.out.println("Error 1a");
}
}
catch (FileNotFoundException | NullPointerException e)
{
System.out.println("Error 2");
}
}
public static void main(String[] args)
{
fio();
}
}
Contents of file "inrecords.dat"
Zimmerman, Frederick A. (301)640-7354
Thomas, Charles G. (301)735-1293
Pearl, Minnie I. (202)586-7424
Blackwell, Constance J. (202)931-4412
Payne, Isaac H. (301)426-9982
Mitchell, Grayson T. (301)424-6574
Borges, Emmanuel (301)429-3406
Risher, Margaret V. (202)839-6668
Mulvahill, Margaret T. (301)735-3345
Lum, Ewella F. (301)664-8591
Gray, Ronald D. (301)884-3506
Plummer, Bentley M. (301)680-5928
Bennett, Nyra B. (301)735-7434
Johnston, Sharon N. (301)648-4805
Thomas, June D. (202)735-5400
Ice, Ann-Marie (301)647-9537
Jones, MaryAnn (301)324-5343
Harris, Melany L. (301)836-5113
Gunter, Stehanie (301)942-4056
Hill, Carolyn G. (301)735-3891
McCormick, Patricia W. (301)822-5975
Dougherty, Shannon V. (301)252-3250
Knight, Nicki P. (301)408-7357
Moore, Valerie C. (301)345-2023
Edwards, Muriel A. (301)735-8649
Jones, Jenny J. (301)735-0659
Bowman, Elizabeth N. (301)735-9535
Goodwin, Norma H. (301)372-3443
Harley, Albert C. (301)364-0512
Suggestions:
You will have an array index of bound exception when a >= ctr-2
for the following segment of your code:
while(a <= (ctr-1)) { if(num0[a].equals("7") && num0[a+1].equals("3") && num0[a+2].equals("5")) // other code }
use a for
loop instead: for(a=0; a < ctr.length-3; a++)
Always catch the exception and try to print stack trace or log it: Which tell you what kind of exception you might have. printing Error 1a
or Error 2
is not going to tell anybody what really happened.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.