-3
public class CaesarCipher {
    public static void main(String[] args) {
        String answer = "";

        do {
            String Phrase = "";
            Scanner scan = new Scanner(System.in);
            System.out.println("Type in a phrase");
            String phrase = scan.nextLine();
            System.out.println("Your Phrase was " + phrase);

            for (int i = 0; i < phrase.length(); i++) {
                char charAt = phrase.charAt(i);
                System.out.println("The " + (i) + " character is " + charAt);
            }
            System.out.print("Would you like to try again? (Yes/No).");
            answer = scan.next();
            scan.close();
        } while (answer.equals("Yes"));

        System.out.println("Thank you, have a nice day.");
    }
}

I can get this to go through and even end correctly when the user responds "no", but every time I say yes and re run it. The program crashes after it asks for another input ("type in a phrase"). It says that there is nothing for the scanner to scan even though it hasn't given me an opportunity to enter anything.

David Conrad
  • 12,745
  • 1
  • 37
  • 46
Devon
  • 1
  • 3
    `scan.close();` - this closes System.in. – user253751 Feb 25 '15 at 00:43
  • 1
    ^ and `answer = scan.next();` probably should be `answer = scan.nextLine();` – MadProgrammer Feb 25 '15 at 00:44
  • Also, I am a very novice programmer so any tips would be helpful. – Devon Feb 25 '15 at 00:50
  • It looks like that was what was wrong, I thought by reopening my scanner at the beginning of the do loop it would still work. How come this isn't so @immibis – Devon Feb 25 '15 at 00:51
  • 1
    Because System.in is still closed. (So is your old scanner, but that's irrelevant since you created a new one) – user253751 Feb 25 '15 at 00:52
  • You don't want to close anything you didn't open. If you opened it, it's your responsibility to close it. But you didn't open `System.in`. (If you were going to close it, the `scan.close()` should be in a `finally` block or you should use try-with-resources.) – David Conrad Feb 25 '15 at 00:57
  • Thank you all for the quick responses! – Devon Feb 25 '15 at 01:07

2 Answers2

-1

Your problem is simple you don't have to call scan.close(). In fact you can read from the Scanner class definition: "When a Scanner is closed, it will close its input source if the source implements the Closeable interface."

Paul
  • 148
  • 1
  • 11
-1

public class CaesarCipher {

public static void main(String[] args) {

String answer = "";


do {
    String Phrase = "";
    Scanner scan = new Scanner(System.in);
    System.out.println("Type in a phrase");
    String phrase = scan.nextLine();
    System.out.println("Your Phrase was " + phrase);

    for (int i = 0; i < phrase.length(); i++) {
        char charAt = phrase.charAt(i);
        System.out.println("The " + (i) + " character is " + charAt);

    }
    System.out.print("Would you like to try again? (Yes/No).");
    answer = scan.next();

} while (answer.equals("Yes"));
System.out.println("Thank you, have a nice day.");

}

Devon
  • 1