CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2011
    Posts
    5

    From String array to int array throwing ','and null from text file

    hey every one
    i developed a code to read a text file and put the integers in array but i had a problem to convert the string array to int array throwing any thing else except the imts ,

    this is the content of the text file

    5
    1,10
    8,10
    10,8
    7,4
    3,1
    0,1,10
    1,2,5
    2,3,25
    0,3,3
    3,4,8




    this my code

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



    *
    public class Read {





    public static void main (String[] args) throws FileNotFoundException, IndexOutOfBoundsException , IOException, Exception {

    System.out.println("Enter a filepath to copy from, and one to copy to.");
    Scanner in = new Scanner(System.in);
    String a=in.next();
    File f = new File (a);
    FileInputStream FF = new FileInputStream(f);
    BufferedReader br = new BufferedReader(new InputStreamReader(FF));

    BufferedReader brr = new BufferedReader(new FileReader(a));
    String line;
    int linecount =0 ;
    while ((line = brr.readLine()) != null) {
    linecount++;
    }

    char [] k = null;

    String [] s = null ;
    s = new String[50];

    int [] dd = null ;
    char [] d = null;


    for (int j = 0; j < linecount; j++){
    s[j] = br.readLine();
    }

    FF.close();
    }

    }


    ==========================================================


    i tried with many ways to convert that string array to int array so the int array well be like

    {5,1,10,8,10,10,8,7,4,3,1,0,1,10,1,2,5,2,3,25,0,3,3,3,4,8}
    i used cahrtat method and parseint method but it doesnt work

    Can any one help me with this

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: From String array to int array throwing ','and null from text file

    I used cahrtat method and parseint method but it doesnt work
    What does "doesnt' work" mean?
    If you get errors, please copy and paste the full text of the error messages here.
    Norm

  3. #3
    Join Date
    May 2011
    Posts
    5

    Re: From String array to int array throwing ','and null from text file

    thanks for responding

    this is what i get when i use parseint


    Enter a filepath to copy from, and one to copy to.
    C:\Users\F.A.M\Desktop\GGGGGGGGG\tr.txt
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.parseInt(Integer.java:499)
    at s.Read.convertStringArraytoIntArray(Read.java:20)
    at s.Read.main(Read.java:64)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 7 seconds)


    ===============
    when i use a char array to move every strings to like


    String [] s = null ;
    s = new String[50];

    char [] dd = null ;
    char [] d = null;
    int t=0;

    for (int j = 0; j < linecount; j++){
    s[j] = br.readLine();
    }
    for (int j = 0; j < linecount; j++){
    d=s[j].toCharArray();
    for (int y=0 ; y<=d.length ; y++){
    dd[t]=d[y] ;
    }
    }

    Exception in thread "main" java.lang.NullPointerException

    or

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: From String array to int array throwing ','and null from text file

    You should look at the String before trying to convert it to an int.
    The error message says that the String was empty. For input string: ""

    Exception in thread "main" java.lang.NullPointerException

    or

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    Post the FULL text of error messages and the source code that caused it.
    What you posted is basically useless without the source code line number.

    Where is the array dd given a value? For example dd = new char[400];


    To break your input data up into tokens look at the StringTokenizer class
    or the String split method.
    Last edited by Norm; May 24th, 2011 at 05:11 PM. Reason: How to parse String to tokens
    Norm

  5. #5
    Join Date
    May 2011
    Posts
    5

    Re: From String array to int array throwing ','and null from text file

    the problem is that netbeans wont let me copy line numbers and i put an index value index value
    and thats what i get

    Enter a filepath to copy from, and one to copy to.
    C:\Users\F.A.M\Desktop\GGGGGGGGG\tr.txt
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at s.Read.main(Read.java:68)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 50 seconds)



    code


    char [] dd = new char [500] ;
    char [] d = new char [500];
    int t=0;

    61: for (int j = 0; j < linecount; j++){
    62: s[j] = br.readLine();
    63: }
    64: for (int j = 0; j < linecount; j++){
    65: d=s[j].toCharArray();
    66: for (int y=0 ; y<=d.length ; y++){
    67: if (d != null){
    68: dd[t]=d[y] ;
    }
    }
    }


    then i changed all initial values to 1 and same error message



    is there any better you think to solve this problem

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: From String array to int array throwing ','and null from text file

    What are the values of s[j], t, y and the length of d when line 68 is executed?
    Add printlns to show the values
    Last edited by Norm; May 24th, 2011 at 05:36 PM.
    Norm

  7. #7
    Join Date
    May 2011
    Posts
    5

    Re: From String array to int array throwing ','and null from text file

    i should delete line 69 that cause the error otherwise it wont print any thing
    this the code after deleting the error line (t will not change == remain 0)

    for (int j = 0; j < linecount; j++){
    s[j] = br.readLine();
    }
    for (int j = 0; j < linecount; j++){
    d=s[j].toCharArray();
    System.out.println(s[j]);

    for (int y=0 ; y<=d.length ; y++){

    System.out.println(y);

    }

    }
    System.out.println("==========");

    System.out.println(linecount);
    FF.close();
    }

    }


    OUTPUT
    Enter a filepath to copy from, and one to copy to.
    C:\Users\F.A.M\Desktop\GGGGGGGGG\tr.txt

    0
    5
    0
    1
    1,10
    0
    1
    2
    3
    4
    8,10
    0
    1
    2
    3
    4
    10,8
    0
    1
    2
    3
    4
    7,4
    0
    1
    2
    3
    3,1
    0
    1
    2
    3
    0,1,10
    0
    1
    2
    3
    4
    5
    6
    1,2,5
    0
    1
    2
    3
    4
    5
    2,3,25
    0
    1
    2
    3
    4
    5
    6
    0,3,3
    0
    1
    2
    3
    4
    5
    3,4,8
    0
    1
    2
    3
    4
    5
    ==========
    12
    BUILD SUCCESSFUL (total time: 12 seconds)

    THANK FOR BEING SO PATIENT WITH ME NORM

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: From String array to int array throwing ','and null from text file

    Glad you're making progress.

    Remember to test the values of data you read in to be sure its what you want.
    Norm

  9. #9
    Join Date
    May 2011
    Posts
    5

    Re: From String array to int array throwing ','and null from text file

    put i didnt get how to move these numbers to separate array without the comma ',' and without errors falling on me Its very complicated you know

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: From String array to int array throwing ','and null from text file

    Look at the String split method.
    Norm

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured