• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanning an input file then running an applet  RSS feed

 
Johnny Smither
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is probably pretty simple but I can't seem to figure it out. My teacher wants us to write a code that scans a text file, then outputs some text based information AND displays an applet bar graph of the data sorta like this for the text:

Sample Bar Graph

1-10 |*****
11-20 |******
21-30 |**
31-40 |*
41-50 |***********
51-60 |******
61-70 |********
71-80 |***
81-90 |****
91-100 |****

and then an applet thats the same info just a little more graphic. My code interprets the input well, but when i try to make an applet output, the code basically forgets all of my variables and starts anew (when i just state the public static graphic (paint) class after everything) OR it refuses to scan the input file (if i switch the public class from main.....throw ExceptionIO to just public class graphic(paint))

If this is confusing ill try to explain better. also i know my code isnt 100% complete in the applet section yet

Also, as a side note, if someone could tell me if theres a way to have java automatically determine how many separate lines there are in a text file without me having to manually count them, thatd be great

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using applets? They are obsolete technology. If it is part of the assignment, you will have to use an applet, but ask about it diplomatically.

I suggest you need to divide and rule. Start with a class which represents the whatever is in the text file. Read all the data from the text file and do nothing more with them than printing them out.
Why are you using an array for the data rather than a List? The easiest way to print an array and understand it is probably to use
System.out.println(Arrays.toString(myArray));

Once you have got that working, and a class which encapsulates whatever that array is supposed to represent, you can consider the remainder of the assignment.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch again.
 
Johnny Smither
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you using applets? They are obsolete technology. If it is part of the assignment, you will have to use an applet, but ask about it diplomatically.

I suggest you need to divide and rule. Start with a class which represents the whatever is in the text file. Read all the data from the text file and do nothing more with them than printing them out.
Why are you using an array for the data rather than a List? The easiest way to print an array and understand it is probably to use
System.out.println(Arrays.toString(myArray));

Once you have got that working, and a class which encapsulates whatever that array is supposed to represent, you can consider the remainder of the assignment.


youll have to excuse me if im a little slow at understanding these things, I just started learning a few weeks ago.

Yes, Ive kind of been unimpressed by applets, especially after we learned some bit a bout GUIs. It is part of the assignment though. If I am understanding you correctly, your basically saying write a program to interpret and print the data, then use a seperate program to compile the applet while referencing the first program? also we never learned lists, just arrays but i will look into it.


thank you!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand the requirements... are you supposed to write an application which reads a text file and writes some data somewhere, and also an applet which reads that data and displays it? Or is it all supposed to be done by the applet?

From your code it looks like it's the latter, but your applet also had a public static main method, which applets don't have. There's an applet life-cycle which involves various methods which are called at various times when the applet starts and stops instead. I expect you should have learned about those methods; at any rate that's where you need to start looking.
 
Johnny Smither
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'm not sure I understand the requirements... are you supposed to write an application which reads a text file and writes some data somewhere, and also an applet which reads that data and displays it? Or is it all supposed to be done by the applet?

From your code it looks like it's the latter, but your applet also had a public static main method, which applets don't have. There's an applet life-cycle which involves various methods which are called at various times when the applet starts and stops instead. I expect you should have learned about those methods; at any rate that's where you need to start looking.


http://www.csc.villanova.edu/~map/2014/f14/proj2.html <<<<project description

the program reads a text file, outputs some text (we use a program called JGrasp to write our codes, so the text output would be there) AND output the data in an applet

the problem does indeed start with the public static main method, because the applet wont work with it. However, the public graphic method which an applet needs does not support the scan utility in order to read the text file. I need to both scan the text file AND create an applet which I dont understand how to do. Ive been looking into methods and classes but nothings working>
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JGrasp is a simple IDE, so you should be able to run the code elsewhere.
Nowhere in the specification does it say you have to use an applet. It only shows a screenshot of an applet.

You need to divide and rule, as I said earlier. There are at least four parts to what you are doing:
  • Read the file
  • Count the values
  • Display them as *****
  • Display them as bar graphs
  • All those four parts can be done separately. Indeed should be done separately, and then put together.
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If your numbers in the text file fit into a predefined range, then an array will work quite nicely.
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:JGrasp is a simple IDE, so you should be able to run the code elsewhere.
    Nowhere in the specification does it say you have to use an applet. It only shows a screenshot of an applet.

    You need to divide and rule, as I said earlier. There are at least four parts to what you are doing:
  • Read the file
  • Count the values
  • Display them as *****
  • Display them as bar graphs
  • All those four parts can be done separately. Indeed should be done separately, and then put together.


    hmmm ok. My current code reads, counts, and displays them. So all I would need to do is display them graphically.


    But how do i display them graphically AND scan the file? i cant display the file graphically unless i scan it first

    edit: ok after reading so much my brain feels numb, im starting to understand the problem a bit more. The class extends to allow the applet to run, then the first method (public static void main (String[] args) throws IOException) is required so I can scan the file and assign the numbers to an array or variable. Then I need a second method ( public void paint (Graphics page)) that will draw whatever I want on the applet. So I am seeing I need a way for the second method to access the array variable from the first method...Right?

    edit2: My text file does have a defined range, so I guess thats what my teacher was thinking
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Show us some results from reading and displaying. Something like 1-10 3 11-20 7 21-30 4 etc.
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I dont quite understand how the results have anything to do with my question but here


    I listed the numbers used in the text file I used (since i cant attach text files??), and ran the program as an application rather than an applet since it doesnt work properly yet.


    ----jGRASP exec: java Project2


    1 10 11 21 20 30 80 99 100 59 4 5 6 2 4 4 22 14 29


    01-10: |********
    11-20: |***
    21-30: |****
    31-40: |
    41-50: |
    51-60: |*
    61-70: |
    71-80: |*
    81-90: |
    91-100: |**

    ----jGRASP: operation complete.

     
    Paul Clapham
    Sheriff
    Posts: 22819
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Johnny Smither wrote:So I am seeing I need a way for the second method to access the array variable from the first method...Right?


    Yes, that's right. There are at least three ways to do that.
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Yes, that's right. There are at least three ways to do that.


    Can you please explain at least one of the ways
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What have you been taught about the structure of objects?
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    nothing really
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Do you know about fields?
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    nope
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Have a look at the Java Tutorials. Only they call fields member variables. You can have an array as a field.
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Have a look at the Java Tutorials. Only they call fields member variables. You can have an array as a field.


    I read all of them and while they did talk about a lot of things, I couldnt quite understand what I was supposed to do. I think Im just going to wait for my teacher, because this is getting ridiculously frustrating
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did you find the bit about objects having data and behaviour?
    The data are maintained in the form of instance fields. One thing you could do with your array is make it an instance field.
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Did you find the bit about objects having data and behaviour?
    The data are maintained in the form of instance fields. One thing you could do with your array is make it an instance field.


    After declaring them as static instance fields i get this error (like 1000 lines of this error):

    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at Project2.paint(Project2.java:95)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:819)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)


    If i declare them as private instance fields i get this error:

    non-static variable textnum cannot be referenced from a static context
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There ain't no such thing as a static instance field. If you don't have a good explanation for it, the keyword static is a mistake. And instance fields are never static.

    You must put the field name to the left of an assignment operator = before you use it. If the worst comes to the worst, initialise your array like this:-
    int[] number = {};
    That will initialise it to a 0-element array; you can reassign it when you have read the numbers from the file. Make sure you call the method which reads from the file before you call the method which uses the array.
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Im just telling you what the error says. I already tried initializing like you said. If i put then the script runs, however the array[][] in the applet method is still zero; the data from the main method doesnt carry over


    heres the current code



    and heres the errors:

    ----jGRASP exec: javac -g Project2.java

    Project2.java:52: error: non-static variable NUM cannot be referenced from a static context
    while (p < NUM) //checking which row of the array each number fits in
    ^
    Project2.java:56: error: non-static variable array cannot be referenced from a static context
    {array[0][p] ++;
    ^
    Project2.java:57: error: non-static variable NUM cannot be referenced from a static context
    p=NUM;}
    ^
    Project2.java:61: error: non-static variable array cannot be referenced from a static context
    array[0][NUM] = this.array[0][NUM];
    ^
    Project2.java:61: error: non-static variable NUM cannot be referenced from a static context
    array[0][NUM] = this.array[0][NUM];
    ^
    Project2.java:61: error: non-static variable this cannot be referenced from a static context
    array[0][NUM] = this.array[0][NUM];
    ^
    Project2.java:61: error: non-static variable NUM cannot be referenced from a static context
    array[0][NUM] = this.array[0][NUM];
    ^
    Project2.java:65: error: non-static variable NUM cannot be referenced from a static context
    for (int letter = 0; letter < NUM; letter++)
    ^
    Project2.java:68: error: non-static variable array cannot be referenced from a static context
    while (d < array[0][letter])
    ^
    9 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have too much code in the main method. Most of that code should be moved into its own methods. Not static.

    Have you managed to read the text file and do the counting? If so, you will have somewhere a nice int[] full of real values. Maybe it is called counts. And that will be an instance field of some class or other. So you can get your hands on it like this:-Now you can use that public method to get the counts array into another class. The reason I wrote clone() is that arrays are always mutable. The clone takes a (shallow) copy of it, but you won't have any problems about shallowness with an int[]. That means your other code can play with the array to its heart's content secure in the knowledge it can't damage the original object.

    Now you have got the counts in the array, you can work out how to display them.
     
    Johnny Smither
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you!
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
    Show us what you have now, please.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!