输入字符串的java.lang.NumberFormatException

时间:2014-12-08 17:21:31

标签: java string

我正在创建一个程序,该程序将给定的输入字符串转换为数字,以便对输入进行编码。但是一旦输入字符串变得太长,我就会遇到NumberFormatException。我无法理解如何解决这个问题。

请注意,我必须从给定的字符串输入中获取子字符串,将它们转换为numericValues,然后将这两个字符串的总和作为答案。

代码:

public class Puzzle {


private static char[] letters =  {'a','b','c','d','e','f','g','h','i', 'j','k','l','m','n','o','p','q','r','s',
    't','u','v','w','x','y','z'};
private static String input;
private static String delimiters = "\\s+|\\+|//+|=";
public static void main(String[]args)
{
    input = "youuu + are = gay"; //as soon as the substrings before = sign are 
//longer than 5 characters the exception occurs

    System.out.println(putValues(input));
}

//method to put numeric values for substring from input
@SuppressWarnings("static-access")
public static long putValues(String input)
{

    Integer count = 0;
    long answer = 0;
    String first="";
    String second = "";
    StringBuffer sb = new StringBuffer(input);
    int wordCounter = Countwords();
    String[] words = countLetters();
    System.out.println(input);  

        if(input.isEmpty())
        {
            System.out.println("Sisestage mingi s6na");
        }
        if(wordCounter == -1 ||countLetters().length < 1){

            return -1;
        }
        for(Character s : input.toCharArray())
        {

         for(Character c : letters)
         {               
             if(s.equals(c))
             {
                count = c.getNumericValue(c);

                System.out.print(s.toUpperCase(s) +"="+ count + ", ");

             }

         }
         if(words[0].contains(s.toString()))
         {
             count = count - 1;
            count = s.getNumericValue(s);
            //System.out.println(count);
            first += count.toString();              

         }
         if(words[3].contains(s.toString())){

             count = s.getNumericValue(s);
             second += count.toString();
         }

        }
        try {

        answer = Integer.parseInt(first) + Integer.parseInt(second);
        } catch(NumberFormatException ex)
        {
            System.out.println(ex);
        }

        System.out.println("\n" + first + " + " + second + " = " + answer);


    return answer;

}
public static int Countwords()
{
    String[] countWords = input.split(" ");
    int counter = countWords.length - 2;
    if(counter == 0) {
    System.out.println("Sisend puudu!");
    return -1;
    }
    if(counter > 1 && counter < 3) {
        System.out.println("3 sõna peab olema");
        return -1;
    }
    if(counter > 3) {
    System.out.println("3 sõna max!");
    return -1;
    }
    return counter;
}

//method which splits input String and returns it as an Array so i can put numeric values after in the
//putValue method
public static String[] countLetters()
{   

    int counter = 0;
    String[] words = input.split(delimiters);       
    for(int i = 0; i < words.length;i++) {

            counter = words[i].length();

         if(words[i].length() > 18) {
            System.out.println("Yhe s6na maksimaalne pikkus on 18 t2hem2rki ");             
        }           
    }                                           
        return words;           
}

1 个答案:

答案 0 :(得分:2)

Java中的整数(与许多语言一样)受最小值和最大值的限制。

有关这方面的更多信息,请访问:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

你可以在catch-block中给出一个有意义的错误

  

您没有输入有效的32位整数值。

或者您可以切换到BigDecimal这样可以容纳更大值的内容:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

(注意:BigDecimal与普通int的工作方式非常不同,因此请仔细阅读文档,并在必要时阅读Google的示例。

编辑:如果您需要,也可以将其解析为:Long.parseLong(INPUT, 10);。这样你就可以将限制扩展到64位。