UW CSE logo uw

CSE 142: Computer Programming I, Winter 2008

arrow CSE Home arrow About Us arrow Search arrow Contact Info

Homework 6 (Baby Names) FAQ

How do I get started????
How do I make my code work for the 0 case?
Why do I get a NoSuchElementException?
Why do I get an InputMismatchException?
How do I make a Scanner 'start over' or 'go back'?
Why are my coordinates wrong?

Q: How do I get started????
A: Read Ch. 6 in the textbook. Look over the IMDB movie example from lecture.
Q: How do I make my code work for the 0 case?
A: The 0 ranking case is one of the hardest parts of the assignment. We recommend getting everything else to work first before worrying about the 0 case. You'll need to insert code to handle 0 differently than other rankings. One hint: A ranking of 0 is really more like a ranking of 1000, so it may help you to treat it that way in parts of your code.

Make sure to avoid redundant code. If you're doing the same thing in many branches of an if/else, that's redundant. It will make your code harder to write and more likely to have bugs.
Q: Why do I get a NoSuchElementException?
A: You tried to read past the end of a Scanner. Read the exception output text carefully and look for the first line that mentions your program. That's the line that is the culprit. For example:
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at BabyNames.sillyMethod(BabyNames.java:39)
    at BabyNames.main(BabyNames.java:15)

Q: Why do I get an InputMismatchException?
A: You tried to read the wrong type of token from a Scanner. For example, you tried to read the word "Lisa" as an int.

Read the exception output text carefully and look for the first line that mentions your program. That's the line that is the culprit. For example:
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at BabyNames.sillyMethod(BabyNames.java:39)
    at BabyNames.main(BabyNames.java:15)

Q: How do I make a Scanner 'start over' or 'go back'?
A: You can't! But you can make a new Scanner over the same String to start reading over again.
Q: Why are my coordinates wrong?
A: Try using println() to see what they are.