aspose file tools*
The moose likes Beginning Java and the fly likes Help needed gathering and sorting info in arrays to make a histogram Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help needed gathering and sorting info in arrays to make a histogram" Watch "Help needed gathering and sorting info in arrays to make a histogram" New topic
Author

Help needed gathering and sorting info in arrays to make a histogram

Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
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:
91-100: **
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:


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

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:

Lines 38 and 43 seem to be reversed.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
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?

William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

You need to stay away from the keyboard for a little bit, and look at your project with fresh eyes.

I copied this into Eclipse and receive a bunch of errors:

For example: (line 69)

You don't even define "number".
and: (line 81)

What is "system"? Hint, you have it correct at lines 77 and 78.


Fix the trivial issues first, then we can help on the larger ones.

WP
Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
EDIT: this is slightly new code, please see my bottom post for updated problems

William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Brian, I am trying to help you.

You have more than one error (still).

For example in generateRandom();

rand.nextGaussian() returns a double not an int.

The for loop, has no datatype for the variable "i".

Are you using an IDE where you can see all the errors at once?


WP
Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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.
Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
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!
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

If you are forced to use a compiler that stops on the first error, my apologies.

However, why not comment that line out, document it, and move on to the next one?

WP
Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
Thanks for the help from everyone! Unfortunately the draft of this code is due soon, so I will mark this as solved just to keep it closed. You guys are the best
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed gathering and sorting info in arrays to make a histogram