• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stuck on Homework

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I'm pretty new to java and I'm totally stuck on my homework assignment. Can anyone help me with a push in the right direction? What I'm supposed to be doing is to make the output print the students and their score in ascending order. Right now it grades the student and then lists them randomly. I'm thinking that I'm going to need to feed these scores into a multi-dimensional array so that I can sort them, but I'm having trouble figuring out how to do that? Should I be using the Scanner?

Any help is greatly greatly greatly appreciated! Thanks!





 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put your java inside 'code tags', which preserves formatting and generally makes it easier to read. Next time, just highlight your code and click the little 'code' button as if you wanted to make it italic or bold.

I'll try to look at your problem in a little bit, but odds are pretty good someone will be along soon to help.

Welcome to the Ranch!!!
 
Helena Zubkow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Fred! What a warm welcome. This website is great.
 
Ranch Hand
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are quite a few ways that you can do sorting in java. Are you looking for help with an algorithm for sorting values? What kind of things have you learned about already?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a couple of notes...

It is not showing the scores randomly...it is showing them in the order they were input. That's me being overly literal, but these sorts of things are important when you talk to other programmers. You need to be precise so everyone agrees on what we're talking about.

Next, I would strongly advise you ALWAYS use curly brackets, even when you don't need them. On line 31, you have an if-statement without them. This works and is legal, but I guarantee that at some point, you'll put in a 'System.out.println()' which will become the one line that runs in the body of the if, and your logic is shot to hell. It really doesn't cost anything to put them in, and will save you many headaches in the future.

Now, as to your specific problem...it boils down to needing to sort the data. Right now, you compute the first student's score, print it, forget it, and move on to the next student.

What you need to do is store all the students scores in a collection of some kind (and array, a List or whatever you think is best). You also need to store which student had that score. Once you have them all figured out and saved, you can sort the collection by score. then, you simply iterate through your list, in order, and print the student number and score for each.

I've been vague on purpose. That's how I approach problems I have. I start vague, but make sure I understand what i'm doing at each step. Then I start going back and refining each step of the process. So, see if the above makes sense. If not, ask more questions. If so, see if you can code some of it, and always come back if you have more questions or get stuck.

 
Helena Zubkow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome, thank you for the tip about the curly braces. I actually did have that happen when I was playing with the code and couldn't figure out why, so thank you.
 
Helena Zubkow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so I'm still stuck. I can't figure out how to read the data that's being printed into a new multi-dimensional array. I have no idea how to go from what I have now to those numbers going into a two-dimensional array like [0,7][1,6][2,5], etc so that I can sort the new array. Am I supposed to be using the scanner? I have an idea of what I need to do, but I don't know how to do code it.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you printing into a new multidimensional array? I see you reading from one above, and printing out how many correct answers the student got. If you are stuck on a different piece of code, post that so we can help.

Hunter
 
Helena Zubkow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, here's my attempt at putting the information into a new array. Can anyone tell me where I'm going wrong?


This is my output:
Student 0's correct count is 7
[[I@3e25a5
[[I@3e25a5
[[I@3e25a5
[[I@3e25a5
...
(snip)
...
[[I@3e25a5
Student 1's correct count is 6
[[I@19821f
...
(snip)
...
[[I@de6ced
BUILD SUCCESSFUL (total time: 1 second)
 
Mike Zal
Ranch Hand
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not attempt to print the studentGrade array until after you have finished adding to it. You should also declare the array before the for loops. Since you declared it inside the loop, the array is recreated each iteration of the for loop. Remember that to display an element in your array you must use square brackets, i.e. studentGrades[k][l].
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if this would take you beyond what you've learned so far, but if I was doing this I wouldn't try to use a multi-dimensional array. I'd use a single array of objects that combine the student (name/id/wharever) with the grade. In other words, I'd create a simple class for this information. The resulting code would be much easier to understand.

(By the way - I hope you don't mind, but I just edited your last post to trim the output. I think it's clear what's going on without all the output being listed).
 
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic