Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default getting integers from a string

    I need some help trying to figure out the best approach to getting all the integers out of a string. The string has an undetermined number of integers, each separated by a single space. I am basically readingl in a file, and i need to put the integers into a 2 dimensional array, based on what line they are in. For instance, my text file would be:

    3 5
    6 19 3



    so that is a 2 line file, and i wanted something like myArray[0][1] = 5.

    I am sucesfully reading the file in, but im not getting the desired output. Here is what I have. I keep getting an 'error: null'. My basic approach was to read each character, and if the next character was not a ' ', keep appending to the current value. Once we reach a space (start of new character), move to the next position in the array. Any ideas, am I taking the right approach?


    //Read File Line By Line
    while ((strLine = br.readLine()) != null) {



    for(int pos=0; pos<strLine.length(); pos++){

    while(strLine.charAt(pos)!=' '){
    // MatrixVals[row][pos] = MatrixVals[row][pos] + Character.toString(strLine.charAt(pos));
    System.out.println(MatrixVals[row][pos]);
    pos++;
    }

    }

    row++;
    }



    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Use a Scanner and the nextInt method.
    or
    Read an entire line and use the String.split method.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    thanks for the replies, I have implemented a scanner:

    here is a part of the file, but now its saying Exception in thread "main" java.util.NoSuchElementException: No line found. The file is just 1 line that says "3 4"....

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {

    String line = scanner.nextLine();
    LineData[LC] = scanner.nextLine();
    System.out.println(line);
    LC++;
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    String line = scanner.nextLine();
    LineData[LC] = scanner.nextLine();
    You say the file only has one line and yet you try to read two lines. I wonder why you get that error.

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    Ok, I fixed that, I want the current line to be set = to the array (LC) at position whatever line we are on. I'm getting a null pointer. I would guess that at the first pass, .hasNextLine() is pointing to the first line, correct?

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {


    LineData[LC] = scanner.nextLine();
    System.out.println(LineData[LC]);
    LC++;
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    }
    }

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    So you get a null pointer exception. The error message tells you which line the error occurs on. Look at that line and determine what could be null. Use some print statements before this line to find out exactly what is null. Investigate why it is null.

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    ok, I think im almost there:

    The split method gives me an array of length (how ever many sections there are), but its a string array. since I will be manipulating these integers, im trying to convert that array to a string array, and then iterate over the whole file. Im getting a null pointer on

    intLineData[i] = Integer.parseInt(linedata[i]);

    This does not make sense to me since intLineData[0] should be the first number in my file.




    String [] linedata = null;
    String[][] matrixData = null;
    int[] intLineData = null;
    int LC = 0;
    try {
    //
    // Create a new Scanner object which will read the data
    // from the file passed in. To check if there are more
    // line to read from it we check by calling the
    // scanner.hasNextLine() method. We then read line one
    // by one till all line is read.
    //
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    linedata = line.split(" ");
    for(int i=0; i<linedata.length; i++){
    intLineData[i] = Integer.parseInt(linedata[i]);
    matrixData[LC][i] = linedata[i];
    System.out.println(matrixData[LC][i]);
    }
    LC++;
    }

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    int[] intLineData = null;
    // other code
    intLineData[i] = Integer.parseInt(linedata[i]);
    No where in the "other code" bit do I see you create the intLinedata array.

  10. #10
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    well, i did set it to null (up above I put int[] intLineData = null;

    and then i basically did a manual array copy, with the difference being I converted each element to an integer.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You did no such thing. As you said you initialise intLinedata to null and it never changes from null. Blantant hint: where's the intLineData = new whatever line of code? Huh? Huh?

  12. #12
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    thats what this seems to suggest:

    Java String Split Example | Java Examples - Java Program Sample Source Code

    for instance If I had myString = "3 4 5 6",

    when I set myArray = to myString.split(" "), that it will put myArray[0] = 3, myArray[1]=4, etc.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Yes, the split method returns an array of Strings but to what variable do you assign that array? Not the same one that is null.

  14. #14
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    well I set it to null just to initialize it.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Dude are you yanking my chain!

    Seriously think!

    You have two variables:
    lineDate which gets the returned value (a String array) from the split method.
    intLineData WHICH IS NULL WHICH IS NULL WHICH IS NULL.

    If i shout it enough times will that help you to understand.

  16. #16
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    lol...I'm still not understanding..
    YES, it is null when I initialize it, but once I iterate over the lineData array and set each element in it = to its integer value at the same index in the INTLINEDATA array, its no longer null.....right? I cant initially set intLineData to anything but null, because I don't know how big it is until I go through the for loop with the lineData array......what should I initialize it to?

  17. #17
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    should I do something like

    int[] intLineData = new int[linedata.length];

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Did you try it?

  19. #19
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    a little better, now the null pointer is on
    matrixData[LC][i] = intLineData[i];

    String [] linedata = null;
    int[][] matrixData = null;

    int LC = 0;
    try {
    //
    // Create a new Scanner object which will read the data
    // from the file passed in. To check if there are more
    // line to read from it we check by calling the
    // scanner.hasNextLine() method. We then read line one
    // by one till all line is read.
    //
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    linedata = line.split(" ");
    int[] intLineData = new int[linedata.length];
    for(int i=0; i<linedata.length; i++){
    intLineData[i] = Integer.parseInt(linedata[i]);
    matrixData[LC][i] = intLineData[i];
    System.out.println(matrixData[LC][i]);
    }
    LC++;
    }

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    }

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Are you fargin kidding me?
    Do we have to go through this whole process again?

    You have the same problem, matrixData is null. come on. Time to think for yourself.

Page 1 of 2 12 LastLast

Similar Threads

  1. Sum of Integers - From File
    By Cubba27 in forum New To Java
    Replies: 8
    Last Post: 03-25-2010, 12:45 PM
  2. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 11:29 AM
  3. Integers and Lists
    By TGH in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 10:49 AM
  4. Multiples Integers from a String
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 04-01-2009, 01:00 AM
  5. how to get the Integers out of a String
    By JordashTalon in forum New To Java
    Replies: 10
    Last Post: 01-30-2009, 07:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •