Results 1 to 20 of 20
- 06-05-2008, 10:47 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 06-05-2008, 11:17 PM #2
I thought we just did that at reply #2.
- 06-06-2008, 08:16 AM #3
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 06-06-2008, 08:23 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-06-2008, 10:47 AM #5
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
:-)
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
- 06-06-2008, 10:56 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-06-2008, 11:03 AM #7
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 06-06-2008, 11:15 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-06-2008, 11:20 AM #9
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
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; } }
- 06-06-2008, 11:43 AM #10
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
thank u
yes, that is what i wanted to do, than you very much for ur help
little_polarbear
- 06-06-2008, 11:55 AM #11
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
No worries, glad that I could help.
- 06-06-2008, 04:34 PM #12
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 06-06-2008, 05:16 PM #13
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
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..
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.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; }
Edit: The first shot didnt work, bah now I'm late going home! Anyway that should work.
CheersLast edited by pao; 06-06-2008 at 05:26 PM.
- 06-06-2008, 10:56 PM #14
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 06-09-2008, 11:49 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I'm bit confusion on your explanation. Can you explain it little more in details.
- 06-09-2008, 02:40 PM #16
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 06-09-2008, 02:46 PM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you show what you have done so far.
- 06-09-2008, 03:06 PM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-09-2008, 04:26 PM #19
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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);
}
- 06-10-2008, 03:53 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
But you have to think about array bounds. I have not handled that in my code.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); } } }
It's doesn't matter if you keep the pattern as you said in the file.
Similar Threads
-
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM -
read the file from different line number
By vaskarbasak in forum Advanced JavaReplies: 3Last Post: 06-02-2008, 01:31 PM -
How to initialize an Array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:47 PM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM -
How to initialize array at runtime
By Java Tip in forum Java TipReplies: 0Last Post: 11-09-2007, 03:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks