2017-01-07 1 views
0

안녕하세요 저는 HomeTeam, AwayTeam, HomeScore, Awayscore로 4 개의 배열로 분할 된 축구 테이블에 대한 사용자 입력을 수집하는 프로그램을 만들어야하는 프로젝트를 수행하고 있습니다. 그런 다음 특정 형식으로 제공됩니다. Iv는이 기본 섹션을 완료했지만 사용자가 홈 팀과 원정 팀에 숫자를 입력하지 못하도록하고 또한 빈 공간으로 아무것도 입력하지 못하도록 예방할 필요가 있습니다.배열의 Java 유효성 검사

String[] hometeam = new String[100]; 
    String[] awayteam = new String[100]; 
    int[] homescore = new int[100]; 
    int[] awayscore = new int[100]; 
    int games = 0; 
    String amount = ""; 
    boolean stop = true; 
    Scanner keyboard = new Scanner(System.in); 

    System.out.println("Please enter the amount of games you wish to list. Type stop to end program and show results"); 

    amount = keyboard.nextLine(); 
    int amountconvert = Integer.parseInt(amount); 
    String input = ""; 

    if (amountconvert > 100) { 
     System.out.println("The maximum amount of games you can add is 100. Please try again"); 
     return; 
    } 

    do { 
     System.out.println("Enter the HomeTeam:AwayTeam:HomeScore:AwayScore "); 

     input = keyboard.nextLine(); 
     if (input.equalsIgnoreCase("stop")) { 
      break; 
     } 

     String[] inputindex = input.split(":"); 
     if (Integer.parseInt(inputindex[2]) < 0 || Integer.parseInt(inputindex[3]) < 0) { 
      System.out.println("Errors found, Please format as shown"); 
     } else if (inputindex.length == 4) { 
      hometeam[games] = inputindex[0]; 
      awayteam[games] = inputindex[1]; 
      homescore[games] = Integer.parseInt(inputindex[2]); 
      awayscore[games] = Integer.parseInt(inputindex[3]); 
      games++; 
     } 
    } while (games < amountconvert); 

    keyboard.close(); 
    System.out.println("*******************************************************************"); 

    for (int index = 0; index < games; index++) { 
     System.out.println(" " + hometeam[index] + " " + "[" + homescore[index] + "]" + " | " + awayteam[index] 
       + " " + "[" + awayscore[index] + "]"); 
    } 
    System.out.println("*******************************************************************"); 

저는 초보자이므로 도움을받을 수 있습니다. iv는 온라인으로 보았고 그것을 할 수있는 대부분의 방법은 try/catch와 while 루프이지만 그들이 필요로하는 위치와 루프를 만드는 방법을 파악할 수 없다.

답변

0

나는 당신이 원하는 것은 루프를 만드는 것이라고 생각한다. 스캐너가 입력을 스캔하는 지점 주변에서 입력에 대해 테스트 한 다음 일부 조건이 충족되지 않으면 (입력이 유효하지 않은 경우) 다시 시작합니다. 이런 식으로 뭔가 루프 동안 할 일을 대체 할 수있는 :

do { 
    System.out.println("Enter the HomeTeam:AwayTeam:HomeScore:AwayScore "); 
    boolean stopped = false; // flag to be activated if the user entered stop 
    while (true) { // loop to keep getting an input until it is valid 
    input = keyboard.nextLine(); 
    if (input.equalsIgnoreCase("stop")) { 
     stopped = true; 
     break; 
    } 
    String[] inputIndex = inpit.split(":"); 
    if (/*Test anything that must be true in order for the input to be valid*/) { 
     // do some actions now that the input is correct 
     break; // Exit the input loop if the input checks out 
    } 
    else { // if the input is invalid 
     System.out.println("Invalid input. Please use the correct format."); 
    } 
    } 
    if (stopped) { // break out of the outer loop if the user entered stop 
    break; 
    } 
} while (/*Condition to keep adding more entries to the array*/); 
0

확인을 위해 입력하면 자바 정규 표현식을 사용하여 정확합니다. https://docs.oracle.com/javase/tutorial/essential/regex/

: 여기

public static boolean isNumeric(String str){ 
    return str.matches("-?\\d+(\\.\\d+)?"); 
} 

당신이 정규 표현식에 대한 자세한 배울 수있다 : 예를 들어, 다음 함수는 문자열이 숫자 여부가 있는지 여부를 확인