2016-08-02 3 views
-1

이 코드는 숫자가 회문인지 여부를 확인합니다. Palindrome 검사 방법을 실행할 때마다 java.lang.StringOutOfBoundsException이 나타납니다.java.lang.StringOutOfBoundsException을 인식 할 수 없습니다.

도와주세요. 당신이없는 문자열에서 문자를 선택 charAt를 사용할 때

import java.util.*; 

/** 
* Lab 1 . 
* @author Kevin Rasquinha 
* @version 30 July 2016 
*/ 
public class Lab1 
{ 
    private Scanner scan = new Scanner(System.in); 

    /** 
    * count the number of digits in a number 
    * @param num the number to analyse 
    * @return the number of digits it has 
    */ 
    public int numDigits (int num) 
    { 
     int nDigits = 0; 
     int digit; 
     while (num>0) 
     { 
      digit = num % 10;  // take off the last digit 
      num = num /10;   // reduce the number 
      nDigits = nDigits + 1; // increment count of digits 
     } 
     return nDigits; 
    } 

    /** 
    * Read a number from the keyboard, and report how many digits it has. 
    * Ensure the number is within a desired range. 
    */ 
    public void countDigits() 
    { 
     int num=0; 
     while (num<1 || num > 1000) 
     { 
      System.out.print("What number (1 to 1000)? "); 
      num = scan.nextInt(); 
      scan.nextLine(); 
      System.out.println("Num = " + num); 
      if (num<1 || num > 1000) 
       System.out.println("1 to 1000, please"); 
     } 
     System.out.println (num + " has " + numDigits(num) + " digits"); 
    } 

    /** 
    * method used to if a number is the sum of the cube of its digits 
    * @param args (not used) 
    */ 
    public void sumCubesDigits() 
    { 
     for (int initial = 1; initial < 1000; initial ++) 
     { 
      int num = initial; 
      int thirddig = num%10; 
      num = num/10; 
      int secdig = num%10; 
      num = num/10; 
      int firstdig = num%10; 
      int sum = (thirddig*thirddig*thirddig) + (secdig*secdig*secdig) + (firstdig*firstdig*firstdig); 
      if (sum == initial) 
      { 
       System.out.println ("The number " + initial + " is equal to the sum of the cube of its digits."); 
      } 
     } 
    } 

    /** 
    * Recieves an int and writes the same int backwards 
    * @param args (not used) 
    */ 
    public int backwards(int num) 
    { 
     int rev = 0; 
     int value = num; 
     while (value != 0) 
     { 
      rev = rev*10; 
      rev = rev + value%10; 
      value /= 10; 
     } 
     return rev; 
    } 

    /** 
    * Receives digit from method backwards 
    * Asses whether backwards = the original number 
    * @param args (not used) 
    */ 
    public boolean palindrome (int num, int digit) 
    { 
     boolean a = false; 
     int normal = num; 
     int reversed = digit; 
     if(reversed == digit) 
     { 
      a = true; 
     } 
     return a; 
    } 

    /** 
    * Recieves int from user - num 
    * Sends num to backwards 
    * Sends num to palindrome 
    * Recieves boolean from palindrome 
    * Outputs message to user 
    */ 
    public void checkPalindrome() 
    { 
     System.out.println ("Enter number to be see if it is a palindrome"); 
     int num = scan.nextInt(); 
     int digit = backwards(num); 
     boolean check = palindrome (num, digit); 
     if (check = true) 
     { 
      System.out.println("Your number is a palindrome"); 
     } 
    } 

    /** 
    * Present a menu to the user, and obtain their selection. If they 
    * type an erroneous value, report it and try again. Either upper 
    * case or lower case input is accepted. 
    * @return an upper case character showing the user's choice 
    */ 

    public char menuChoice() 
    { 
     System.out.println(""); 
     System.out.println("What do you want to do?"); 
     System.out.println("(c) Count the digits in a number"); 
     System.out.println("(g) Find out the numbers where the the sum of the cube of its digits is equal to it"); 
     System.out.println("(p) Find out if a number is a palindrome"); 
     System.out.println("(q) Quit"); 
     System.out.print("Your choice? "); 
     char answer = ' '; 
     boolean ok = false; 
     while (! ok) 
     { 
      answer = scan.nextLine().trim().toUpperCase().charAt(0); 
      ok = (answer == 'C' || answer == 'Q' || answer == 'G' || answer == 'P'); 
      if (! ok) 
      { 
       System.out.println("Please type one of c,C,q,Q,g,G,p,P"); 
       System.out.print("Your choice? "); 
      } 
     } 
     return answer; 
    } 

    /** 
    * test driver for the program 
    */ 
    public void test() 
    { 
     char answer = ' '; 
     while (answer != 'Q') 
     { 
      answer = menuChoice(); 
      switch (answer) 
      { 
       case 'C': countDigits(); break; 
       case 'G': sumCubesDigits(); break; 
       case 'P': checkPalindrome();break; 
       case 'Q': break; 
      } 
     } 
    } 

     /** 
    * main program: create a test driver and let it loose 
    * @param args (not used) 
    */ 
    public static void main (String [] args) 
    { 
     Lab1 l1 = new Lab1(); 
     l1.test(); 
    } 
} 
+0

answer = scan.nextLine(); if(answer != null) answer = answer.trim().toUpperCase().charAt(0);에 변경해야합니다. 'if (check)'를 의미합니다. –

+1

여기에는 'sumCubesDigit'와 같이 모든 관련성없는 코드가 있습니다. 발생한 오류와 관련없는 모든 것을 제거하고 스택 추적을 게시하십시오. –

+0

'palindrome' 메소드의 중간에는'int reversed = digit;'뒤에'if (reversed == digit)'가옵니다. 그게 진짜로 당신이 쓸 의도였습니까? 그것은 항상 사실 일 것입니다. –

답변

0

StringIndexOutOfBoundsException 주로 발생합니다.

나는 문제가 여기에있다 같아요

answer = scan.nextLine().trim().toUpperCase().charAt(0); 

을 그 코드에서 유일한 charAt 때문이다.

사용자가 아무 것도 입력하지 않으면 해당 행은 어떻게됩니까? 물론 Exception을 던져라!

0

당신은 if (check = true)if (check == true)에와 answer = scan.nextLine().trim().toUpperCase().charAt(0); 그것은 당신의 예외의 원인은 아니지만, (= 사실 확인) 경우``할당하지를 비교