Our class has been mainly writing C programs and only moved onto Java a little while ago, so please dont laugh if my code looks silly in places. Anyway i'll show you the question first then explain my problem's:
"Convert the C program you wrote in Assignment 1 that decides whether three integers inputted representing a triangle are invalid, equilateral, scalene or isosceles into Java. You should now adapt the program so that it reads the data from a file, the name of which is passed as a parameter on the command line. Your program may assume that this file will always exist and need not verify this. The format of the input file should be of with each of the three side lengths for each triangle written on a single line separated by spaces, for example: 10 20 30 5 5 8 5 5 5 5 8 12 Your program should process the file in batch mode without requiring interaction from the user. Your program should, by default, output the results to the screen. However, if a second parameter is passed on the program's command line then this should be treated as a filename in the current directory where the results should be written to. In this case your program should not output anything to the screen except for potential error messages. Your program should also be as robust as possible in terms of processing the data it receives."
Now my original C program was Non-modular, so i had to covert it to a modular Java one(If I did it ok :roll: ), then i left the user input parts outs and instead made a function that would read input from a file(as a string), I set 'newline' to return the input to main (storing it in string 'data')(is this right?) then i brought the valid/calculations code across from the C program, but now not only dont I know how to pass the 'data' input to the 'check' function(because its a string), but also how would i spilt up the input thats inside the string (need to know because the 3 numbers in the string represent 3 sides of triangle and need to be compaired against each other). The original variables for the three sides were side1,side2,side3 so thats why they are in there, I just didnt know how the alocate the input to them. I havent started on a 'write to file' part yet mainly because i wanted to get it reading from file and checking the input in 'check' function working first anyway heres code.
Sorry for the long post, any help at all would be great -thanks
[ edited to break long lines -ds ] [ May 27, 2004: Message edited by: Dirk Schreckmann ]
This has a ways to go to turn into Java. You've laid out some good ideas about what has to be done - prompt the user, read a file, do the check, display the results.
Let's take very small steps. See if you can fill in the methods to make these little programs work. I'm assuming the file "test.txt" has one line that looks like "3 4 5"
See how main tests one method, then two? Later we can add three and more.
Set a bookmark to the Java APIs http://java.sun.com/j2se/1.4.2/docs/api/index.html and start looking at BufferedReader and FileReader. You need to use them together for the first little program. StringTokenizer is a good bet for the second program, and the Integer.parseInt method.
Take a shot at these, post code as you make progress or find questions. Taking little bitty bites like this gives you frequent success (instead of a big program with lots of issues) and makes the whole thing more fun!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: May 27, 2004
Sorry only just got back from school.
About FileReader/BufferedReader, is that something that must be in there? The reason i ask is because they were never mentioned in any of the lectures so I really dont know how to type it out properly, on the notes it says "you normally will just use the readLine() method". So will it just work to use "Keybd.in.readLine();" ? Also the reason im using "textFile" class is because they told us to use it. (placing it in the same directory as the program) sorry, its just im getting kinda confused here hehe
Joined: Jan 29, 2003
You may have been given some nonstandard stuff to help you along. Keybd and textFile are not things I know from the Sun libraries. Won't be able to help you much with those. Sorry if I got going in a bad direction for you.
Joined: May 27, 2004
If I were to try the example you did above is this ok?