Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:16:53+00:00 2026-05-27T11:16:53+00:00

Ok So this code I have below is compiling but I have a logic

  • 0

Ok So this code I have below is compiling but I have a logic error. Ok so After i enter the file with the data i would like it to read and I choose to print that data on screen I have the choice of loading a different file. However, when I do enter the name of the new file that I would like to load it does not load the file and the error message that I designated for that particular situation it outputted. I think i have to flush the stream buffer after each write or something like that. So um if anyone can point of why this is happening then I would appreciate it.

import java.util.Scanner;
import java.io.*;

public class Driver {
    private static int numberOfCustomer = 0;
    private static Customer[] customerList = new Customer[10];

        private static void readInCustomer(String file){
        FileReader freader;
        BufferedReader inputFile;
        try{
            freader = new FileReader(file);
            inputFile = new BufferedReader(freader);
            String strLine;


            while ((strLine = inputFile.readLine()) != null)   {
                    customerList[numberOfCustomer] = new Customer();
                    customerList[numberOfCustomer].ID = strLine;
                    customerList[numberOfCustomer].name  =                 inputFile.readLine();
                    customerList[numberOfCustomer].address  = inputFile.readLine();
                    customerList[numberOfCustomer].phone  = inputFile.readLine();
                    numberOfCustomer++;
            }

            inputFile.close();
        }catch(Exception e){
            System.out.println("Could not find file "+file+" System will now exit");
            System.exit(1);

        }

        return;
    }

        private static void printCustomer(Customer customer){
        System.out.println("The Customer Data corresponding to Customer Number " + customer.ID  + " is:");
        System.out.println("Name:\t\t\t"+customer.name);
        System.out.println("Address:\t\t"+customer.address);
        System.out.println("Telephone:\t\t"+customer.phone);
        System.out.println();
        return;
    }

        private static void printAll(){
        boolean hasID = false;
            Scanner keyboard = new Scanner(System.in);

        System.out.println("All customers from data file "+numberOfCustomer);


        System.out.println(" Here they are!!! ");
        for(int i=0; i<numberOfCustomer; i++){
            if(customerList[i] != null){
                System.out.println("The Customer Data corresponding to Customer Number " + customerList[i].ID  + " is:");
                System.out.println("Name:\t\t\t"+customerList[i].name);
                System.out.println("Address:\t\t"+customerList[i].address);
                System.out.println("Telephone:\t\t"+customerList[i].phone);
            }
        }
        if(!hasID){

        System.out.println("");
        }
        System.out.println("Would you like to go to the menu? (Y or N):");
        String input = keyboard.nextLine();
        char repeat = input.charAt(0);
         if(repeat == 'Y' || repeat == 'y'){Menu();} 
                    return;
    }

    private static void Menu(){
        boolean hasID = false;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("YOU MAY CHOOSE FROM THE FOLLOWING OPTIONS:");
      System.out.println("A. SEARCH for a customer by ID number");
      System.out.println("B. DISPLAY the entire Customer List");
      System.out.println("C. RE-LOAD DATA from a different data file");
      System.out.println("D. QUIT:");

        String choice = keyboard.nextLine();

        char repeat = choice.charAt(0);
         if(repeat == 'A' || repeat == 'a'){Scostomer();} 
            if(repeat == 'B' || repeat == 'b'){printAll();}
            if(repeat == 'C' || repeat == 'c'){mainn();}
              return;

        }

   public static void Scostomer(){
    boolean hasID = false;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Type in the Id you are search for");

        String customerID = keyboard.nextLine();

        for(int i=0; i<numberOfCustomer; i++){
            if((customerList[i]!=null) && (customerID.equals(customerList[i].ID))){
                hasID = true;
                printCustomer(customerList[i]);
                i=customerList.length;
            }
        }

        if(!hasID){
            System.out.println("Sorry, customer not found.");
        }
        System.out.println("Would you like to search for another custnomer? (Y or N):");
        String input = keyboard.nextLine();
        char repeat = input.charAt(0);
         if(repeat == 'Y' || repeat == 'y'){Scostomer();} 
            if(repeat == 'N' || repeat == 'n'){Menu();}
                    return;
    }

    public static void main(String arg[]){
        Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the fileName that contains the data of your customers: ");
        readInCustomer(keyboard.nextLine());
        Menu();
        return;
        }

    public static void mainn(){
        Scanner keyboard = new Scanner(System.in);

            System.out.println("Enter the fileName that contains the data of your customers: ");
        readInCustomer(keyboard.nextLine());
        Menu();
        return;

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:16:54+00:00Added an answer on May 27, 2026 at 11:16 am

    Works for me (load a file, type c to load new file, type b to display all). I’d suggest there’s something simple wrong (wrong filename or something silly), so you you’ll want to log the exception details, instead of ignoring them in your catch block.

        } catch(Exception e) {
            e.printStackTrace(); // should tell you what's wrong!
            System.out.println("Could not find file "+file+" System will now exit");
            System.exit(1);
    
        }
    

    Flushing is not relevant here as you are reading, not writing. You could probably tidy up the whole menu/input loop too, it’s a bit wacky (but it works!). For example, your main and mainn methods are identical – have you considered abstracting that out into a separate method?

    Instead of limiting yourself to 10 customers, you can use a List and not have to worry about how many customers you’re going to read in (I’ve only changed your code to use a List instead of an array):

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class Driver {
        private static int numberOfCustomer = 0;
        // List instead of array!
        private static List<Customer> customerList = new ArrayList<Customer>();
    
        private static void readInCustomer(String file) {
            FileReader freader;
            BufferedReader inputFile;
            try {
                freader = new FileReader(file);
                inputFile = new BufferedReader(freader);
                String strLine;
    
                while ((strLine = inputFile.readLine()) != null) {
                    Customer customer = new Customer();
                    customer.ID = strLine;
                    customer.name = inputFile.readLine();
                    customer.address = inputFile.readLine();
                    customer.phone = inputFile.readLine();
                    customerList.add(customer); // add to the List!
                }
    
                inputFile.close();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Could not find file " + file
                        + " System will now exit");
                System.exit(1);
    
            }
    
            return;
        }
    
        private static void printCustomer(Customer customer) {
            System.out
                    .println("The Customer Data corresponding to Customer Number "
                            + customer.ID + " is:");
            System.out.println("Name:\t\t\t" + customer.name);
            System.out.println("Address:\t\t" + customer.address);
            System.out.println("Telephone:\t\t" + customer.phone);
            System.out.println();
            return;
        }
    
        private static void printAll() {
            boolean hasID = false;
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("All customers from data file " + numberOfCustomer);
    
            System.out.println(" Here they are!!! ");
            for (Customer customer : customerList) {
                System.out
                        .println("The Customer Data corresponding to Customer Number "
                                + customer.ID + " is:");
                System.out.println("Name:\t\t\t" + customer.name);
                System.out.println("Address:\t\t" + customer.address);
                System.out.println("Telephone:\t\t" + customer.phone);
            }
    
            if (!hasID) {
                System.out.println("");
            }
            System.out.println("Would you like to go to the menu? (Y or N):");
            String input = keyboard.nextLine();
            char repeat = input.charAt(0);
            if (repeat == 'Y' || repeat == 'y') {
                Menu();
            }
            return;
        }
    
        private static void Menu() {
            boolean hasID = false;
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("YOU MAY CHOOSE FROM THE FOLLOWING OPTIONS:");
            System.out.println("A. SEARCH for a customer by ID number");
            System.out.println("B. DISPLAY the entire Customer List");
            System.out.println("C. RE-LOAD DATA from a different data file");
            System.out.println("D. QUIT:");
    
            String choice = keyboard.nextLine();
    
            char repeat = choice.charAt(0);
            if (repeat == 'A' || repeat == 'a') {
                Scostomer();
            }
            if (repeat == 'B' || repeat == 'b') {
                printAll();
            }
            if (repeat == 'C' || repeat == 'c') {
                mainn();
            }
            return;
    
        }
    
        public static void Scostomer() {
            boolean hasID = false;
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("Type in the Id you are search for");
    
            String customerID = keyboard.nextLine();
    
            // iterate over the List!
            for (Customer customer : customerList) {
                if (customerID.equals(customer.ID)) {
                    hasID = true;
                    printCustomer(customer);
                    break;
                }
            }
    
            if (!hasID) {
                System.out.println("Sorry, customer not found.");
            }
            System.out
                    .println("Would you like to search for another custnomer? (Y or N):");
            String input = keyboard.nextLine();
            char repeat = input.charAt(0);
            if (repeat == 'Y' || repeat == 'y') {
                Scostomer();
            }
            if (repeat == 'N' || repeat == 'n') {
                Menu();
            }
            return;
        }
    
        public static void main(String arg[]) {
            Scanner keyboard = new Scanner(System.in);
            System.out
                    .println("Enter the fileName that contains the data of your customers: ");
            readInCustomer(keyboard.nextLine());
            Menu();
            return;
        }
    
        public static void mainn() {
            Scanner keyboard = new Scanner(System.in);
    
            System.out
                    .println("Enter the fileName that contains the data of your customers: ");
            readInCustomer(keyboard.nextLine());
            Menu();
            return;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data that looks like this . And my code below simply
Hi I have below pseudo code with throws an exception like this throw new
I have a code like this below, which gives me a $link that equals
I get this error, while I'm testing the code below: You have an error
I have this code below. As you can see I am passing two variables
I have this javascript code below that uses jquery, it is suppoed to be
Racking my brains on this one. I have the code below: the first stages
Compiling on linux using gcc. I would like to convert this to hex. 10
When compiling the code below, I get the following error: PersonalInformation is not abstract
I currently have a library-like .c file (that'll be shown below). I have 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.