Newbie has simple question

Discussion in 'Java' started by bilsch, May 2, 2012.

  1. bilsch

    bilsch Guest

    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.
    bilsch, May 2, 2012
    #1
    1. Advertising

  2. bilsch

    Stefan Ram Guest

    bilsch <> writes:
    >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.
    Stefan Ram, May 2, 2012
    #2
    1. Advertising

  3. bilsch

    bilsch Guest

    On 05/02/2012 05:22 AM, Stefan Ram wrote:
    > bilsch<> writes:
    >> 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?
    bilsch, May 2, 2012
    #3
  4. On 05/02/2012 02:10 PM, bilsch wrote:
    > 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
    Silvio Bierman, May 2, 2012
    #4
  5. bilsch

    bilsch Guest

    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 have quite some nerve to abandon an earlier thread and simply start
    > a new one.
    >
    > Silvio Bierman
    bilsch, May 2, 2012
    #5
  6. On 05/02/2012 02:57 PM, bilsch wrote:
    > 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 have quite some nerve to abandon an earlier thread and simply start
    >> a new one.
    >>
    >> Silvio Bierman

    >


    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.
    Silvio Bierman, May 2, 2012
    #6
  7. bilsch

    bilsch Guest

    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.



    > 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.
    bilsch, May 2, 2012
    #7
  8. On 05/02/2012 03:36 PM, bilsch wrote:
    > 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.
    Silvio Bierman, May 2, 2012
    #8
  9. bilsch

    Lew Guest

    bilsch wrote:
    > 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.

    --
    Lew
    Lew, May 2, 2012
    #9
  10. bilsch

    bilsch Guest

    On 05/02/2012 11:07 AM, Lew wrote:
    > bilsch wrote:
    >> 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.
    bilsch, May 2, 2012
    #10
  11. bilsch

    Lew Guest

    bilsch wrote:
    > Lew wrote:
    >> You need to read the Java tutorials.
    >>

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


    <http://lmgtfy.com/?q=Java+tutorials>

    --
    Lew
    Lew, May 2, 2012
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Nicholson
    Replies:
    12
    Views:
    801
    Edwin Knoppert
    Dec 6, 2005
  2. Sphenxes
    Replies:
    2
    Views:
    3,097
    Sphenxes
    Sep 25, 2003
  3. TS
    Replies:
    3
    Views:
    351
    =?Utf-8?B?VFM=?=
    Oct 6, 2006
  4. bill.wu
    Replies:
    0
    Views:
    292
    bill.wu
    Jan 17, 2008
  5. Ted Byers
    Replies:
    2
    Views:
    118
    Brian Wakem
    Oct 9, 2009
Loading...

Share This Page