【问题标题】:How to use string tokenizers and buffers.如何使用字符串标记器和缓冲区。
【发布时间】:2014-02-12 01:57:58
【问题描述】:

我正在开发一个程序来反转电话号码,删除所有分隔符,并将其与原始号码进行比较以确定它是否是回文。它还将电话号码转换为带逗号的整数。我有几个问题。我收到错误,不知道为什么。此外,它不会正确确定该数字是否为回文。任何帮助将不胜感激。

//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) );
}
}

【问题讨论】:

  • 请标记正确的答案。

标签: java string parsing stringtokenizer stringbuffer


【解决方案1】:

你的程序确实有一些问题,

  1. 正如wipindipy10所说,用equal方法改变字符串比较

  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]

【讨论】:

  • 应该只是 pal 而不是 !pal。
  • 太棒了!它现在正在工作。我将 Integer 更改为 Long,并使用 !pal 和 pal。谢谢!
【解决方案2】:

由于是比较字符串,所以使用equals方法。

来自

    if(phonebkwd == phoneshort2)

改成

    if(phonebkwd.equals(phoneshort2))

另外,请说明您遇到的错误。

【讨论】:

  • int number = Integer.parseInt(phoneshort2);它接近底部并导致程序崩溃。我不确定这是否是问题所在,或者我所拥有的其他东西是否导致它不起作用。谢谢
  • 检查打印“您的电话号码是回文!”的条件和“你的电话号码不是一个palendrome......”应该是相反的。
  • 好的,我这样做了,但有时我仍然会收到错误消息,而且我的电话号码始终是回文。我似乎无法弄清楚为什么。
  • 您的比较。请记住在比较两个值时使用 ==。此外,当您的条件语句为布尔值时,您可以只使用“if(bVal)”表示 true 或使用“if(!bVal)”表示 false。您可以将代码更改为 if...else
  • 如果您的输入少于或等于 10 位,则不会显示该错误。问题:您对允许的位数有限制吗?
【解决方案3】:

代码中有很多错误。 对于字符串比较,使用 equals 方法

if(phonebkwd.equals(phoneshort2))

你下面的 if 语句是错误的

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 , else 块。 Integer.parseInt(phoneshort2); 的最后一个错误似乎是您正在尝试解析大于 Integer.MAX_VALUE 的数字

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 2010-12-28
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2010-09-14
    相关资源
    最近更新 更多