• 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

Need help with various methods

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm currently working on an assignment for my Java class. I have it finished, but can't seem to figure out why certain parts aren't working. The products portion of it is flawless, but the other 3 - min/max, finding the twelves, and the grader seem to be messed up. I've been at this for a few days now with no luck. It would be great if someone could help me out. Here were the instructions...

Part 1
Write a program that will find the product of a collection of data values. The program should ignore all negative values and terminate when a zero value is encountered.
2.
Write a program to read in a collection of integer values, and find and print the index of the first occurrence and last occurence of the number 12. The program should print an index value of 0 if the number 12 is not found. The index is the sequence number of the data item 12. For example if the eighth data item is the only 12, then the index value 8 should be printed for the first and last occurrence.
3.
Write a program to find the minimum, maximum, and average values of a collection of data values. The number of data values is unspecified.
4.
Write a program to read in a collection of exam scores ranging in value from 0 to 100. The program should count and print the number of A's (90 to 100), B's (70 to 89), C's (50 to 69), D's (35 to 49) and F's (0 to 34). The program should also print each score and its letter grade. Use the following test data:
63 75 72 72 78 67 80 63 0 90 89 43 59 99 82 12 100 75

Part 2
For the second part of your assignment you will want to create a seperate file call "RunMenu" or "RunMyPrograms". This file will contain the main method and will have a loop structure that will allow a user to choose what program they want to run.
1) Product no negatives
2) Find Twelve
3) Max Min Avg
4) Letter Grade
5) Exit

PART 1 CODE:


PART 2 CODE
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, thats an awful lot of code.

Is there anything in particular wrong with it?
Which bits are you having trouble with?
What input are you using?
What error messages are you getting?

Something like this:

For: min/max/average.
If I enter: 2 4 6 8

I would expect to see
You entered: 4 numbers
Max: 8
Min: 2
Average: 5

Instead I get:
Arithmetic Exceptoin: / by zero.



There is a compile error in the "myGrader" function on line 183 in your posted code.
You are using a String where it expects an integer.
And you haven't given "input" a value.

Those hints should point you in the right direction :-)
 
Haris Ceranic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for replying Stefan. I actually got it figured out and I realized where I was making mistakes. Thanks for your time!
 
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

More problems, I am afraid.
Your methods are much too long. The main method for example should contain one statement to start the application off. You should split those methods into lots of small methods.
Avoid empty Strings if possible. Not ...println(""); but ...println();
Don't use String tokenizer, which is a legacy class not recommended for new code. Use String#split(" ");←(one space) that gives you an array directly. Or a Scanner which can read an int from the keyboard directly.
If you are going to use floating‑point arithmetic, don't use floats. Use doubles throughout.
Your indentation is inconsistent.
Space the code lines out with a single space on each side of a binary operator.
Don't declare multiple variables on the same line.
 
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haris Ceranic,

What Campbell Ritchie very accurate just mentioned above, you could find a wonderful example (very similar to yours) explained in a genius way by Junilu Lacar in this thread.
Check 3, 4, and 5 posts.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haris,

My humble tip in case you get stuck the next time: start wrting your comments.

Not just bedause all good code deserves it share of comments. But writi g the comments is a good way to speak out what your code should do. And a lot of the times it will spot the errors and missing links.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic