import java.io.*;
import java.util.*;
public class TestProgram
{
public static void main(String args[])
{
String[][] students;
try
{
BufferedReader in = new BufferedReader(new FileReader("students.txt"));
int x=0, y=0, row = 0;
row = Integer.parseInt(in.readLine());
String line;
students = new String[row][6];
while ((line = in.readLine()) != null)
{
String[] values = line.split("[\\s]+");
y=0;
for (String str : values)
{
students[x][y]=str;
y++;
}
x++;
}
in.close();
String stuNo;
Scanner s2 = new Scanner(System.in);
System.out.println("Enter Student Number for search (Enter 0 to quit): ");
stuNo = s2.next();
while ( Integer.parseInt(stuNo) != 0)
{
String result = "";
int rw = students.length;
for (int i=0; i<students.length; i++) {
for (int j=0; j<4; j++) {
if (stuNo.equals(students[i][j])) {
rw = i;
//break;
}
}
}
if (rw == students.length) {
result += "Your search failed to return any results";
System.out.println("\n" + "Enter Student Number for search (Enter 0 to quit): ");
stuNo = s2.next();
}
else {
for (int j=0; j<6; j++) {
result += students[rw][j] + " ";
}
}
System.out.println("\n" + result+" ");
System.out.println("\n" + "Enter Student Number for search (Enter 0 to quit): ");
stuNo = s2.next();
}
}
catch(NumberFormatException nfEx)
{
System.out.println("Please enter a proper Student Number.");
}
catch( IOException ioException )
{
System.out.println("Problem reading students.txt");
}
}
}
5
111111 NAME NAME TITLE 00.0 0.00
111111 NAME NAME TITLE 00.0 0.00
111111 NAME NAME TITLE 00.0 0.00
111111 NAME NAME TITLE 00.0 0.00
111111 NAME NAME TITLE 00.0 0.00
boolean found = false;
while(!found){
String stuNo;
Scanner s2 = new Scanner(System.in);
System.out.println("Enter Student Number for search (Enter 0 to quit): ");
stuNo = s2.next();
//now with try/catch check that it is a number and break if it is not
//then check if it is 0 and break if it is
// then do the searcgh
// after the sercvha ay
if not found write all error messages and say continue - it will go to the beginning of the loop
if it founsd
found = true;
and then end the while loop
}
import java.io.*;
import java.util.*;
public class TestProgram
{
public static void main(String args[])
{
String[][] students;
try
{
BufferedReader in = new BufferedReader(new FileReader("students.txt"));
int x=0, y=0, row = 0;
row = Integer.parseInt(in.readLine());
String line;
students = new String[row][6];
while ((line = in.readLine()) != null)
{
String[] values = line.split("[\\s]+");
y=0;
for (String str : values)
{
students[x][y]=str;
y++;
}
x++;
}
in.close();
boolean found = false;
while(!found){
String stuNo;
Scanner s2 = new Scanner(System.in);
System.out.println("Enter Student Number for search (Enter 0 to quit): ");
stuNo = s2.next();
if( Integer.parseInt(stuNo) == 0)break;
// while ( Integer.parseInt(stuNo) != 0)
// {
String result = "";
int rw = -1;
for (int i=0; i<students.length; i++) {
for (int j=0; j<4; j++) {
if (stuNo.equals(students[i][j])) {
rw = i;
break; //break;
}
if(rw > 0)break;
}
if(rw > 0)break;
}
System.out.println("here: " + rw);
if (rw == -1) {
System.out.println( "Your search failed to return any results");
continue;
// System.out.println("\n" + "Enter Student Number for search (Enter 0 to quit): ");
// stuNo = s2.next();
// System.out.println("here1");
}
else {
for (int j=0; j<6; j++) {
result += students[rw][j] + " ";
}
}
System.out.println("result: " + result);
System.out.println("\n" + result+" ");
found = true;
// System.out.println("\n" + "Enter Student Number for search (Enter 0 to quit): ");
// stuNo = s2.next();
}
}
catch(NumberFormatException nfEx)
{
System.out.println("Please enter a proper Student Number.");
}
catch( IOException ioException )
{
System.out.println("Problem reading students.txt");
}
}
}
I am not a java expert and certainly don't have your students.txt file but you can try checking for empty lines like this:
while ((line = in.readLine()) != null)
{
// Skip over empty lines.
if (line.trim().length() == 0) {
continue;
}
...
...