Newbie has simple question

B

bilsch

I wrote a loop that reads a text file (NVRAM.TXT) and stores it in a big
character array and prints it out. It works fine. Just for the heck of
it I tried to break it into two files: one that does the reading and the
creation of the array, and another (with main) that prints out the first
10 characters of the array. It doesn't work. There's something wrong
with how I reference the array in the main program. The two files are
shown below, along with the error message that is generated. The
statement causing the error is:

System.out.print(bstr.nvchr[n]);

Please tell me how to make it work (if possible). Here are the two files:

public class NvrWork {
public static void main(String[] args) {
int n;
BigString bstr = new BigString();
for (n = 1; n < 10; n++) {

System.out.print(bstr.nvchr[n]);

}
}
}

public class BigString {

public BigString() {
int fin = 1;
int count = 1;
try {
FileInputStream file = new FileInputStream("NVRAM.TXT");
char[] nvchr = new char [30000];

while (fin != -1) {
fin = file.read();
nvchr[count] = (char) fin;
//System.out.print(nvchr[count]);
count++;
}
System.out.println("Bytes read : " + count);
file.close();
} catch (IOException e){
System.out.println("Could not read file");
}

}
}

OUTPUT:

run:
Bytes read : 26017
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type: <any>
at NvrWork.main(NvrWork.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

TIA. Bill S.
 
S

Stefan Ram

bilsch said:
statement causing the error is:
System.out.print(bstr.nvchr[n]);

This requires »nvchr« to be a field of »bstr« (»BigString«),
but »nvchr« is not a field of »bstr« (»BigString«). It is only
a local variable used in its constructor.
 
B

bilsch

bilsch said:
statement causing the error is:
System.out.print(bstr.nvchr[n]);

This requires »nvchr« to be a field of »bstr« (»BigString«),
but »nvchr« is not a field of »bstr« (»BigString«). It is only
a local variable used in its constructor.

I don't understand what makes it different than the variables fin and
count which are both printable from the main program. Is there no way
to make it work? What does it mean to be a 'field'? Could you tell me
more?
 
S

Silvio Bierman

I wrote a loop that reads a text file (NVRAM.TXT) and stores it in a big
character array and prints it out. It works fine. Just for the heck of
it I tried to break it into two files: one that does the reading and the
creation of the array, and another (with main) that prints out the first
10 characters of the array. It doesn't work. There's something wrong
with how I reference the array in the main program. The two files are
shown below, along with the error message that is generated. The
statement causing the error is:

System.out.print(bstr.nvchr[n]);

Please tell me how to make it work (if possible). Here are the two files:

public class NvrWork {
public static void main(String[] args) {
int n;
BigString bstr = new BigString();
for (n = 1; n < 10; n++) {

System.out.print(bstr.nvchr[n]);

}
}
}

public class BigString {

public BigString() {
int fin = 1;
int count = 1;
try {
FileInputStream file = new FileInputStream("NVRAM.TXT");
char[] nvchr = new char [30000];

while (fin != -1) {
fin = file.read();
nvchr[count] = (char) fin;
//System.out.print(nvchr[count]);
count++;
}
System.out.println("Bytes read : " + count);
file.close();
} catch (IOException e){
System.out.println("Could not read file");
}

}
}

OUTPUT:

run:
Bytes read : 26017
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type: <any>
at NvrWork.main(NvrWork.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

TIA. Bill S.



You have quite some nerve to abandon an earlier thread and simply start
a new one.

Silvio Bierman
 
B

bilsch

I didn't abandon the previous thread. It was finished. This is a new
question and wouldn't even belong in the previous thread.
 
S

Silvio Bierman

I didn't abandon the previous thread. It was finished. This is a new
question and wouldn't even belong in the previous thread.

You obviously have little experience posting to newsgroups.

First of all, do not top-post. Put your replies at the bottom om the
message you are replying to, optionally trimming parts of the original
message. This makes it easier for other people to keep track of the
discussions.

Secondly, a thread you start is not "finished" because you think that
responses are less than helpful and you have lost interest.
If you have resolved your problem, with or without the help of other
thread participants, you are supposed to make such known by posting a
closing message telling people that, and how, you have resolved the
issue. Thanking everyone for their responses at the same time is not
optional.

You are very lucky to have experienced and highly skilled people
available to help you with your programming issues. Handle them with the
grace they deserve and they will continue to be of invaluable help to you.
 
B

bilsch

I told people I had deleted the offending file - therefore I couldn't
provide the error information. Deleting the file ended the problem -
which I said. I've been posting in news groups since before you got out
of grade school.
 
S

Silvio Bierman

I told people I had deleted the offending file - therefore I couldn't
provide the error information. Deleting the file ended the problem -
which I said. I've been posting in news groups since before you got out
of grade school.

All your messages in the thread where responded to by others who did not
understand that the topic was closed. Arveds response to this particular
message contained a number of explicit questions to you that you have
rudely ignored.

I was out of grade school (in fact, close to graduating from university)
when NNTP was invented so that makes your claim unlikely.

Being inexperienced could have been considered an excuse for being rude.
Now you are just that.
 
L

Lew

bilsch said:
I wrote a loop that reads a text file (NVRAM.TXT) and stores it in a big
character array and prints it out. It works fine. Just for the heck of
it I tried to break it into two files: one that does the reading and the
creation of the array, and another (with main) that prints out the first

Files don't do reading and printing.

Programs do.
10 characters of the array. It doesn't work. There's something wrong
with how I reference the array in the main program. The two files are
shown below, along with the error message that is generated. The
statement causing the error is:

System.out.print(bstr.nvchr[n]);

Please tell me how to make it work (if possible). Here are the two files:

public class NvrWork {
public static void main(String[] args) {
int n;
BigString bstr = new BigString();

As Stefan told you, 'bstr' is a local variable. Look up "local variable". Its scope is limited to the 'main()' method of the 'NvrWork' class and is not accessible to another class. Look up "access" as it applies to Java variables, methods and members.
for (n = 1; n < 10; n++) {

System.out.print(bstr.nvchr[n]);

'nvchr' is not a member variable of the 'BigString' type, much less an accessible one.

Read the Java tutorials.
}
}
}

public class BigString {

public BigString() {
int fin = 1;
int count = 1;
try {
FileInputStream file = new FileInputStream("NVRAM.TXT");

Don't do all the work in the constructor. The constructor should only construct, not perform the major work.
char[] nvchr = new char [30000];

while (fin != -1) {
fin = file.read();
nvchr[count] = (char) fin;
//System.out.print(nvchr[count]);
count++;
}
System.out.println("Bytes read : " + count);
file.close();
} catch (IOException e){
System.out.println("Could not read file");
}

}
}

OUTPUT:

run:
Bytes read : 26017
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type: <any>
at NvrWork.main(NvrWork.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

You need to read the Java tutorials.
 
B

bilsch

bilsch said:
I wrote a loop that reads a text file (NVRAM.TXT) and stores it in a big
character array and prints it out. It works fine. Just for the heck of
it I tried to break it into two files: one that does the reading and the
creation of the array, and another (with main) that prints out the first

Files don't do reading and printing.

Programs do.
10 characters of the array. It doesn't work. There's something wrong
with how I reference the array in the main program. The two files are
shown below, along with the error message that is generated. The
statement causing the error is:

System.out.print(bstr.nvchr[n]);

Please tell me how to make it work (if possible). Here are the two files:

public class NvrWork {
public static void main(String[] args) {
int n;
BigString bstr = new BigString();

As Stefan told you, 'bstr' is a local variable. Look up "local variable". Its scope is limited to the 'main()' method of the 'NvrWork' class and is not accessible to another class. Look up "access" as it applies to Java variables, methods and members.
for (n = 1; n< 10; n++) {

System.out.print(bstr.nvchr[n]);

'nvchr' is not a member variable of the 'BigString' type, much less an accessible one.

Read the Java tutorials.
}
}
}

public class BigString {

public BigString() {
int fin = 1;
int count = 1;
try {
FileInputStream file = new FileInputStream("NVRAM.TXT");

Don't do all the work in the constructor. The constructor should only construct, not perform the major work.
char[] nvchr = new char [30000];

while (fin != -1) {
fin = file.read();
nvchr[count] = (char) fin;
//System.out.print(nvchr[count]);
count++;
}
System.out.println("Bytes read : " + count);
file.close();
} catch (IOException e){
System.out.println("Could not read file");
}

}
}

OUTPUT:

run:
Bytes read : 26017
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type:<any>
at NvrWork.main(NvrWork.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

You need to read the Java tutorials.

Thank you. I'm not sure where the tutorials are. Do you have a link?

TIA. Bill S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top