-2

I'm getting a problem in one of my classes for a program where the program is skipping some of the variables. The idea of the program is that I want to write variables to file using bufferedreader. The user registers and logs in (this is done in another class) and they select what what type of event they want to post, after choosing they insert their desired information. The method i'm sharing here is in the instance that the user chooses "Online Event".

public void OnlineEvent()
    {
        try{
        File file = new File("Student&EventInfo.txt");
        FileWriter fw = new FileWriter(file);
        bw = new BufferedWriter(fw);
        
        System.out.println("What Is The Name/Title Of Your Event?");
        Title = sc.nextLine();
        bw.write("Title Of Event: " + Title);
        bw.write("\n");
        System.out.println("What Is The Description Of Your Event?");
        Description = sc.nextLine();
        bw.write("Description Of Event: " + Description);
        bw.write("\n");
        System.out.println("What Is The URL Code For The Online Event?");
        URL = sc.nextLine();
        bw.write("Event's URL Code: " + URL);
        bw.write("\n");
        System.out.println("At What Time Will The Event Be Held?");
        Time = sc.nextLine();
        bw.write("Time Of Event: " + Time);
        bw.write("\n");
        System.out.println("When Is The Date Of The Event?");
        Date = sc.nextLine();
        bw.write("Date Of Event" + Date);
        bw.write("\n");
        System.out.println("Will A Booking Be Required For This Event?");
        Booking = sc.nextLine();
        bw.write("Booking Required? :" + Booking);
        bw.write("\n");
        System.out.println("Will There Be Repeats This Event?");
        System.out.println("1. Yes");
        System.out.println("2. No");   
        b = sc.nextInt();
        
        //Execution In Case The User Says No
        if(b == 2)
        {
            b = b - 2;
            bw.write("There Will Be No Repeats For This Event");
            System.out.println("Do You Have Any Restrictions/Limitations To The Event?");
            System.out.println("(!)Eg: No Loud Music or Maximum Of 30 Participants");
            System.out.println("1. Yes");
            System.out.println("2. No");
            b = sc.nextInt();
            
            if(b == 1)
            {
                b = b - 1;
                System.out.println("What Restrictions/Limitations Will This Event Have?");
                Restrictions = sc.nextLine();
                bw.write("There Will Be The Following Restrictions:" + Restrictions);
                bw.write("\n");
                bw.close();        
            }
            
            if(b == 2)
            {
                b = b - 2;
                bw.write("There Will Be No Restrictions/Limitations For This Event");
                bw.write("\n");
                bw.close();
            }
        }
          
        //The Regular Execution If The User Says Yes
        if(b == 1)
        {
            b = b - 1;
            bw.write("This Event Will Have Repeats.");
            bw.write("\n");
            System.out.println("At What Time Do You Intend To Repeat This Event?");
            RETime = sc.nextLine();
            bw.write("Event Repeat Time: " + RETime);
            bw.write("\n");
            System.out.println("On What Date Will This Event Be Repeated?");
            REDate = sc.nextLine();
            bw.write("Event Repeat Date: " + REDate);
            bw.write("\n");
            System.out.println("Do You Have Any Restrictions/Limitations To The Event?");
            System.out.println("(!)Eg: No Loud Music or Maximum Of 30 Participants");
            System.out.println("1. Yes");
            System.out.println("2. No");
            b = sc.nextInt();
            
            if(b == 1)
            {
                b = b - 1;
                System.out.println("What Restrictions/Limitations Will This Event Have?");
                Restrictions = sc.nextLine();
                bw.write("There Will Be The Following Restrictions:" + Restrictions);
                bw.write("\n");
                bw.close();        
            }
            
            //Execution In Case The User Says No
            if (b == 2)
            {
                b = b - 2;
                bw.write("There Will Be No Restrictions/Limitations For This Event");
                bw.write("\n");
                bw.close();
            }
              
            }//End Of If Statement
        }catch(Exception ex)
            {
                System.out.println("The Error is" + ex);
            }
    }//End Of Method
  • Forgot to mention that it's specifically skipping the variables RETime (used for entering repeat times for events) and Restrictions (used for entering event restrictions). Sorry about this, its my first time using stack overflow. – MasterOfLag Nov 30 '20 at 14:08

1 Answers1

-1

Don't mix nextLine and any other scanner method. Use either only nextLine, or don't use nextLine at all. If you want to read entire lines worth (and you usually do!), always do this to your scanners:

Scanner s = new Scanner(...);
s.useDelimiter("\r?\n");

and then call .next() to read a line.

NB: Common advice is to call an extra nextLine() to 'clear the buffer'. This is bad advice; it does not work if the user ever uses the spacebar. And, you know, it is rather a large key on most keyboards. The above has no such bizarre caveats and is much simpler.

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37