|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-05-2008, 11:47 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
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-06-2008, 12:17 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,145
|
|
I thought we just did that at reply #2.
|
|

06-06-2008, 09:16 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
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  it is really annoying me because i think it is very simple...
little_polarbear
|
|

06-06-2008, 09:23 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-06-2008, 11:47 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
:-)
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, 11:56 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-06-2008, 12:03 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
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, 12:15 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-06-2008, 12:20 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 41
|
|
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.
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, 12:43 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
thank u
yes, that is what i wanted to do, than you very much for ur help
little_polarbear
|
|

06-06-2008, 12:55 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 41
|
|
|
No worries, glad that I could help.
|
|

06-06-2008, 05:34 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
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, 06:16 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 41
|
|
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..
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.
|
|

06-06-2008, 11:56 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
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, 12:49 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
I'm bit confusion on your explanation. Can you explain it little more in details.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-09-2008, 03:40 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
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, 03:46 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
Can you show what you have done so far.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-09-2008, 04:06 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-09-2008, 05:26 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 37
|
|
|
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, 04:53 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
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.
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
| | |