This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Hi! First of all, I know this code is riddled with problems. I have been having a very tough time with it and seem to be going in circles. Long story short, I need to ask how many students are in a class, then ask if their grades should be manually input or randomly generated (using an extended Gaussian curve). Then I need to output grades in a histogram that would look something like this:
81-90 : *****
71-80 : ***
Where each student represents an *.
I am not expecting any handouts, but I am so incredibly frusterated by this. I have been working for hours with minimal progress. Any help is much appreciated
EDIT: Specifically, I am having trouble with treating numStudents as a double. I had it as an int before, and thought I was making good progress on the program only to realize that it needed to be a double and I needed to make significant changes in my code. My compiler gives me the error message of "no suitable method found for nextInt(java.lang.String)" while highlighting ".nextInt" in my first while loop. Also, updated code:
Brian Mart wrote:Specifically, I am having trouble with treating numStudents as a double. I had it as an int before, ...
Why did you make it a double? Is the number of students ever a fractional number, for example there are 13.5 students?
Brian Mart wrote:My compiler gives me the error message of "no suitable method found for nextInt(java.lang.String)" while highlighting ".nextInt" in my first while loop.
You are trying to call the nextInt method on a Scanner object. For some reason, you are passing askNum (a String) to the method. But class Scanner does not contain a nextInt method that takes a String (see the API documentation). What are you trying to do there; why are you passing askNum to the nextInt method? What do you expect line 21 to do:
To tell you the truth, I just got mixed up trying to fix the problems the compiler was giving me, resulting in me making numStudents a double, which hindsight makes no sense whatsoever. I have made some progress though. Now my problem is on the line "inputScores[i] = askGrades.nextInt();". The compiler is telling me "cannot find symbol - method nextInt()". However, this seems to be the formula my book used for a similar code. What is wrong here?
The for loop, has no datatype for the variable "i".
Are you using an IDE where you can see all the errors at once?
Joined: Apr 17, 2012
Will, sorry, I am still trying to get a good grip on Java. I made the changes you recommended (I think) but just edited my previous post to save us from scrolling more. I am using the BlueJ compiler, as suggested by my professor. BlueJ gets hung up on the first problem of the code and does not go on from there, unfortunately, hence the plethora of problems. Thank you again for the help and sorry for any frustration.
Please read a bit about formatting: your long lines are dreadfully difficult to read. I have broken them and you can see how it is done.
Why are you using System.exit()? It is a bit vicious. It will work in this sort of simple application, but in a threaded application can cause a thread to terminate before it has finished its task, and might corrupt files, etc.
If you are using JOptionPane (a very old‑fashioned idiom, but it still works), why not use the YES_NO_OPTION, then you can use YES/NO buttons to continue or stop. Much easier than entering “y” and less error-prone in case somebody writes “Y”. Why are you not using a loop to go back to the start if you click YES? If you read through the JOptionPane documentation, you can find out about it. You can also, if you look hard enough, work out how to get RANDOM or ENTER_MYSELF buttons displayed on an option pane dialogue.
Why are you using nextGaussian at all?
Why are all your methods static?
Does your code compile? I can see some things which I think the compiler wouldn’t be happy about.
Joined: Apr 17, 2012
I will look at all of those references and research more after I get out of the class I am currently in. I am using static and Gaussian because it is required in my assignment. And my program is a long way from being able to compile, I know that. Thanks for the references, though!