1

i have a block of code that is supposed to ask a question, get input THEN ask the next question. Not only does this code not wait for the first input, it does not allow me to perform the second input. I'm new to java and I have read the chapter on file inputs, repeatedly. However I still don't see why this error is occurring. The two String variables(pword, uname) where created outside of this section of code and the FileWriter and PrintWriter classes need to append data to a file is also present outside the loop as well. Do i need to open and close the file before and after each input? that seems excessive.

for (index = 0; index <= arraylength; index ++)
        {


   System.out.println("Enter a username..");
   uname = keyboard.nextLine(); 
   outputFile.println(uname);

   System.out.println("Enter a password..");
   pword = keyboard.nextLine();
   outputFile.println(pword);



        }

this is the full program, updated with the suggestions to use "keyboard.next();" instead of "keyboard.nextLine();"

in my IDE i still get the error to my console

Enter a username.. Enter a password..

when i want it to ask for the username, then take my input, then ask for the password.

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

public class Register {

public static void main (String [] args) throws IOException 
     {

     int index ; 
     int arraylength;
     String uname, pword; 


   Scanner keyboard = new Scanner(System.in);

   //create users
   System.out.println("How many users would you like to create"); 
        arraylength = keyboard.nextInt();

   /*
   create a filewriter object, name the registration database, and 
   create the file using the name of the database. 

   Pass the filewriter object to the print writer object as an arg
   */

   FileWriter fwriter = new FileWriter("database.txt", true);
   PrintWriter outputFile = new PrintWriter (fwriter); //constructor

    //loop user creation      
   for (index = 0; index <= arraylength; index ++)
        {

   keyboard = new Scanner(System.in);
   System.out.println("Enter a username..");
   uname = keyboard.next(); // create string object 
   outputFile.println(uname);


   keyboard = new Scanner(System.in);
   System.out.println("Enter a password..");
   pword = keyboard.next();
   outputFile.println(pword);

        //creat the new file with filewriter so the file is not erased 
        //write to the new file with printwriter object
     }
   outputFile.close();
    }
}
Anonymous
  • 11
  • 7

2 Answers2

0

You can use java.util.Scanner for reading the data from standard input

Scanner scan = new Scanner(System.in);
System.out.println("Enter a username..");
uname = scan.next(); 

This will wait until you press Enter key. Hope it will help you

  • I tried this with my "keyboard" object and it still does not work. That is what I was attempting to describe to the other respondent. – Anonymous Dec 05 '16 at 09:45
  • what exactly is your `keyboard` object? – XtremeBaumer Dec 05 '16 at 09:46
  • @XtremeBaumer Scanner keyboard = new Scanner(System.in); – Anonymous Dec 05 '16 at 09:53
  • okay. does it work if you move the outpuFile.println(); outside the loop? because scanner.next() does work properly – XtremeBaumer Dec 05 '16 at 09:55
  • @XtremeBaumer if i do that, then the user name and password will not be inputted sequentially, which is what I need for this particular program For the username I want to get user input, assign to a variable, print that variable to a file, then repeat that process for the password. – Anonymous Dec 05 '16 at 10:02
  • ahh yes my bad. can you just for testing purpose replace it with a System.out.println()? and of what type is your outputFile? – XtremeBaumer Dec 05 '16 at 10:05
  • scanner.next() and scanner.nextLine() both works fine, I think there might be some issue with printing the data to the output file. – Sachin Mesare Dec 05 '16 at 10:06
  • I will suggest you to save the username and password in some arraylist or Map and after getting all the data from user, save it to your file. – Sachin Mesare Dec 05 '16 at 10:08
  • @XtremeBaumer the output file is text. I have updated to show the orriginal program – Anonymous Dec 05 '16 at 10:13
  • @SachinMesare the issue may be with printing to the file, but there is also an issue when the statements show to my console. I have uploaded the full program to show exactly what i have, as well as exactly what is happening. – Anonymous Dec 05 '16 at 10:13
  • I am using netbeans to code and compile on a windows machine. I don't know if that is relevant. – Anonymous Dec 05 '16 at 10:14
  • `public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); for (int i = 0; i < 2; i++) { System.out.println("Enter Name : "); System.out.println(scan.nextLine()); System.out.println("Enter Pass : "); System.out.println(scan.nextLine()); } }` can you run this and tell us the behavior – Sachin Mesare Dec 05 '16 at 10:16
  • @Anonymous Are you able to run above code? – Sachin Mesare Dec 05 '16 at 10:26
  • @SachinMesare i ffound the problem and described it on another comment – Anonymous Dec 05 '16 at 12:06
0

I like to use BufferedWriter more than PrintWriter and its working with the BufferedWriter:

    int index;
    int arraylength;
    String uname, pword;

    Scanner keyboard = new Scanner(System.in);

    // create users
    System.out.println("How many users would you like to create");
    arraylength = keyboard.nextInt();

    /*
     * create a filewriter object, name the registration database, and
     * create the file using the name of the database.
     * 
     * Pass the filewriter object to the print writer object as an arg
     */

    BufferedWriter bw = new BufferedWriter(new FileWriter(
            "full file path goes here",true));

    // loop user creation
    for (index = 1; index <= arraylength; index++) {

        keyboard = new Scanner(System.in);
        System.out.println("Enter a username..");
        uname = keyboard.next(); // create string object
        bw.write(uname);

        keyboard = new Scanner(System.in);
        System.out.println("Enter a password..");
        pword = keyboard.next();
        bw.write(pword);

        bw.newLine();
    }
    bw.close();

this should give you the desired behavior

XtremeBaumer
  • 5,158
  • 1
  • 15
  • 44
  • will the buffered writer overwrite if there is a file already present with the name that I designate in the constructor? – Anonymous Dec 05 '16 at 11:31
  • it does overwrite the content in this case. but you can add a true boolean in the filewriter constructor, then it does append. just updated my answer to fit the appending – XtremeBaumer Dec 05 '16 at 11:34
  • I found my problem. As i said i use netbeans and when i hit run at the top of the screen for some reason it would not run the specifc program. I had to hit run file. however the keyboard.next(); method worked when i did this – Anonymous Dec 05 '16 at 11:38
  • so you always ran the wrong file? – XtremeBaumer Dec 05 '16 at 11:38
  • i guess so... which i need to look into why using the run button at the top of the screen does not run the specifc file. It (the compiler) says it's running the whole project, but i don't understand where it's getting the other code from because running it the other way works... – Anonymous Dec 05 '16 at 12:08