• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Compiled but I need a little more help :[

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The program compiles fine with no error's, but in some parts it is not doing what I want it to I'll post the code and then the problems im having.



Whenever the program starts it displays the intro and the "Opening File: "
part ok but then when it hits the while statement it returns the "There is no more input!" "This is a Valid, Scalene triangle" everytime.
Is it not finding the text file (input.txt) ?
I have both the input.txt and the output.txt doc's in the same directory as the the program and class files
My input.txt looks like this
30 40 20 \n
23 53 31 \n
11 11 11 \n
68 42 68 \n
24 54 15 \n
9 3 7 \n
And my output.txt file is only writing the number of the 'type' variable that was executed on the switch :/ ie. "333333333333333"

Any help would be great
-thanks
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 - If you take the time to write a try/catch block, you may as well let it help you by outputing the message.

2 - consider the implications of the terminating condition of the readLine() loop.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, one general comment is that ion a couple of places you commit the cardinal sin of Java programming and have catch blocks that look
like:

catch(Exception NoSuchElementException) {}

If an exception were thrown, it would be giving you useful debugging info, and so you want to display some error message. The best thing to display is the exception object itself, as it may giove you additional information about the problem.

Now, the other obvious thing is this bit (I've added some comments):



Now, finally, each time you run the program, it will examine just the first line of the input; I don't see any mechanism for advancing to the second line of the input file. You'd need a loop in main() for that to happen, right? And given that you'd then want to read multiple lines, you'd want to open the file in main, so that the state of the file was preserved across loop iterations. I think the future of the openread() method looks pretty grim...

Since the first line of the input file is, indeed, a scalene triangle, your calculation() routine seems to be doing the right thing!
 
Logan Saturnius
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for your replies,

I fired the openread() method and opened the input.txt file straight from main, but I cant think how to set up the loop for reading the first line of the input file, going through the program displaying results etc, then when going through the loop again making it read the second line. ?
Sorry for asking such a question, its probably something really simple.
So tired, I guess I should'nt stay up for 2 days at a time. hehe

this is what code is now.



-thanks
[ June 02, 2004: Message edited by: Logan Saturnius ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your other version, you had a "while ((line = in.readLine()) != null)" loop. You want exactly that loop in main, right? With basically all the rest of main() inside this loop, so you do the whole thing for each line. The loop will terminate when there's no more input; at this point, you can close the input file and return from main.
 
Logan Saturnius
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Argh,

I've got the program running but it seems to be catching like 2 or so exception errors and not displaying what i want it to

It seems to open the the file and stuff ok but runs into bother :roll:
It triggers the exception error in the tokenize() method, then displays the type of triangle, then triggers an I/O exception at the end and exits.
Im soooo gonna get this program running perfect today, even if it kills me hehe.

I've altered the code in my above post to show the program now.

once again, any help would be great ( you people rock )
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you could change your exception handing code to display the exception:

and tell us what it is we might be able to help.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm wondering if your input file really does look like

30 40 20 \n

literally, with those "\n" characters at the end. The only way that tokenize() would throw an exception is if the lines contained more than three tokens (but less than six).

In any event, tokenize() has a while() loop which can potentially extract more than one set of three tokens from a line; you're only expecting three tokens on a line, so why the loop? Getting rid of the loop will probably also get rid of the exception (although the best design would have you check whether there were more tokens before each individual call to nextToken().)
 
Logan Saturnius
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool! getting rid of the loop eliminated the first error

it goes through ok now, until just after the "Press any key to continue"
"Continuing on..." part
then it displays:

java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Assignment2prog3.main(Assignment2prog3.java:65)

-thanks
 
Logan Saturnius
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also if i wanted the output file to write the actual switch message instead of just the 'type' number how would i do it? cause at the moment its just printing "333333333333" etc. :roll:
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Logan Saturnius:

it goes through ok now, until just after the "Press any key to continue"
"Continuing on..." part
then it displays:

java.io.IOException: Stream closed



Now, this is where we get to the "teach a man to fish" part. If you call readLine() and get an exception that says "Stream closed", that probably means what? Now with a pencil and a printout, if necessary, trace through the execution of the program, and see where that thing happens. Then move the code that does that thing from that location, to a place where it makes more sense... like after you'll never call readLine() again.
 
Logan Saturnius
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh oops.. hehe i see now, i needed to take the close input file part out of the loop, and put it after all input is depleaded and the loop terminates.

Ok now I know i've probably been a bother asking all these questions and all, but i just got one last little question about this program

At the moment the output.txt file is recording the results ok, but since it is recording off the variable 'answer' (and 'answer' has the value of 'type') it's just recording the number of the option that was triggered by the switch (ie. 332103320321) Is there any way to make it record the whole line from the correct switch option?

-Thanks yet again for the help
reply
    Bookmark Topic Watch Topic
  • New Topic