• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Multiple keyboard inputs - > ending in a result field at the end

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

Hi all - am working my way through "if" and "else and have the basic's I think for a user to input a first name, second name and exam score. Output looks like this:



first name of student1: john
Second name of student1: doe
Enter mark  of student1: 67
john doe received a H2.2 for his mark of 67

now I would like to see if I could input more data, like for student2 and student 3 - then have the results as a total at the end.I tried to add a student2 as an input, like below-



but the output just adds like this -

first name of student1: john
Second name of student1: doe
Enter mark  of student1: 55
first name of student2: Second name of student2:

I am still on the basics at the moment but if someone could guide me towards what I could use in this instance to take multiple inputs and after running through the if/else   --> then display the result at the bottom
 
Rancher
Posts: 594
9
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After  int s3 = scan.nextInt(); , add this lineIt helps in clearing out the buffer.
 
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Murph wrote:


Do you really need in those else if blocks the compound conditions? Could you work out the simplification?
 
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I try to avoid <= and >=. They are not actually wrong, but they are harder to read and easier to get wrong than < and >.
As Liutauras says, it is possible to simplify those ifs greatly.
 
Dave Murph
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:After  int s3 = scan.nextInt(); , add this lineIt helps in clearing out the buffer.



Thanks, that was super helpful - added to my notes

as to trying to simplify it, I totally agree - it looks as clunky as hell and not scalable I guess. Its my first week of trying JAVA so if's and else's are a bit .....

I think If I could improve it by rearranging that part a bit -



Still a bit confused about correlating all the inputs for multiple students and getting one output at the end
 
Campbell Ritchie
Marshal
Posts: 74054
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a lot better.

As I said, I prefer < and >. I have the concept that in grades, the boundary mark (e.g. 70) goes in the upper category, so it goes in the lower category if it is less than that. So I use < and start low. I shall let you work out what I would do for something like parcel prices where the boundary value (weight) goes in the lower category.I presume you didn't want H2.1 twice (line 12). Please always use {...} for all ifs, elses ad loops.
Advanced challenge:- Find out how to create an enumerated type (=an enum) to encapsulate grades rather than Strings.
 
Dave Murph
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help so far -

trying to output "grade" in the seems to throw - The local variable grade may not have been initialized, I think I read it might have to redefined elsewhere?

 
Campbell Ritchie
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Murph wrote:Thanks . . .

That's a pleasure

The local variable grade may not have been initialized. . . .

You seem to have duplicated the line testing whether you have the mark in the range 0...100. That means, if you are outwith that range, s3 will have no value.

Never close a Scanner reading System.in. That is Eclipse being hyperfussy. If you get options to correct the resource leak, the correct one to use is @SuppressWarnings.... It is very unusual that you should actually use SuppressWarnings.
Please give “real” names to your variables; s1, s2, and s3 don't tell you what they mean.
 
Dave Murph
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks - duh - I missed that extra line lol. I can change the variable so something that makes more sense, like student1, student2 etc since the plan was to allow for maybe collating the data from 3 or 4 student, running them through the if/else scenario and getting the final output at the end, john doe got a grade xx, jane doe got a grade y etc etc

Still thinking about how I achieve that.



Thinking it out logically, i think if I want  to enter a grade for two students  I should use a loop., then expand the loop to enter grade for 5 students

-> then ask the user to enter a number in console for how many students to enter a grade for and this variable will determine when the loop will end

-> should I  include input validation (do...while loop) to only accept a number greater than or qual to 5.
 
Campbell Ritchie
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Murph wrote:Thanks

That's a pleasure
We have all done that sort of thing.

. . . I can change the variable so something that makes more sense, like student1, student2 . . .

I was thinking of something more like name, mark, etc.

. . . > should I  include input validation (do...while loop) to only accept a number greater than or qual to 5.

Remembering that a do‑while loop always runs at least once, consider whether that is the best type of loop to write. If you are using counter‑controlled iteration, consider whether you really need any validation of the counting. You will probably need validation of the other inputs, however.


Ouch! My miistake. You mean whether the user's input of number of students to count needs validation? If so, I misunderstood you. Sorry. Yes, that number would need validation, and a do‑while loop could be appropriate for doing that validation. Remember to include validation for count > 0.
 
reply
    Bookmark Topic Watch Topic
  • New Topic