Simple Java program using the same variable for input - does not work

advertisements

All I want to do is use the string variable for all my scanner input.

public static void main(String[] args){
Scanner getInput = new Scanner(System.in);

String defaultInFile = "fileContainingEmails.txt";
String defaultOutFile = "copyPasteMyEmails.txt";

String myInFile;
String myOutFile;

    System.out.print("Enter input filename [default: " + defaultInFile + "]: ");
    //CRUD applications oh yea
    String myInputNom = getInput.nextLine();
    if (myInputNom.equals(""))
    {
        myInFile = defaultInFile;
    }
    else
    {
        myInFile = myInputNom;
    }

    //System.out.println(defaultOutFile); THIS WORKS

    if (myInputNom.equals(""))
    {
        System.out.print("Enter output filename [default: " + defaultOutFile + "]: ");
    }
    else
    {
        System.out.print("Enter output filename [default: " + myInFile + "]: ");
    }

    //System.out.println("'" + myInputNom + "'");        

    myInputNom = getInput.nextLine();

    System.out.println("'" + myInputNom + "'"); 

    if (myInputNom.equals(""))
    {
        myOutFile = defaultOutFile;
    }
    else
    {
        myOutFile = myInputNom;
    }

    System.out.println("Input file: " + myInFile);
    System.out.println("Output file: " + myOutFile);
    }

So what am I doing wrong? The second getInput.nextLine(); acts like it ignores all input.

I expect something in myOutFile, but I get nothing.

Thanks!


Your code works fine here.

% java -cp . foo
Enter input filename [default: fileContainingEmails.txt]: df
Enter output filename [default: df]: dfee
'dfee'
Input file: df
Output file: dfee

It's actually a relief to try to help, only to discover that nothing is broken.