开发者

Killing this NumberFormatException

I'm getting a NumberFormatException on the Integer.parseInt() method. I know this exception is produced when something like "ab" is passed to the method, but I'm at a loss finding where this is happening. How can I fix this?

I'm using Netbeans and trying to debug putting a watch on the caseStartLineSplitted[0] variable and then hitting f7, but the code goes through things like the Arrays class, which I don't care about. How can I make it go straight to where caseStartLineSplitted[0]gets changed?

The input file is:

2
3 2 1
ab
1 0
2 0
2 0
2
0 3
abaa
aab
aba
3 3 2
ade
0 1 2
1 2 0
2 1 0
1 2
2 2
a
de



 /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package afd;

    import java.io.*;
import java.util.*;

/**
 *
 * @author Administrator
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here

        FileReader fr = new FileReader("E://Documents and Settings//Administrator//My Documents//NetBeansProjects//AFD//src//afd//dfa.in");


        BufferedReader br = new BufferedReader(fr);

        String firstLine= br.readLine();


        String [] firstLineSplitted = firstLine.split(" ");

        /*debug*/
        System.out.println(firstLine);

        int numberOfTestCases = Integer.parseInt(firstLine);

        for (int indexOfTestCases =0; indexOfTestCases < numberOfTestCases; indexOfTestCases++  ){

            String caseStartLine = br.readLine();

            /*debug*/
            System.out.println(caseStartLine);
            String [] caseStartLineSplitted = caseStartLine.split("");

            int numberOfStates = Integer.parseInt(caseStartLineSplitted[0]);

            int numberOfAlphabetSymbols = Integer.parseInt(caseStartLineSplitted[1]);

            //int numberOfFinalStates = Integer.parseInt(caseStartLineSplitted[2]);

            String alphabetLine = br.readLi开发者_如何学JAVAne();

            for (int indexOfStates = 0; indexOfStates < numberOfStates; indexOfStates++){

                  String ijLine = br.readLine();
                  String [] ijLineSplitted = ijLine.split(" ");

                  int i = Integer.parseInt(ijLineSplitted[0]);
                  int j = Integer.parseInt(ijLineSplitted[1]);


            }

            String finalStatesLine = br.readLine();
            String finalStatesLineSplitted [] = finalStatesLine.split(" ");
            ArrayList<Integer> finalStates = new ArrayList<Integer>();

            for (int conversionIndex =0; conversionIndex < finalStatesLineSplitted.length; )

            }

         }
        }


I think your mistake is a missing space on this line:

String[] caseStartLineSplitted = caseStartLine.split(" ");

You wrote:

String[] caseStartLineSplitted = caseStartLine.split("");

When I change that i get the following output, with no errors:

2
3 2 1
0 3


Why dont you surround the entire block with a try/catch NumberFormatException and handle it that way? You don't need to be granular, because if there are letters anywhere in a line, that line is invalid and you can move on.


Try using breakpoints starting at

String [] caseStartLineSplitted = caseStartLine.split("");

and using Step Over (F8)


You could also make use of Scanner(String) instead of split() and then use hasNext(), hasNextInt() and nextInt().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜