-2

I am trying to import a web file containing a numbers that correspond to vertices for a graph. The first line states how many vertices there are. So i defined an int verticesAmount to correspond to how many vertices there are. After that I started a for loop to see catch the input of each line in the file and then put each number in an array by splitting them using the .split(" ") method. Everything up to this point compiles fine and when I print out the array I just made it prints out fine. However, when I try to cast each element of the array to an INT i get a compiler error and I cannot figure out why. Any help would be appreciated. Thank You.

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Graph {

public static void main(String[] args)throws Exception{

    ArrayList<ArrayList<Integer>> array;

    Scanner input2 = new Scanner(System.in);
    System.out.println("Enter a URL");
    String urlString = input2.next();

    java.net.URL url = new java.net.URL(urlString);
    Scanner input = new Scanner(url.openStream());

    String verticesAmount = input.next();
    int amount = Integer.parseInt(verticesAmount);
    String[] stringArray = new String[10];
    ArrayList<Integer> row = new ArrayList<Integer>();

    String line;

    for(int i = 0; i < amount; i++){
        line = input.nextLine();
        stringArray = line.split(" ");
        for(int j = 0; j < stringArray.length; j++)
            row.add(Integer.parseInt(stringArray[j]));
seiko149
  • 19
  • 6
  • 1
    In your question you say "when I try to _cast_" but I don't see any casts in your code. Are you referring to `parseInt` here? Are you seeing an error at `parseInt`? – Ray Toal Jan 04 '14 at 08:01
  • This is the error I get:Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Graph.main(Graph.java:29) – seiko149 Jan 04 '14 at 08:03
  • I see. Your numbers are not separated by exactly one space. I can give an answer. – Ray Toal Jan 04 '14 at 08:04
  • Yes I guess I got the two terms confuse. I am trying to parse each element of the String array into an int – seiko149 Jan 04 '14 at 08:05

3 Answers3

3

The problem is that you are asking to split your line using exactly one space as the split character.

If you were to split a line like this:

SPACE 5 SPACE SPACE 4

on a single space, your resulting array would be

[ "", '5', "", '4' ]

If you run through each string in this array calling parseInt then you will get exceptions on the empty strings.

One quick and dirty approach is to skip empty strings. This can help you if you have a completely blank line, too.

It is common in these kind of situations for your split call to be

line.trim().split("\\s+")

which removes leading and trailing whitespace and also splits across runs of multiple spaces. Either way, do be sure to skip over blank lines.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0

As per the comments and sample code given, you are getting an NumberFormatException because your line.split(" ") may contain any non-numeric data. Please check your intput before calling parseInt() method

UVM
  • 9,776
  • 6
  • 41
  • 66
0

One certain way to get numbers and nothing but numbers is to use the regular expression "\\d+" and go through the line with Matcher#find(). Since you are dealing only with integers, this will work. The regex will change depending on what you want (e.g. "\\d+\.\\d+" for decimals, etc.).

Using this technique will protect you against multiple spaces, tabs, a mistaken non-digit character ... pretty much anything, really.

Chthonic Project
  • 8,216
  • 1
  • 43
  • 92