fork download
  1. import java.util.HashMap;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.util.Scanner;
  7.  
  8. class Foo {
  9. Path file;
  10.  
  11. public Foo(Path file) {
  12. this.file = file;
  13. }
  14.  
  15. public Boolean baz(String arg) {
  16. Boolean b = false;
  17. try {
  18. b = Files.lines(this.file).anyMatch(s -> s.equals(arg));
  19. } catch(IOException ioex) {
  20. ioex.printStackTrace();
  21. }
  22. return b;
  23. }
  24. }
  25.  
  26. public class Main {
  27. public static void main(String[] args) {
  28. HashMap<Boolean, String> msg = new HashMap<Boolean, String>();
  29. msg.put(true, "入場できます");
  30. msg.put(false, "入場できません");
  31. Path file = Paths.get("name.txt");
  32. Foo bar = new Foo(file);
  33. System.out.println("名前を入力してください");
  34. Scanner scan = new Scanner(System.in);
  35. String str = scan.next();
  36. System.out.println(msg.get(bar.baz(str)));
  37. }
  38. }
  39.  
Runtime error #stdin #stdout #stderr 0.13s 49312KB
stdin
Standard input is empty
stdout
名前を入力してください
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1478)
	at Main.main(Main.java:35)