Results 1 to 20 of 20
  1. #1
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default initialize a number, which is read in from a file, into an array

    Hello, it is me again,
    i have a new problem... if i wanna read the first number of a file into my java programm as an integer, but it is not working... i tried a lot... so what can i do...

    my part of the code is:

    private void reading(String fileName)
    {
    try
    {
    File file = new File(fileName);
    BufferedReader in = new BufferedReader(new FileReader(file));
    String line;

    while((line = in.readLine()) = 0);
    int v = line;

    for(int i = 0; i = v; i)
    n = array[i]


    in.close();

    }
    catch (IOException e)
    {
    System.out.println("read error: " + e.getMessage());
    }


    }

    or sth like this, i am so unsure about it... but makes it sense what i wanna do...
    it would be really nice, if anyone could help me

    little_polarbear

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I thought we just did that at reply #2.

  3. #3
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default similar

    oh, then i mean sth different...
    now my problem is very similar to my otherone. What i ment was, that i just need to know the number in the first line and in the sixth line, but not as array, just as number... i thought i could get it if i am doing it similar to my other post, but it is not working :confused: it is really annoying me because i think it is very simple...

    little_polarbear

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    While you read each line, maintain a counter. When the counter updates to you are expecting value, get that line to your variable. Then convert it to int value.

  5. #5
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default :-)

    Hey, i managed it, but now i have another problem... i have one method where i am getting an a, which is a number... then i want, that another method it using the same variable as well. I thought, i nedd just to write into the new method:

    namemethod();

    and that it is, but my programm is telling me, that i have to put sone variables into the brackets, but which one... is there a generally rule...

    little_polarbear

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What you say is not clear to me. Do you want to use same variable in two methods? Can you show your code where you want to do this.

  7. #7
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default Code

    oh sry...
    that's my code

    private void Feldeintrag(String fileName, int position)
    throws FileNotFoundException
    {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    int a = 0;


    try
    {
    for(int i = 0; i<= position; i++)
    line = in.readLine();
    a = Integer.parseInt(line);
    }
    catch (IOException e)
    {
    System.out.println("Error in reading file");
    }
    System.out.println("the first number of the line is: " + a);


    }

    and now i have a new mehod in which i wanna create an array. i asked already how to do it and i think it is kind of working, but i need an array, whose number of fields is a, which i found out in my method Feldeintrag. So i want to put a into a new method to use it as my number of fields.

    little_polarbear

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Thats mean you want to store the value of a in an array. Then what is the array size. To declare an array you want to use constant array length.

    If you don't know the length you can't use it. You can use a List or such container to do this.

  9. #9
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    Hi, I am not sure exactly what it is that you want from reading your post... I gather that you want to create an array whose length is the same as the value of a?? If so here is an example using your code.

    If not then post something that more clearly explains what you are looking for.

    Java Code:
    import java.io.*;
    
    public class FieldenTest {
        
        public static void main(String[] args) {
            FieldenTest test = new FieldenTest();
            try {
                String[] arrayToBeCreated = null;
                int arraySize = test.Feldeintrag("c:\\t.txt", 2);
                
                if(arraySize > 0) {
                    arrayToBeCreated = new String[arraySize];
                    System.out.println("FeldeinTrag returned:" + arraySize);
                    System.out.println("Array size: " + arrayToBeCreated.length);
                } else {
                    System.out.println("FeldeinTrag returned size < 1");
                }
                
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        private int Feldeintrag(String fileName, int position) throws FileNotFoundException {
            
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            String line = null;
            int a = 0;
            
            
            try {
                for(int i = 0; i<= position; i++) {
                    line = in.readLine();
                }
                
                a = Integer.parseInt(line);
                
            } catch (IOException e) {
                System.out.println("Error in reading file");
            }
            System.out.println("the first number of the line is: " + a);
            
            return a;
        }
    }

  10. #10
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default thank u

    yes, that is what i wanted to do, than you very much for ur help

    little_polarbear

  11. #11
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    No worries, glad that I could help.

  12. #12
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default It is not really working how i want to

    Hey, it is me again,
    i tried the code which i got, but it is not really working as i want to... so i thought i didn't explained clearly my problem.
    I have a file which contains number which has to be sorted.

    5
    6
    2
    7
    3
    5
    .
    .
    .
    the first number says, how many number i have to sort out. In this case 5. That what i did in my code and called a. Now i have to put the next five numbers, here 6,2,7,3,5 into an array, so that i can sort them out. And there is my problem... i managed to find out the first number, but i don't know how to put the other ones into an array. I wanted to do it in an new method... and with a for-loop like

    for(int i = 1; i<=a; i++)
    and then sth like
    int() field = new int(i);

    but it is not really working how i wanted to do it. my first problem is, that i don't know how to get my a into a new method... and then i couldn't do the rest as well...

    could anyone help me a bit... that would be really nice

    little_polarbear

  13. #13
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    Im going home in a sec but I will try and help quickly, you have your bit of code to get the first value (a) ok, and now you want to read the file all over again and put these values into an array? You could just amend your first method to do it all instead of reading the file twice..

    Java Code:
     private int[] Feldeintrag(String fileName) throws FileNotFoundException {
            //Position always 0 if you want the first line in the text file
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            String line = null;
            int[] arrayToBeCreated = null;
            int a = 0;
            int track = 0;
            try {
                 
                 while((line = in.readLine()) != null) {
                     
                     System.out.println("line = " + line);
                     if(track == 0) {
                         a = Integer.parseInt(line);
                         System.out.println("a = " + a);
                         arrayToBeCreated = new int[a];
                     } else  {
                        arrayToBeCreated[track - 1] = Integer.valueOf(line);
                     }
                     
                     if(track == a) {
                      break;    
                     }
                     
                     track ++;                 
                 }
                System.out.println("size = " + arrayToBeCreated.length);
                System.out.println("0 = " + arrayToBeCreated[0]);
                System.out.println("1 = " + arrayToBeCreated[1]);
                System.out.println("2 = " + arrayToBeCreated[2]);
            } catch (IOException e) {
                System.out.println("Error in reading file");
            }
            System.out.println("the first number of the line is: " + a);
            
            return arrayToBeCreated;
        }
    I cant promise that this does exactly what you want but I have to leave work now, I know what it is like waiting for a reply on a forum though. The method should be enough to get you in the right direction.

    Edit: The first shot didnt work, bah now I'm late going home! Anyway that should work.

    Cheers
    Last edited by pao; 06-06-2008 at 06:26 PM.

  14. #14
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default one last question

    Hey, it is actually working now.... but i have one last question...
    i have this numbers
    5
    2
    6
    3
    7
    5
    2
    3
    4

    and now i have this first array... but now i would like to have another one, the number of fields says the number after my array, in this case 2 and that's the lenght of my new array, so here my array contains 3 and 4, so i wanted actually to do it in a new method, in which i can create an b = a+1 which is the field that i would like to use, but my problem is, that if i am writing a new method, because it would be so unorganised if i am putting everything in one method, it is not recognising my a from the other method... so how can i do it...

    little_polarbear

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  16. #16
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default ok

    ok,i have a file which contains the numbers

    5 number of fields for the arrayToBeCreated
    2
    6
    3
    7
    5
    2 number of fields for the arrayKeys
    3
    4

    then i had to created an array, called arrayToBeCreated, which has five field, cos the first number of the line is 5. The following 5 numbers are in the field. So i have arrayToBeCreated, which has 5 fields with the numbers 2,6,3,7,5. Then i have to create a second array, called arrayKeys. It has two fields, because the next number after all the numbers which i have to put into the arrayToBeCreated is 2. The numbers after my 2 has to be in the fields of arrayKeys. so in this case 3,4
    does it makes sense...
    i wanted to do it in a new method, but it is not really working. i thought i have to do it over a track again, like track == a+1 or sth like this, but it is not working :-(

    little_polarbear

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    It's quite simple. Read the first line and set the size of the array to that. Then after set the size read one by one lines to the array. But it can be hurt if you don't handle the number of elements equal to the array size. Simply about ArrayOutOfBound exception.

  19. #19
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default Code

    Hey,
    I just posted what I did, i changes it a bit, trying to use what u said, but I cannot really do it... but that is the part of the code in which i am trying to read it in... can u help me with this... I am really lost in it..

    thank u so much for helping me so patiently :-) that's really nice :-)

    little_polarbear



    private void reading(String fileName)
    throws FileNotFoundException
    {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    int[] arrayToBeCreated = null;
    int[] arrayForKey = null;
    int track = 0;
    int a = 0;
    int b = a+1;
    int c = 0;

    try
    {
    while((line = in.readLine()) != null)
    {
    System.out.println("line= " + line);
    if(track == 0)
    {
    a = Integer.parseInt(line);
    System.out.println("a = " + a);
    arrayToBeCreated = new int[a];
    }
    else
    {
    arrayToBeCreated[track - 1] = Integer.valueOf(line);
    }
    if(track == a)
    {
    break;
    }
    track++;
    if(track == b)
    {
    c = Integer.parseInt(line);
    System.out.println("c =" + c);
    arrayForKey = new int[c];
    }
    else
    {
    arrayForKey[track -1] = Integer.valueOf(line);
    }
    if(track == a+c)
    {
    break;
    }
    track++;

    }
    System.out.println("size of arrayToBeCreated = " + arrayToBeCreated.length);
    System.out.println("5th field of arrayToBeCreated = " + arrayToBeCreated[4]);
    System.out.println("size of arrayForKey =" + arrayForKey.length);
    System.out.println("2nd field of arrayForKey = " + arrayForKey[1]);

    }

    catch (IOException e)
    {
    System.out.println("Error in reading file");
    }
    System.out.println("the first number of the line is: " + a);

    }

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, please use code tags posting codes next time into the forum. It's much easier to read.

    Next thing is, as much as possible use less number of variables. I have no idea some of your variables used in the code. It's so simple. See my code. Just use few lines.

    Java Code:
        public static void main(String[] args) {
            BufferedReader br = null;
            int[] numberArray;
            String line = null;
            try {
                br = new BufferedReader(new FileReader(file_path));
                while((line = br.readLine()) != null) {
                    int size = Integer.parseInt(line);
                    numberArray = new int[size];
                    
                    for(int i = 0; i < size; i++) {
                        int val = Integer.parseInt(br.readLine());
                        numberArray[i] = val;
                    }
                    
                    for(int j = 0; j < numberArray.length; j++) {
                        System.out.println(numberArray[j]);
                    }
                }
            } 
            catch (IOException ex) {
                Logger.getLogger(ArrayCreate.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally {
                try {
                    br.close();
                } catch (IOException ex) {
                    Logger.getLogger(ArrayCreate.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    But you have to think about array bounds. I have not handled that in my code.

    It's doesn't matter if you keep the pattern as you said in the file.

Similar Threads

  1. Replies: 5
    Last Post: 07-30-2008, 10:30 AM
  2. read the file from different line number
    By vaskarbasak in forum Advanced Java
    Replies: 3
    Last Post: 06-02-2008, 02:31 PM
  3. How to initialize an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:47 PM
  4. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 PM
  5. How to initialize array at runtime
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-09-2007, 04:47 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
  •