2016-11-19 1 views
0

정수 만 있고 중복되지 않은 클라이언트 ID의 유효성을 검사하고 싶습니다. txt 파일에는 ID, 이름 등과 같은 클라이언트 세부 정보가 들어 있습니다. 공공 무효 loadClientData() {자바에서 txt 스캐너 데이터의 유효성을 검사하는 방법

String fnm="", snm="", pcd=""; 
    int num=0, id=1; 
    try { 
     Scanner scnr = new Scanner(new File("clients.txt")); 
     scnr.useDelimiter("\\s*#\\s*"); 
     //fields delimited by '#' with optional leading and trailing spaces 

     while (scnr.hasNextInt()) { 
      id = scnr.nextInt(); 
      snm = scnr.next(); 
      fnm = scnr.next(); 
      num = scnr.nextInt(); 
      pcd = scnr.next(); 

      //validate here type your own code 

      if(id != scnr.nextInt()) { 
       System.out.println("Invalid input! "); 
       scnr.next(); 
      } 
     //some code... 
     } 
    } 
} 

은 단지 첫 번째 클라이언트에서 인쇄 할 수 있습니다.

도움이 필요하십니까?

또한 이미 유효성이 검사 되었습니까?

은 스캐너 클래스에 대한 Java 문서부터 client.txt

0001# Jones#   Jack#   41# NX4 4XZ# 
0002# McHugh#   Hugh#   62# SS3 3DF# 
0003# Wilson#   Jonny#   10# LO4 4UP# 
0004# Heath#   Edward#   9# LO4 4UQ# 
0005# Castle#   Brian#   15# LO4 4UP# 
0006# Barber#   Tony#   11# LO4 4UQ# 
0007# Nurg#   Fred#   56# NE8 1ST# 
0008# Nurg#   Frieda#   56# NE8 1ST# 
0009# Mouse#   Mickey#   199# DD33 5XY# 
0010# Quite-Contrary# Mary#   34# AB5 9XX# 
+0

clients.txt의 내용을 표시 할 수 있습니까? – vlatkozelka

답변

1

입니다 :

공공 INT nextInt()

스캔 int로 입력의 다음의 토큰 .

폼 nextInt이 방법 (의 호출)에 정확하게 기수이 스캐너 디폴트 기수 인 호출 nextInt (기)와 같은 역할을하는 것이다.

반환 값 : 입력으로부터 스캔 INT 예외 : 스캐너가 Integer 정규 표현식과 일치하지 않는 잘못된 입력을 읽는 경우 함수는 InputMismatchException가 발생합니다

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range 
NoSuchElementException - if input is exhausted 
IllegalStateException - if this scanner is closed 

.

당신은 try - catch 블록을 사용하여 검증을 위해 그 예외를 사용하고 "오류"당신이

당신은 또한 당신의 코드에서 다른 문제가

을 원하는 방식으로 처리 할 수 ​​있습니다. 수정하여 의견에 설명을 넣으십시오.

public static void loadClientData() { 



try { 
     String fnm = "", snm = "", pcd = ""; 
     int num = 0, id = 1; 
     Scanner scnr = new Scanner(new File("clients.txt")); 
     scnr.useDelimiter("\\s*#\\s*"); 
     //fields delimited by '#' with optional leading and trailing spaces 
     //check for hasNext not hasNextInt since the next character might not be an integer 
     while (scnr.hasNext()) { 

      //here happens the integer validation 

      try { 
       id = scnr.nextInt(); 
      } catch (InputMismatchException ex) { 
       System.out.println("Invalid input !"); 
      } 
      System.out.print(id + " "); 
      snm = scnr.next(); 
      System.out.print(snm + " "); 
      fnm = scnr.next(); 
      System.out.print(fnm + " "); 
      num = scnr.nextInt(); 
      System.out.print(num + " "); 
      pcd = scnr.next(); 
      System.out.println(pcd + " "); 

      //you need to do this to get the scanner to jump to the next line 
      if (scnr.hasNextLine()) {      
       scnr.nextLine(); 
      } 
      //validate here type your own code 
      /* if (id != scnr.nextInt()) { 
      System.out.println("Invalid input! "); 
      //scnr.next(); 
      }*/ 

     } 
    } catch (Exception ex) { 

    } 

} 
관련 문제