Juuri Peeter
Juuri Peeter

Reputation: 131

java.lang.NumberFormatException for input string

I'm creating a program which makes the given input string into number so that input will be coded. But I'm running into a NumberFormatException as soon as the input string gets too long. I can't see how I can fix this.

Note that I have to get substrings from the given string input, turn them into numericValues then get the sum of these two strings as an answer.

Code:

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

Upvotes: 0

Views: 2560

Answers (1)

jabbink
jabbink

Reputation: 1291

Integers in Java (as in many languages) are limited by a minimum and maximum value.

More information on this can be found here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

You could give a meaningful error in the catch-block

You did not enter a valid 32-bit Integer value.

Or you could switch to something like a BigDecimal which can hold bigger values: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

(watch out: BigDecimal works very different from a normal int, so read the documentation wisely, and Google for examples if necessary)

EDIT: you can parse it to Long as well, if you want that: Long.parseLong(INPUT, 10);. That way you extend the limit to 64-bit.

Upvotes: 2

Related Questions