• Post Reply Bookmark Topic Watch Topic
  • New Topic

User input, change into an Array, send to a new method, then output results  RSS feed

 
Joe Bodley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I am still new to java and this one will prob be quite an easy answer for the most of you. I am trying to take a user input of number students, and the test scores for that students using a scanner class. I am tne assigning them to a new Array list. From there I want to send them to a new method that will print out a statement that tell the user what student made grades A-F. Such as
Enter how many students:
Enter their scores:
--> Student 0 got _ _ and the grade is C
--> Student 0 got _ _ and the grade is A
--> Student 0 got _ _ and the grade is F
here is my code so far. I have not created to If / else statement to run through the grades, it is that arrays i am having problems with. I keep getting a bit done then stuck again.
package lab_9;

There are the return 0.0; to just stop the issue of compile errors.


 
W. Joe Smith
Ranch Hand
Posts: 710
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think on this line double[] grades = new double[students]; you probably mean double[] grades = new double[grade]; .

Other than that, what specifically are you having trouble with on your arrays? Are you getting exceptions when you try to run? Are you getting different output than you were anticipating wfrom your input?
 
Joe Bodley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am receiving the error message 'int cannot be dereferenced'. And i have made the array set to students because i only want to have the array the same width as the students, because user will be inputting such as:
4
65 78 45 34
I dont want an array width of 65.
The main issue is sending the array to the method. Which again is the'int cannot be dereferenced'.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bodley wrote:
The main issue is sending the array to the method. Which again is the'int cannot be dereferenced'.


Ok, I get why you wouldn't want a 65 length array, that would be a little silly.

I have to ask....is the code you've posted the exact same code you are trying to compile? If it is, you should be getting an error here System.out.println(letterGrade); , because you have no variable letterGrade. I think you meant the method letterGrade(), although I could be wrong.

Your issue is in your for-loop. You have grade.length, where grade is just an int. If you use your grades array instead it should work. To prevent this, I'd recommend rethinking your variable names so they aren't so similar.
 
Joe Bodley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am trying to send it to the Method letterGrade. Would I still use a System.out.letterGrade(); to send it?
And how do I use my grade Array instead?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe and welcome to the JavaRanch.

Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I will add them for you so you can see the difference.
 
Joe Bodley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thank you for letting me know and for doing that, And i can clearly see the difference now.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bodley wrote:Yes I am trying to send it to the Method letterGrade. Would I still use a System.out.letterGrade(); to send it?
And how do I use my grade Array instead?


Ah, I see what you are saying. OK, System.out.println() is used to write output. It doesn't pass anything into methods, however println() is a method itself. For your letterGrade method I wouldn't think you want to pass the whole array, but rather just one object from the array. Since it is static, you could say in your loop something like


That would pass in the item at location i in your array to the letterGrade method. To be useful you will need to figure out how to assign it to a variable or otherwise make use of the value as you see fit.

For your letterGrade method declaration, you need to specify that it is going to take an input parameter. Since your grades array is a double array, it would look something like this:


Then in your method you would use numberGrade (which is the variable that contains what is being passed into the method) to determine the letterGrade. When you get it functioning I'm guessing you won't want a double returned, though, since a letter isn't a double.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm seeing that you're having trouble with some really basic stuff. That is not a bad thing it just means that you have a lot to learn. We all had to learn Java at some point in our lives. I would highly recommend you to read this tutorial. Especially the "Learning the Java Language" part. It covers all the basics.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!