Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to stop a while loop thats reading text files

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to stop a while loop thats reading text files

    I have a program that searches through text files for whatever the user wants, if the program cant find i want it to stop and say "Student not found. Please try again.".

    Currently Ive been trying to use boolean, but when it prints it does it multiple times and i only want one line

    import java.nio.file.*;
    import java.io.*;
    import java.util.Scanner;
     
    public class SearchStudentFiles
    {
        public static void main(String[] args)
        {
     
            String studentSearch ="";
            while (!"999".equals(studentSearch))                             
            {
            File file = new File("/Users/Graham/NetBeansProjects/StudentSort/HonorsStudentList.txt");                    //PATH WILL BE DIFFERENT 
            File file2 = new File("/Users/Graham/NetBeansProjects/StudentSort/GoodStandingStudentList.txt");             //PATH WILL BE DIFFERENT
            File file3 = new File("/Users/Graham/NetBeansProjects/StudentSort/ProbationStudentList.txt");                //PATH WILL BE DIFFERENT
            Scanner scan = new Scanner(System.in);
     
            System.out.println("Enter student's First name, Last name, or Student ID Number to SEARCH: ");
            studentSearch = scan.next();
     
            Scanner scanner;
     
     
     
            //Honors Students File Search
            try
            {
              scanner = new Scanner(file).useDelimiter(",");
              boolean found = false;
              while (scanner.hasNext())
              {
                  final String lineFromFile = scanner.nextLine();
                  if (lineFromFile.contains(studentSearch))
                  {
                      Student student = new Student();
                      String[] fieldArray =  new String[3];
                      String delimiter = ",";
                      fieldArray = lineFromFile.split(delimiter);
                      student.setStudentId(Integer.parseInt(fieldArray[0]));
                      student.setLastName(fieldArray[1]);
                      student.setFirstName(fieldArray[2]);
                      student.setGradePoint(Double.parseDouble(fieldArray[3]));
     
                      System.out.printf("%-15s%-15s%13s\n", "Student ID", "Name (First Last)",  "GPA");
                      System.out.printf("%-15s%-12s%-10s%8.1f%25s\n", 
                                        student.getStudentId(), 
                                        student.getFirstName(),
                                        student.getLastName(),
                                        student.getGradePoint(),
                                        "Honor Student"); 
                      found = true;
                      break;
     
                  }
                  if(!found){
                      System.out.println("Student not found. Please try again.");
                  }
     
              }
            }
            catch (IOException e)
            {
                System.out.println(" SOMETHING WENT WRONG " + file.toString());
            }
     
     
     
     
            //Students in Good Standing File Search
            try
            {
              scanner = new Scanner(file2).useDelimiter(",");
              boolean found = false;
              while (scanner.hasNext())
              {
                  final String lineFromFile = scanner.nextLine();
                  if (lineFromFile.contains(studentSearch))
                  {
                      Student student = new Student();
                      String[] fieldArray =  new String[3];
                      String delimiter = ",";
                      fieldArray = lineFromFile.split(delimiter);
                      student.setStudentId(Integer.parseInt(fieldArray[0]));
                      student.setLastName(fieldArray[1]);
                      student.setFirstName(fieldArray[2]);
                      student.setGradePoint(Double.parseDouble(fieldArray[3]));
     
                      System.out.printf("%-15s%-15s%13s\n", "Student ID", "Name (First Last)",  "GPA");
                      System.out.printf("%-15s%-12s%-10s%8.1f%25s\n", 
                                        student.getStudentId(), 
                                        student.getFirstName(),
                                        student.getLastName(),
                                        student.getGradePoint(),
                                        "Good Standing Student");
                      found = true;
                      break;
     
                  }
                  if(!found){
                      System.out.println("Student not found. Please try again.");
                  }
     
              }
            }
            catch (IOException e)
            {
                System.out.println(" SOMETHING WENT WRONG " + file.toString());
            }
     
     
     
     
            //Probation Student File Search
            try
            {
              scanner = new Scanner(file3).useDelimiter(",");
              boolean found = false;
              while (scanner.hasNext())
              {
                  final String lineFromFile = scanner.nextLine();
                  if (lineFromFile.contains(studentSearch))
                  {
                      Student student = new Student();
                      String[] fieldArray =  new String[3];
                      String delimiter = ",";
                      fieldArray = lineFromFile.split(delimiter);
                      student.setStudentId(Integer.parseInt(fieldArray[0]));
                      student.setLastName(fieldArray[1]);
                      student.setFirstName(fieldArray[2]);
                      student.setGradePoint(Double.parseDouble(fieldArray[3]));
     
                      System.out.printf("%-15s%-15s%13s\n", "Student ID", "Name (First Last)",  "GPA");
                      System.out.printf("%-15s%-12s%-10s%8.1f%25s\n", 
                                        student.getStudentId(), 
                                        student.getFirstName(),
                                        student.getLastName(),
                                        student.getGradePoint(),
                                        "Probation Student"); 
                      found = true;
                      break;
     
                  }
                  if(!found){
                      System.out.println("Student not found. Please try again.");
                  }
     
     
              }
     
            }
            catch (IOException e)
            {
                System.out.println(" SOMETHING WENT WRONG " + file.toString());
            } 
        }
     
        }   
    }



    What am i doing wrong?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to stop a while loop thats reading text files

    Please copy the contents of the console from when the program executes that shows the problem.
    Add a comment like: (<<< HERE ...) where the program is not doing what is desired.

    What am i doing wrong?
    Are you printing the Not Found for every record that is read?
    When should the Not Found message be printed? Should it be after all the records are read from the file, not after each one.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to stop a while loop thats reading text files

    Enter student's First name, Last name, or Student ID Number to SEARCH:
    232
    Student not found. Please try again.
    Student not found. Please try again.<<Here
    Student not found. Please try again.<<Here
    Student not found. Please try again.<<Here




    I want it to go through all of the text and then if it finds nothing print a single line saying "Student not found"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to stop a while loop thats reading text files

    go through all of the text
    When will that be true? That is where the test for found should be made and the message should be printed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading a text file + Nested Loop
    By iCurtisIT in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 1st, 2013, 03:13 PM
  2. [SOLVED] Java - reading in two text files of different sizes into an ArrayList
    By deeevo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 25th, 2013, 02:00 PM
  3. Reading text files and the calculating averages from it
    By tuffguy721 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 30th, 2013, 05:18 PM
  4. Reading Text Files Assignment [Scanner Method]
    By iAce in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 9th, 2012, 10:41 PM
  5. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM

Tags for this Thread