Skip to content

Commit

Permalink
Change Scanner access modification
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman3455 committed Aug 4, 2024
1 parent 4f3b15e commit b253410
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions app/src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
import java.util.Scanner;

public class App {
protected static Scanner scanner = new Scanner(System.in);
private static Scanner scanner = new Scanner(System.in);

public static String getUserInput() {
return scanner.next();
}

public static void closeScanner() {
scanner.close();
}

public static void main(String[] args) {
System.out.print("""
Expand All @@ -23,7 +31,7 @@ public static void main(String[] args) {
0-Exit
Your choise:\s""");

String selection = scanner.next();
String selection = getUserInput();

switch (selection) {
case "1":
Expand All @@ -50,6 +58,6 @@ public static void main(String[] args) {
System.out.println("Invalid value, try again");
}

scanner.close();
closeScanner();
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/hexlet/code/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Cli {
public static String greetings() {
System.out.print("\nWelcome to the Brain Games!\nMay I have your name? ");
String userName = App.scanner.next();
String userName = App.getUserInput();
System.out.println("Hello, " + userName + "!");

return userName;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/hexlet/code/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static void run(String[][] questionsAndAnswers, String ruleMessage) {
while (countCorrectAnswers < MAX_WIN) {
System.out.println("Question: " + questionsAndAnswers[0][countCorrectAnswers]);
System.out.print("Your answer: ");
String userAnswer = App.scanner.next();
String userAnswer = App.getUserInput();

if (!userAnswer.equalsIgnoreCase(questionsAndAnswers[1][countCorrectAnswers])) {
System.out.println("'" + userAnswer + "' is wrong answer ;(. Correct answer was '"
Expand Down

0 comments on commit b253410

Please sign in to comment.