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

  • Home
  • SEARCH
  • 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 8305963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:14:06+00:00 2026-06-08T18:14:06+00:00

Sorry for the lengthy code below, my code is able to execute and run

  • 0

Sorry for the lengthy code below, my code is able to execute and run properly when user input option ‘5’. I have already get all the values from user input and there is no error, but it did not write to my file.

My file currently has data in it:

Anthony Ducan;anthony;a123;55 Peter Street;3321444;VISA;3213504011223
Barry Blake;barry;a999;456 George Street;23239876;VISA;435677779876
Claire Rerg;clare;c678;925 Edward Lane;67893344;MASTERCARD;223344556677

I am trying to store the exact same way as my textfile.
Is there something wrong with my coding at void writeLinesToFile() method or is it the void newCust() method?

Still puzzled that I have no errors but it is not writing to my file.

public MainPage1(){ //start of MainPage1()

        System.out.println("=====================================================");
        System.out.println("Kreg Hotel Booking System - Main Page");
        System.out.println("=====================================================");
        System.out.println("[1] General Information");
        System.out.println("[2] Make Booking");
        System.out.println("[3] Active Booking Summary");
        System.out.println("[4] Existing Customer");
        System.out.println("[5] New Customer");
        System.out.println("[6] Check Booking Status");
        System.out.println("[7] Promotions");
        System.out.println("[8] Exit\n");
        System.out.println("Note: You have to select option 2 & 3 before option 4 & 5\n");
        System.out.println("Please enter your selection: ");        

try{choice = input.nextInt();}//to try to see if user input integer

         catch (java.util.InputMismatchException e){//to catch if user didnt input integer  
            System.out.println("Invalid Input");
            return;
        }


        while(true){ //start of do-while loop

            if(choice==4||choice==5)
            {
                if(roomInfo.isEmpty()==true || addOns.isEmpty()== true){
                    System.out.println("Please enter option 2 or 3 before option 4 or 5");
                    choice=input.nextInt();
                }
                else if(choice == 4){existingCustomers();}
                else if(choice ==5){newCust();}
                else new MainPage1();               
            }

        switch(choice){

                case 1:break;
                case 2:break;
                case 3:break;
                case 4:break;
                case 5:newCust();
                        break;
                case 6:break;
                case 7:break;
                case 8:System.out.println("Thank you. See you again soon");//exit the menu
                        System.exit(0);break;
                default:System.out.println("Please enter number between 1 to 8");//prompts the user if they didnt enter between 1 to 8
                       choice = input.nextInt();
                       break;
            }

        }//while(choice!=1 || choice !=2 || choice!=3 ||choice!=4); // end of do-while loop



    }//end of MainPage1()


public static void main(String[] args){//start of main page


    new MainPage1();//to call the constructor


}//end of mainpage



public void writeLinesToFile(String filename,String[] linesToWrite,boolean appendToFile){

    PrintWriter pw = null;

    try {

      if (appendToFile) {

        //If the file already exists, start writing at the end of it.
        pw = new PrintWriter(new FileWriter(filename, true));

      }
      else {

        pw = new PrintWriter(new FileWriter(filename));
        //this is equal to:
        //pw = new PrintWriter(new FileWriter(filename, false));

      }

      for (int i = 0; i < linesToWrite.length; i++) {

        pw.println(linesToWrite[i]+";");

      }
      pw.flush();

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {

      //Close the PrintWriter
      if (pw != null)
        pw.close();

    }

  }

  public void newCust(){

    System.out.println("Welcome to Kreg Hotel Booking System");
    System.out.println("==============================================");
    System.out.println("Please enter your name: ");
    n = input.nextLine();
    n = input.nextLine();
    System.out.println("Please enter your login username: ");
    un = input.nextLine();
    System.out.println("Please enter your login password: ");
    pw = input.nextLine();
    System.out.println("Please enter your address: ");
    a = input.nextLine();
    System.out.println("Please enter your contact: ");
    con = input.nextLine();
    System.out.println("Please enter your credit card type: ");
    ct = input.nextLine();
    System.out.println("Please enter your credit card number: ");
    cn = input.nextLine();
    MainPage1 util = new MainPage1();
     util.writeLinesToFile("customerinfo.txt",new String[]{n,un,pw,a,con,ct,cn},true);
     new MainPage1();

  }
}//end of class
  • 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-06-08T18:14:07+00:00Added an answer on June 8, 2026 at 6:14 pm

    Have a look at File, BufferedWriter and FileWriter docs. You should be able to accomplish this with those two class. This code should get you started.

    String content = "Add this everytime";
    
    File file =new File("example.txt");
    
    //if file does not exists, then create it
    if(!file.exists()){
       file.createNewFile();
    }
    
    //true = append file
    FileWriter fileWritter = new FileWriter(file.getName(),true);
    BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
    bufferWritter.write(content);
    bufferWritter.close();
    
    System.out.println("Done");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with my code posted below, set and get methods. I
I have below code in html <div id=divTest> Hello <input type=text id=txtID/> <input type=text
Below is my code. Collisions actually register and execute, but only if your ship
I have 2 macros below that I am trying to execute 1 after another
Ok below is the full code I have written for what is suppose to
sorry but I do not have the actual code with me, but I will
The code below asks to input the numbers two times. public class Palindrome {
I apologize for the lengthy code. I have a simple question, but I thought
i already posted this question.Sorry that i failed to add any code.That thread is
Sorry for the length, wanted to give a complete description! I have a need

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.