• Post Reply Bookmark Topic Watch Topic
  • New Topic

Working with file data and catching errors  RSS feed

 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on an assignment in which I have to read information from a text file. I need to take the numbers I retrieve from the text file and have them as numerical data that I can then work with. The information is made-up students names and students grades.

In the above loop, if you read a grade that is greater than 100 or less than 0 (and not the sentinel value), throw an exception. In the catch, ask the user to enter a correct value >= 0 and <=100. When the user enters a value within range, continue processing student’s grade with this new data otherwise continue asking the user to enter a correct value.


^That's what I'm trying to do. If there's a score that's out of range, then it needs to handle the error and ask the user to enter a new value, but I also want to make it so that the user can't enter something like non numeric characters or anything that isn't an integer. I tried to do this in the block I commented out where I have a boolean isValid method. I don't necessarily have to use a boolean method or anything. I was google searching trying to find help and found that ande decided to try to work with it. I'm not sure if there's a better way or if I'm going way off base with this.





If anyone's questioning the madness above or why something (other than the boolean method) is written the way it is, some of it is because of certain requirements I have to follow.
This is the full assignment specifications

Write a program that reads a sequential file created by Notepad. Read the student’s name and grades. Calculate the grade average. Display the name and grade average to the screen and write it to a file. Create an exception class and use it in the Demo class.
File Description:
Student name
Grade 1 (there is only one grade per line)
Grade 2

Grade Last
Sentinel value (the sentinel value is -999.9)
Repeat the above grouping for each student for any number of students
EOF
Functionality of Demo class:
Prompt user for file name. Obtain the name of the file to be read. If the file does not exist, continue to prompt the user to enter an existing file name until they enter one that exists. Do not use exception processing for this part.
The first line of the file is the student’s name. Each line up to but not including the sentinel value is a grade. The sentinel value indicates the end of a student’s grades. After the sentinel value the file may have either the end of file or another set of student’s grades. The number of grades is unknown. The number of students is unknown. Your program must read until is reaches the end of file. A sentinel value must always precede the end of file. (see file description above)
As the program reads the grades, accumulate a total and a count of the number of grades. When the sentential value is read, calculate an average. Display the student’s name and average on the screen. Also write this information to a file.
While in the above loop, if you read a grade that is greater than 100 or less than 0 (and not the sentinel value), throw an InvalidTestScore exception. In the catch, ask the user to enter a correct value >= 0 and <=100. When the user enters a value within range, continue processing student’s grade with this new data otherwise continue asking the user to enter a correct value.
Functionality of InvalidTestScore Class:
Create a class named InvalidTestScore. It should inherit from Exception. It needs a no argument constructor and a two argument constructor. The two argument constructor has parameters for the student’s name and invalid grade. There are no fields in this class. The no argument constructor displays the message Invalid test score. The two argument constructor displays the message The invalid test score for “student’s name“ is “student’s grade.” Use super to display this information.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You method isValid() starting at line 41 should be placed outside of the main() method.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think one of the other issues I noticed, is that the boolean method can only return true or false. But I need a way for int validNum to have the text file data stored to it, but only if the data is actually valid. I'm not sure if the boolean method will work for this.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Smith wrote:I think one of the other issues I noticed, is that the boolean method can only return true or false. But I need a way for int validNum to have the text file data stored to it, but only if the data is actually valid. I'm not sure if the boolean method will work for this.

Well, there are only two boolean values after all: true & false. I'm not sure what you mean by "a way ... to have the text file data stored to it" but it sounds like you want to load up validNum with more duties than just telling you whether a number you give it is valid or not. Keep things simple. One method should do one thing and one thing well. Assign different responsibilities to different methods. This way you won't confuse yourself and make problems for yourself.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps instead of isValid() you could have
This could be used both for getting the grade value from the file string and from an input string. Return -1 if it is invalid. Perhaps parseGrade() might call isValid(). Either way, don't put any file reading/writing or keyboard entry in either of these methods.

Also, I wonder if a grade should be of type double as hinted at by the sentinel value of -999.9.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Don't compare a Boolean to true or false - it is very error prone and hard to see the error if you happen to mistype as:

. you should just say
But then this line is wrong anyway since isValid is the name of your function, not a variable.

Also, in your isValid() method you don't throw an error if the grade is out of bounds.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I decided to just scrap the boolean thing and at least work on making the program run and validate that numbers in the text file are 0 - 100. Which it does that, but is there a way to make this program check that data in the text file isn't anything other than an integer and is also in the range of 0 - 100? Right now line 3 is solely dependent on the idea that there aren't any strings, doubles, or anything other than integers between the user name and the sentinel value. If I were to manually edit the text file and type something crazy in there, I'm guessing it would crash because line 3 can't handle anything other than integers. Also on line 15 if anything other than an int is input, it crashes.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankly, the requirement that makes you write lines 7 to 16 is ridiculous. That code is using exceptions to control the flow of execution. In the real world of programming, that is a bad practice so you should bring that up with your instructor.

As for your question of how to make the program check that the data in the text file isn't anything other than an integer, this is where you can actually use exception handling. Line 3 will throw an exception for any input that isn't an integer, so just handle it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!