2014-02-12 1 views
0

전화 번호를 뒤집고 모든 구분 기호를 제거하고 원래 번호와 비교하여 그것이 회문인지 확인하는 프로그램을 작성 중입니다. 또한 전화 번호를 쉼표로 정수로 변환합니다. 몇 가지 문제가 있습니다. 나는 오류가 발생하고 이유를 모릅니다. 또한 번호가 회문인지 아닌지를 정확하게 판별하지 않습니다. 어떤 도움을 주시면 감사하겠습니다.문자열 토큰 화기 및 버퍼 사용 방법.

//Phone String Palindrome Conversions 
//This program will turn a phone number around and check to see if it is a palindrome 
//This program will remove all deasdspace and symbols from the phone number 
//This program will reverse the string and compare it to the original 
//This program will put a phone number in a long intiger format 
import java.io.*; 
import java.util.*; 
import java.text.DecimalFormat; 
public class DottinoN_palindrome 
{ 
    public static void main (String [] args) throws IOException 
{ 
    String phoneshort1; 
    boolean pal; 
BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Please enter your phone number"); 
String phone1=br.readLine(); 
phoneshort1 = spaceremover(phone1); 
System.out.println(phoneshort1); 
pal = palindrometest(phoneshort1); 
System.out.println(pal); 
if(pal = false) 
{ 
    System.out.println("Your phone number is a palindrome!"); 
} 
else if(pal = true) 
    System.out.println("Your phone number is not a palendrome..."); 
numberformat(phoneshort1); 

} 
public static String spaceremover (String phone2) 
{ 
String phoneshort = ""; 
StringTokenizer st = new StringTokenizer(phone2,"()- " ,false); 
while(st.hasMoreTokens()) 
{ 
    phoneshort += st.nextToken(); 
} 
return phoneshort; 
} 
public static boolean palindrometest (String phoneshort2) 
{ 
boolean pal; 
StringBuffer br = new StringBuffer(phoneshort2); 
String phonebkwd = br.reverse().toString(); 
if(phonebkwd == phoneshort2) 
{ 
    pal = true; 
} 
else pal = false; 
System.out.println(phonebkwd + "--" + phoneshort2); 
return pal; 

} 
public static void numberformat (String phoneshort2) 
{ 
DecimalFormat formatter = new DecimalFormat("0,000,000,000"); 
int number = Integer.parseInt(phoneshort2); 
System.out.println("Your phone number as an intiger is: " + formatter.format(number)); 
} 
} 
+0

적절한 답을 표시하십시오. – wipindipy10

답변

0

문자열을 비교하기 때문에 같음 메서드를 사용하십시오.

그것을

또한
if(phonebkwd.equals(phoneshort2)) 

로 변경

if(phonebkwd == phoneshort2) 

에서, 당신은 점점 오류를 지정하십시오. 프로그램에서 정말 몇 가지 문제가 있습니다

+0

int number = Integer.parseInt (phoneshort2); 그것의 밑바닥은 프로그램을 추락시키는 원인이되고있다. 이것이 문제가되는 라인인지 또는 내가 가지고있는 다른 것이 작동하지 않는 원인인지 확실하지 않습니다. 감사합니다. – user2933941

+0

무엇이 오류입니까? – wipindipy10

+0

인쇄 상태를 확인하십시오. "전화 번호는 회문입니다!" 그리고 "귀하의 전화 번호는 palendrome 아니 ..."그것은 다른 방법으로 주위에 있어야합니다. – wipindipy10

1

,

  1. wipindipy10로 말했다 동일한 방법으로 변경 문자열 비교

  2. 회문, 그

    if (!pal) { 
        System.out.println("Your phone number is a palindrome!"); 
    } else { 
        System.out.println("Your phone number is not a palendrome..."); 
    } 
    
    에 변화가 있는지 여부를 조건은 인쇄하는 경우
  3. 위에서 언급 한 예외가 바닥 근처에서 발생했습니다. 이렇게하면 입력이는 Integer.MAX_VALUE를 초과 전화, 즉 코드에서 2147483647 [0x7fffffff]

+0

그것은 단지 pal이어야하며! pal가 아니어야합니다. – wipindipy10

+0

우수! 지금 일하고있어. Integer를 Long으로 변경하고! pal와 pal를 사용했습니다. 고맙습니다! – user2933941

0

이 있습니다 많은 오류라고 할 수있다. 문자열 비교 사용을 위해 당신의 문이

if(pal = false) 
{ 
    System.out.println("Your phone number is a palindrome!"); 
} 
else if(pal = true) 
    System.out.println("Your phone number is not a palendrome..."); 

당신이 경우, 다른 블록을 교환한다 잘못되면 다음과 같은 방법

if(phonebkwd.equals(phoneshort2)) 

에게 같습니다. Integer.parseInt (phoneshort2)의 마지막 오류입니다. Integer.MAX_VALUE보다 큰 숫자를 파싱하려고 시도하는 것 같습니다.