2012-09-05 7 views
1

나는 논리 게이트 동작을 시뮬레이트하는 프로그램을 작성했다. 내 코드와 문제 무엇NoSuchElementFound 내 코드를 실행하는 중 예외가 발생했습니다. 무엇이 잘못 되었습니까?

Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Scanner.java:855) 
    at java.util.Scanner.next(Scanner.java:1478) 
    at java.util.Scanner.next(Scanner.java:1478) 
    at java.util.Scanner.nextInt(Scanner.java:2067) 
    at Operations.chooseOperation(LogicGatesSimulator.java:103) 
    at LogicGatesSimulator.main(LogicGatesSimulator.java:132) 

다음과 같이 내 코드

import java.io.*; 
import java.util.Scanner; 

// This program simulates the logic gates operations for the given input 
class GatesSimulator 
{ 
    int input1, input2, output; 
    boolean boolInput1, boolInput2, boolOutput, validate = true; 

    void simulateAndGate() 
    { 
     getInput(); 
     boolOutput = boolInput1 & boolInput2; 
     showOutput("AND"); 
    } 

    void simulateOrGate() 
    { 
     getInput(); 
     boolOutput = boolInput1 | boolInput2; 
     showOutput("OR"); 
    } 

    void simulateNotGate() 
    { 
     System.out.println("Under Development"); 
    } 

    void simulateXorGate() 
    { 
     getInput(); 
     boolOutput = boolInput1^boolInput2; 
     showOutput("XOR"); 
    } 

    void simulateNandGate() 
    { 
     getInput(); 
     boolOutput = !(boolInput1 & boolInput2); 
     showOutput("NAND"); 
    } 

    void simulateNorGate() 
    { 
     getInput(); 
     boolOutput = !(boolInput1 | boolInput2); 
     showOutput("NOR"); 
    } 

    void getInput() 
    { 

     Scanner simInput = new Scanner(System.in); 

     while (validate) 
     { 

      // Scanner simInput = new Scanner(System.in); 
      System.out.println("First Input: "); 
      input1 = simInput.nextInt(); 

      System.out.println("Second Input: "); 
      input2 = simInput.nextInt(); 
      // simInput.close(); 
      if ((input1 == 1 || input1 == 0) && (input2 == 1 || input2 == 0)) 
      { 
       boolInput1 = (input1 == 1) ? true : false; 
       boolInput2 = (input2 == 1) ? true : false; 
       validate = false; 
      } 
      else 
      { 
       System.out.println("Enter a Valid Input(1/0)"); 
      } 
     } 
     simInput.close(); 
    } 

    void showOutput(String gate) 
    { 

     output = (boolOutput == true) ? 1 : 0; 
     System.out.println(input1 + " " + gate + " " + input2 + " = " + output); 

    } 
} 

class Operations 
{ 
    int choice; 
    GatesSimulator simulator = new GatesSimulator(); 
    boolean contd = true; 

    void chooseOperation() 
    { 

     Scanner input = new Scanner(System.in); 

     while (contd) 
     { 
      System.out 
       .println("Enter Your Choice of Simulation\n\t1 -> AND   Gate\n\t2 -> OR Gate\n\t3 -> NOT Gate\n\t4 -> XOR Gate\n\t5 -> NAND Gate\n\t6 -> NOR Gate\n\t7 -> Exit"); 
      // Scanner input = new Scanner(System.in); 
      choice = input.nextInt(); 
      // input.close(); 
      switch (choice) 
      { 
      case 1: 
       simulator.simulateAndGate(); 
       break; 
      case 2: 
       simulator.simulateOrGate(); 
       break; 
      case 3: 
       simulator.simulateNotGate(); 
       break; 
      case 4: 
       simulator.simulateXorGate(); 
       break; 
      case 5: 
       simulator.simulateNandGate(); 
       break; 
      case 6: 
       simulator.simulateNorGate(); 
       break; 
      case 7: 
       contd = false; 
       break; 
      default: 
       System.out.println("\tEnter A valid Choice(1 to 7)\t"); 
      } 
     } 
     input.close(); 
    } 
} 

public class LogicGatesSimulator 
{ 
    public static void main(String args[]) 
    { 
     Operations gates = new Operations(); 
     System.out.println("\t\t\tLogic gate Simulator\t\t\t"); 
     gates.chooseOperation(); 
    } 
} 

내가 좋아하는 출력을 얻고있다.

+0

내 코드의 실제 문제가 무엇 nextInt()를 호출하기 전에 hasNext()를 사용할 필요가 ?? – rajesh

+0

너는 그것을 잡지 못했다 –

+0

while while this while loop, 'while (contd)'이 영원히 돌아갈거야? 어쩌면 nextInt()'for while (true) '루프에서 "next ints"가 부족하다. –

답변

5

당신은

if(scanner.hasNextInt()) 
     scanner.nextInt(); 
관련 문제