This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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

Creating a Quiz Program in Java: Method takeQuiz doesn't work properly

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am creating a program to simulate a quiz. It allows the user to enter 1 to add a question to the quiz, and 2 to take the quiz. My program works correctly up until I try to take the quiz (takeQuiz();) I am assuming that user input within the addQuestion() method is not storing within my string arrays. I have included an image to show the output when I try to take the quiz (Formatting for the question is messed up and it fails to display the answer choices). Any help is appreciated!

Annotation-2020-07-21-124304EnterQuestion.png
Output when I addQuestion()
Output when I addQuestion()
Annotation-2020-07-21-124327TakeQuiz.png
Output when I takeQuiz()
Output when I takeQuiz()
 
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that you don't quite understand how Scanner.next() works vs. Scanner.nextLine(). The former will take one token (word) from the input whereas the latter will take an entire line. Notice how the question displayed back to the user only contains one word.
 
Maya Williams
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:It seems that you don't quite understand how Scanner.next() works vs. Scanner.nextLine(). The former will take one token (word) from the input whereas the latter will take an entire line. Notice how the question displayed back to the user only contains one word.



Hello. The main reason I did the many Scanner.next() and nextLine() was because originally when I ran my program, All of my statements within the addQuestion() method would out print together so it wouldn't allow me to add input for each statement. Is there another way for this to be fixed? Are you saying the Scanner.next/nextLine() are causing the problems within the takeQuiz() method. Sorry, I am still quite new to Java.
 
Maya Williams
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maya Williams wrote:

Junilu Lacar wrote:It seems that you don't quite understand how Scanner.next() works vs. Scanner.nextLine(). The former will take one token (word) from the input whereas the latter will take an entire line. Notice how the question displayed back to the user only contains one word.



Hello. The main reason I did the many Scanner.next() and nextLine() was because originally when I ran my program, All of my statements within the addQuestion() method would out print together so it wouldn't allow me to add input for each statement. Is there another way for this to be fixed? Are you saying the Scanner.next/nextLine() are causing the problems within the takeQuiz() method. Sorry, I am still quite new to Java.



I also fixed the question.add(input.next()); to question.add(nextLine()); however I still have problems when I run the method.
 
Junilu Lacar
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maya Williams wrote:however I still have problems when I run the method.


and those are ... ?

Please tell the details if you need help on them.
 
Junilu Lacar
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Were you instructed to keep the questions and various answer options in separate ArrayLists? If not, then this design is a poor choice. Things that go together should be kept together. The code you have posted so far in various threads is far from ideal in terms of object-orientation and taking full advantage of Java as an OO language. Keeping questions and options in different ArrayLists is a throwback to older, procedural languages like COBOL and BASIC that didn't take advantage of user-defined abstract data types.

If I were to do this, I'd use a class like this:

That would just be a start. I'd probably override the toString() method so that I can display the question and the available options (ordered randomly) with a simple println statement, like on line 8 below:
 
Junilu Lacar
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I wrote:


Lines 11-12 of that code actually won't work. Those two lines were meant to add the correct answer to the list of options -- the remove() call was to make sure it isn't already in there. I forgot that Arrays.asList() returns a List implementation that doesn't support add() or remove(). To work as I intended, the code would be something like this instead:
 
Junilu Lacar
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using == to compare string values here is a problem:

Don't use == to compare objects for value equality. Use equals() instead.

EDIT: I just noticed that these are char, not String. Using == in this case is fine although it's going to be case-sensitive. That is, if the user enters 'A' and the correct answer is 'a' then the program will still consider it as a wrong answer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic