0

My code simply asks the user to enter data for a file. I want to ask them every time if they want to add a new record before doing the process. Following is my code.

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Assign11 {

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

        Scanner keyboard = new Scanner (System.in);

        System.out.println("enter FILE name");
        String FileName = keyboard.nextLine();

        FileWriter fwriter = new FileWriter(FileName);
        PrintWriter StudentFile = new PrintWriter(fwriter);

        String name = "";
        int age = 0;
        double gpa = 0.0;
        String answer = "";

        do {
                System.out.println("Enter name.");
                name = keyboard.nextLine();

                System.out.println("Enter age.");
                age = keyboard.nextInt();

                keyboard.nextLine();

                System.out.println("Enter GPA.");
                gpa = keyboard.nextDouble();

                StudentFile.println (name);
                StudentFile.println (age);
                StudentFile.println (gpa);

                System.out.println("Do you wish to enter a new record? "
                            + "Type 'y' or 'n'.");
                answer = keyboard.nextLine();

        } 
        while (answer.equalsIgnoreCase("y"));

        StudentFile.close();
        System.exit(0);
    }
}

But the problem is that the question of adding a new record doesn't asked to user. So I was wondering what I did wrong and how I could fix it.

Vimal
  • 420
  • 6
  • 28
  • Can you try and add `keyboard.nextLine();` right after `gpa = keyboard.nextDouble();` just like you did after `age = keyboard.nextInt();` and then try again. – whatamidoingwithmylife Nov 01 '19 at 07:50
  • 2
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Progman Nov 01 '19 at 07:58

1 Answers1

0

Here is quick fix for you. Please check following code.

You need to use next() in place of nextLine().

    public static void main(String arg[]) {
    Scanner keyboard = new Scanner (System.in);
    System.out.println("enter FILE name");
    String FileName = keyboard.nextLine();
    try{
        FileWriter fwriter = new FileWriter(FileName);
        PrintWriter StudentFile = new PrintWriter(fwriter);
        String name = "";
        int age = 0;
        double gpa = 0.0;
        String answer = "";
            do {
                System.out.println("Enter name.");
                name = keyboard.next();

                System.out.println("Enter age.");
                age = keyboard.nextInt();

                System.out.println("Enter GPA.");
                gpa = keyboard.nextDouble();

                StudentFile.println (name);
                StudentFile.println (age);
                StudentFile.println (gpa);

                System.out.println("Do you wish to enter a new record? Type 'y' or 'n'.");
                answer = keyboard.next();
            }
            while (answer.equalsIgnoreCase("y"));

            StudentFile.close();
            System.exit(0);
            }catch(IOException ex){
                ex.printStackTrace();
            }finally {
                keyboard.close();
            }
        }

Hope this solution works.

Vimal
  • 420
  • 6
  • 28