help with some java

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

help with some java

Post by dawodus on Sat Oct 13, 2012 1:24 pm
([msg=70113]see help with some java[/msg])

hi there i need some help on a mini project im doing we were told to create a film quiz, im trying to go one step further and make a film quiz which would randomly select 10 questions out of the 30 questions in the array and compare them to the answers(also in an array) i would like it to ignore lower and uppercases differences. then eventually i would like it to calculate the score of the user and put the user on a scoreboard anf tell the user what possition he is, il post what iv wrote so far(programs unfinished and doesnt run right)can any one help me ?

import java.util.Random;

import javax.swing.JOptionPane;


public class FilmQuiz {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StartQuiz();
System.exit(0);


}

private static void StartQuiz() {
// TODO Auto-generated method stub
String StartAnswer;
StartAnswer = JOptionPane.showInputDialog("welcome to Sukkys Film Quiz Input yes if your ready to start");
if (StartAnswer.equals ("yes")) {
AskQuestion();
}
else
JOptionPane.showMessageDialog(null, "you didnt enter yes so im guessing that you dont want to play bye.");
System.exit(0);
}

private static void AskQuestion() {
// TODO Auto-generated method stub
String[] Questions = new String[30];
String[] Answers = new String[30];
Random randomGenerator = new Random();
String UserAnswer;


Questions[0] = ("How many toy story films are out so far?");
Questions[1] = ("Will Smith played the part of Steven Hiller in the film 'Independence Day'. What was his codename??");
Questions[2] = ("In the movies who plays the role of Harry Potter?");
Questions[3] = ("In which film did Jim Carrey play the bank clerk Stanley Ipkiss?");
Questions[4] = ("Name 3 James Bond films that have a one-word title");
Questions[5] = ("In which film did Patrick Swayze play Johnny Castle?");
Questions[6] = ("Who played Indiana Jones in the films?");
Questions[7] = ("Which Disney film features the song 'The Bear Necessities'?");
Questions[8] = ("Who played Mary in the Film 'There's Something About Mary'?");
Questions[9] = ("In the film Goldfinger, what sort of school was run by Pussy Galore?");
Questions[10] = ("What was the name of Batman's butler?");
Questions[11] = ("What actor starred in the 1999 film The Green Mile?");
Questions[12] = ("In which 1950's film, were some of the actors mistakenly wearing wristwatches?");
Questions[13] = ("Who played the part of a flustered vicar in 4 Weddings and a Funeral?");
Questions[14] = ("Who played the part of Annie Porter in the 2 Speed films?");
Questions[15] = ("According to the title of the film, what sort of cowboys are Clint Eastwood, James Garner, Donald Sutherland and Tommy Lee Jones?");
Questions[16] = ("Sharon Stone uncrossed her legs to much ado in which film?");
Questions[17] = ("In the animated film Fantasia, who plays the part of the sorcerer’s apprentice?");
Questions[18] = ("What was the call sign used by Tom Cruise in Top Gun?");
Questions[19] = ("What's the name of the game played by Harry Potter and his team mates on broomsticks?");
Questions[20] = ("In Disney's Lion King who starts as King?");
Questions[21] = ("In a 1970s Film, what was the name of 'The Biggest dog in the World?'");
Questions[22] = ("Other than Marlon Brando which other actor has played the part of Vito Corleone in the Godfather series?");
Questions[23] = ("Which song from the Film 'Jungle Book' was nominated for an Oscar in 1964?");
Questions[24] = ("If Lady is a pedigree spaniel what is the name of the mongrel?");
Questions[25] = ("Who is the headmaster of Hogwart’s School of Witchcraft and Wizardry?");
Questions[26] = ("Which series of films features a police detective called Martin Riggs?");
Questions[27] = ("Who was Captain Hook's first mate in Peter Pan?");
Questions[28] = ("What was the first name of the character played by Olivia Newton John in Grease?");
Questions[29] = (" In the Harry Potter stories name either of the dogs kept by Hagrid?");
Questions[30] = ("Which film won the Oscar for best animation in 2004?");
Answers[0] = ("3");
Answers[1] = ("Eagle");
Answers[2] = ("Daniel Radcliffe");
Answers[3] = ("The Mask");
Answers[4] = (" 'Goldfinger', 'Thunderball', 'Moonraker', 'Octopussy' and 'Goldeneye'");
Answers[5] = ("Dirty dancing");
Answers[6] = ("Harrison Ford");
Answers[7] = ("The Jungle Book");
Answers[8] = ("Cameron Diaz");
Answers[9] = ("Flying");
Answers[10] = ("Alfred");
Answers[11] = ("Tom Hanks");
Answers[12] = ("Ben Hur");
Answers[13] = ("Rowan Atkinson");
Answers[14] = ("Sandra Bullock");
Answers[15] = ("Space Cowboys");
Answers[16] = ("Basic Instict");
Answers[17] = ("Mickey Mouse");
Answers[18] = ("Maverick");
Answers[19] = ("Quidditch");
Answers[20] = ("Mufasa");
Answers[21] = ("Digby");
Answers[22] = ("Robert De Niro");
Answers[23] = ("The Bare necessities.");
Answers[24] = ("Tramp");
Answers[25] = ("Professor Albus Dumbledore");
Answers[26] = ("Lethal Weapon");
Answers[27] = ("Smee");
Answers[28] = ("Sandy");
Answers[29] = ("Fang or Fluffy");
Answers[30] = ("Finding Nemo");

for (int i = 1; i <= 30; i++)
{
UserAnswer= JOptionPane.showInputDialog(Questions);
if (UserAnswer.equals(Answers));
}







}

}
dawodus
New User
New User
 
Posts: 8
Joined: Sat Oct 13, 2012 1:22 pm
Blog: View Blog (0)


Re: help with some java

Post by weekend hacker on Sat Oct 13, 2012 5:10 pm
([msg=70119]see Re: help with some java[/msg])

Code: Select all
for (int i = 0; i <= 30; i++)
{
  UserAnswer= JOptionPane.showInputDialog(Questions[i]);
  if (UserAnswer.equalsIgnoreCase(Answers[i]))
  {
     //do stuff
  }
}

This will ask all the questions.
You should look into random or randomMath.Random()
Obviously if you're asking random questions then things would be different.

The rest is up to you, we're not really in the habbit of doing peoples homework.
<Yoda> if someone says something i don't like, i ban him, ban whoever defends him, and then ban the witnesses...
User avatar
weekend hacker
Administrator
Administrator
 
Posts: 192
Joined: Sun Apr 13, 2008 2:39 pm
Location: 127.0.0.1
Blog: View Blog (0)


Re: help with some java

Post by WallShadow on Sat Oct 13, 2012 5:23 pm
([msg=70122]see Re: help with some java[/msg])

Hooray, weekend beat me to it. Though I'm not just gonna erase this.

Well to start of, did you even compile this and run it yourself yet?

You have an array out of bounds error; you have a 30 element array and are trying to write to the thirty-first element.

Second, the line "UserAnswer.equals(Answers)" is a giant logical mistake. equals, by default, compares object pointers. I can see what you are trying to do though, you are trying to compare the user input string to the string of answers. But what you are actually doing is comparing the user input string's address, to the address of the ARRAY of strings (note, the String class overrides the equals function to compare Strings to Strings but still uses the default implementation of comparing references when given an object). What you need to do is compare them one at a time, either that, or concatenate together a large string of answers and compare it to the user's string (good luck convincing the user to input the answers all in the proper order and spacing). Also, if you want to compare strings, don't use equals. use compareTo(), and if you are comparing strings, it is actually better to use compareToIgnoreCase() or to use toLowerCase() before you even compare them so that an answer of "eagle" will still match "Eagle".

Honestly, if you want help, you post a SPECIFIC problem, and we answer that SPECIFIC problem, but if you post unfinished code, you won't get much help. I could easily code this entire application in a neat manner in a matter of half an hour, but you didn't ask for that. In my opinion, there's a lot of stuff you really should clean up.



Fun fact for anyone reading; due to string caching in Java, on some occasions, this would return true:

Code: Select all
String a = "Hello";
String b = "Hello";
return a == b;
User avatar
WallShadow
Contributor
Contributor
 
Posts: 625
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: help with some java

Post by LoGiCaL__ on Sat Oct 13, 2012 5:30 pm
([msg=70123]see Re: help with some java[/msg])

WallShadow wrote:good luck convincing the user to input the answers all in the proper order and spacing.


I agree. Maybe it would be better to present the user with a multiple choice type of quiz instead of a accepting user input. You could then just test for the correct choice instead of comparing your answer array to the users input answers.

A quick example:

Your answer for index 23 in the answer array is "The Bare necessities.", notice the last char in the array is a '.' . If the user gets everything correct including the spacing and doesn't enter the '.', then the compare is going to be useless.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1063
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: help with some java

Post by WallShadow on Sat Oct 13, 2012 5:34 pm
([msg=70124]see Re: help with some java[/msg])

LoGiCaL__ wrote:
WallShadow wrote:good luck convincing the user to input the answers all in the proper order and spacing.


I agree. Maybe it would be better to present the user with a multiple choice type of quiz instead of a accepting user input. You could then just test for the correct choice instead of comparing your answer array to the users input answers.


Ya, it should be done question by question, not all at once. Also, I'm thinking this would be much better implemented in a full fledged form instead of dialog boxes, say the user navigates away from it? It's annoying having to hide all of your applications just to access the question.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 625
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: help with some java

Post by weekend hacker on Sat Oct 13, 2012 6:15 pm
([msg=70125]see Re: help with some java[/msg])

WallShadow wrote:Also, if you want to compare strings, don't use equals. use compareTo(), and if you are comparing strings, it is actually better to use compareToIgnoreCase() or to use toLowerCase() before you even compare them so that an answer of "eagle" will still match "Eagle".

I personaly dislike compareTo, the String method equals(or equalsIgnoreCase) is so much simpler, and returns simply true or false. Atleast for strings that are suposed to be "the same".(although he needs to check them 1 by 1 as in my first post)
compareTo will try to tell you how different they are and its definition is downright confusing. If only there was an equals that did unicode...

PS: is it obvious that while I'm on windows and don't have my spellchecker I can't write for shit?
<Yoda> if someone says something i don't like, i ban him, ban whoever defends him, and then ban the witnesses...
User avatar
weekend hacker
Administrator
Administrator
 
Posts: 192
Joined: Sun Apr 13, 2008 2:39 pm
Location: 127.0.0.1
Blog: View Blog (0)


Re: help with some java

Post by dawodus on Sat Oct 13, 2012 6:50 pm
([msg=70128]see Re: help with some java[/msg])

sorry i just read over my question it does look like im being lazy, i should have asked the questions specifically , i actually want to do it myself just wanted to know.

1.how to make it so that the array would recieve uppercases and lowercases and maybe ever multiple answers as you can see with a few questions may have more than 1 answer.
2. if randomization is possible to the array number inside[](i would only like to ask 10 questions of the 30 randomly)
3. we are not allowed to make it multiple choice unfortunetly it has to relay if the answer is specifically right or wrong
4. this method feels messy hence why i stoped half way is there not a better way maybe it could retrieve the questions from a file and compare it to answers from another file?

please dont write the program for me as i wont learn anything that way can you maybe tell me what i need to read on or the name of the methods i should be using
thanking you in advance.
dawodus
New User
New User
 
Posts: 8
Joined: Sat Oct 13, 2012 1:22 pm
Blog: View Blog (0)


Re: help with some java

Post by WallShadow on Sun Oct 14, 2012 1:43 am
([msg=70135]see Re: help with some java[/msg])

Wow, six hours and no answers. Sorry.

dawodus wrote:1.how to make it so that the array would recieve uppercases and lowercases and maybe ever multiple answers as you can see with a few questions may have more than 1 answer.


What do you mean by this? You can't make an array receive your input, but you can split the string into multiple strings with delimiters or patterns or just fancy string manipulation. Here is an example with patterns, it takes a string and cuts it based on where the commas are located:

Code: Select all
   Pattern pattern = Pattern.compile(Pattern.quote(","));
   String [] data = pattern.split("testing, spaces! ! ! , !@#$%^&*()_+-=`~\\| ,, ,");
   
   for(int i = 0; i < data.length; i++) {
      
      System.out.println(i + " : '" + data[i] + "'");
      
   }

(note: make sure to import java.util.regex.Pattern)
should print out:
Code: Select all
0 : 'testing'
1 : ' spaces! ! ! '
2 : ' !@#$%^&*()_+-=`~\| '
3 : ''
4 : ' '


dawodus wrote:2. if randomization is possible to the array number inside[](i would only like to ask 10 questions of the 30 randomly)


Yes, as weekend said, you can use Random. I prefer using Math.random() as it is simpler but java.util.Random works just as well. Math.random() returns a double value x such that ( 0.0 <= x < 1.0 ). So multiply that by 30, and then cast it to an integer, and voila, you have a random number from 0 to 29. add one to make it from 1 to 30.

dawodus wrote:3. we are not allowed to make it multiple choice unfortunetly it has to relay if the answer is specifically right or wrong


Not a question but btw, is this for a CS class or something? Seems a bit restrictive.

dawodus wrote:4. this method feels messy hence why i stopped half way is there not a better way maybe it could retrieve the questions from a file and compare it to answers from another file?


Don't. Reading form a file is a whole different mess which you don't want to bring into this. Although it can easily be done, you shouldn't use it unless you have studied how to read/write files in java before.


BTW, NEVER, NEVER, NEVER, use System.exit() unless you've met a critical error which Java can't handle on it's own. System.exit() exits the ENTIRE JVM, which means that ANY java programs will stop executing, not only yours. In order to stop your program properly, you must exit every thread that you are using. Unless you create additional threads, you only have to worry about exiting your main one. Generally, the moment main() is done executing, the program exits normally. If you must, you can use the statement 'return;' to exit the main() immediately.

The only times that you will have to use System.exit() are times when you have serious problems like memory leaks, potential exploits, or missing key files and directories.



weekend hacker wrote:PS: is it obvious that while I'm on windows and don't have my spellchecker I can't write for shit?


Your grammar is above the 95% correct mark, so my mind automatically skims over any grammar mistakes you make. To me it seems like you have perfect grammar.

weekend hacker wrote:I personaly dislike compareTo, ...


*Sigh* I guess everyone will have their own opinions.

weekend hacker wrote:If only there was an equals that did unicode...


I'm fairly certain Java works with unicode if you set some special settings in the String class, doesn't it?

- WallShadow <3
User avatar
WallShadow
Contributor
Contributor
 
Posts: 625
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: help with some java

Post by dawodus on Sun Oct 14, 2012 6:42 am
([msg=70143]see Re: help with some java[/msg])

thanks this helped alot, i will go an try to see what i can do and let you know if im successful or not and yes it is for my computer science class just started it at uni.

-- Mon Oct 15, 2012 11:02 am --

it seems this worked for getting them out sequentially i only did 5 questions for practise now to try for getting them randomized

for (int i = 0; i <= 5; i++)
{
UserAnswer= JOptionPane.showInputDialog(Questions[i]);
if (UserAnswer.equalsIgnoreCase(Answers[i]))
{
JOptionPane.showMessageDialog(null, "Welldone that was correct");

}
else

JOptionPane.showMessageDialog(null, "That was incorrect");
}
dawodus
New User
New User
 
Posts: 8
Joined: Sat Oct 13, 2012 1:22 pm
Blog: View Blog (0)


Re: help with some java

Post by keazy on Sun Feb 10, 2013 1:31 pm
([msg=73630]see Re: help with some java[/msg])

package movie.quiz;
import java.util.Random;
import java.lang.String;
import javax.swing.JOptionPane;

/**
*
* @author Keazy
*/
public class MovieQuiz {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
StartQuiz();
System.exit(0);
}

private static void StartQuiz() {
// TODO Auto-generated method stub
String StartAnswer;
String Yes = "yes";

StartAnswer = JOptionPane.showInputDialog("welcome to Keazy's Film Quiz Input yes if your ready to start");
if (StartAnswer.equals(Yes)) {
AskQuestion();
}
else if (StartAnswer.equals("Yes")){
AskQuestion();
}

else {
JOptionPane.showMessageDialog(null, "you didnt enter yes so im guessing that you dont want to play bye.");
System.exit(0);
}
}

private static void AskQuestion() {
// TODO Auto-generated method stub
Integer QuestionNum;
String[] Questions = new String[31];
String[] Answers = new String[31];
String UserAnswer;
Integer Score = 0;
Random Random = new Random();



Questions[0] = ("How many toy story films are out so far?");
Questions[1] = ("Will Smith played the part of Steven Hiller in the film 'Independence Day'. What was his codename?");
Questions[2] = ("In the movies who plays the role of Harry Potter?");
Questions[3] = ("In which film did Jim Carrey play the bank clerk Stanley Ipkiss?");
Questions[4] = ("Name 3 James Bond films that have a one-word title");
Questions[5] = ("In which film did Patrick Swayze play Johnny Castle?");
Questions[6] = ("Who played Indiana Jones in the films?");
Questions[7] = ("Which Disney film features the song 'The Bear Necessities'?");
Questions[8] = ("Who played Mary in the Film 'There's Something About Mary'?");
Questions[9] = ("In the film Goldfinger, what sort of school was run by Pussy Galore?");
Questions[10] = ("What was the name of Batman's butler?");
Questions[11] = ("What actor starred in the 1999 film The Green Mile?");
Questions[12] = ("In which 1950's film, were some of the actors mistakenly wearing wristwatches?");
Questions[13] = ("Who played the part of a flustered vicar in 4 Weddings and a Funeral?");
Questions[14] = ("Who played the part of Annie Porter in the 2 Speed films?");
Questions[15] = ("According to the title of the film, what sort of cowboys are Clint Eastwood, James Garner, Donald Sutherland and Tommy Lee Jones?");
Questions[16] = ("Sharon Stone uncrossed her legs to much ado in which film?");
Questions[17] = ("In the animated film Fantasia, who plays the part of the sorcerer’s apprentice?");
Questions[18] = ("What was the call sign used by Tom Cruise in Top Gun?");
Questions[19] = ("What's the name of the game played by Harry Potter and his team mates on broomsticks?");
Questions[20] = ("In Disney's Lion King who starts as King?");
Questions[21] = ("In a 1970s Film, what was the name of 'The Biggest dog in the World?'");
Questions[22] = ("Other than Marlon Brando which other actor has played the part of Vito Corleone in the Godfather series?");
Questions[23] = ("Which song from the Film 'Jungle Book' was nominated for an Oscar in 1964?");
Questions[24] = ("If Lady is a pedigree spaniel what is the name of the mongrel?");
Questions[25] = ("Who is the headmaster of Hogwart’s School of Witchcraft and Wizardry?");
Questions[26] = ("Which series of films features a police detective called Martin Riggs?");
Questions[27] = ("Who was Captain Hook's first mate in Peter Pan?");
Questions[28] = ("What was the first name of the character played by Olivia Newton John in Grease?");
Questions[29] = (" In the Harry Potter stories name either of the dogs kept by Hagrid?");
Questions[30] = ("Which film won the Oscar for best animation in 2004?");
Answers[0] = ("3");
Answers[1] = ("Eagle");
Answers[2] = ("Daniel Radcliffe");
Answers[3] = ("The Mask");
Answers[4] = ("'Goldfinger', 'Thunderball', 'Moonraker', 'Octopussy' and 'Goldeneye'");
Answers[5] = ("Dirty dancing");
Answers[6] = ("Harrison Ford");
Answers[7] = ("The Jungle Book");
Answers[8] = ("Cameron Diaz");
Answers[9] = ("Flying");
Answers[10] = ("Alfred");
Answers[11] = ("Tom Hanks");
Answers[12] = ("Ben Hur");
Answers[13] = ("Rowan Atkinson");
Answers[14] = ("Sandra Bullock");
Answers[15] = ("Space Cowboys");
Answers[16] = ("Basic Instict");
Answers[17] = ("Mickey Mouse");
Answers[18] = ("Maverick");
Answers[19] = ("Quidditch");
Answers[20] = ("Mufasa");
Answers[21] = ("Digby");
Answers[22] = ("Robert De Niro");
Answers[23] = ("The Bare necessities.");
Answers[24] = ("Tramp");
Answers[25] = ("Professor Albus Dumbledore");
Answers[26] = ("Lethal Weapon");
Answers[27] = ("Smee");
Answers[28] = ("Sandy");
Answers[29] = ("Fang or Fluffy");
Answers[30] = ("Finding Nemo");

for (int i = 0; i <= 9; i++)
{
QuestionNum = Random.nextInt(31);

UserAnswer= JOptionPane.showInputDialog(Questions[QuestionNum]+QuestionNum);
if (UserAnswer.equalsIgnoreCase(Answers[QuestionNum]))
{
JOptionPane.showMessageDialog(null, "Welldone that was correct");
Score++;
}
else {

JOptionPane.showMessageDialog(null, "That was incorrect");
}
}

PrintScore(Score);





}

private static void PrintScore( int Score){
if (Score <= 3){
JOptionPane.showMessageDialog(null, "You only got "+Score+ " right. Maybe you spend too much time outside");
}
else if(Score <=7){
JOptionPane.showMessageDialog(null, "You got "+Score+ " right. The force is strong with you");
}
else{
JOptionPane.showMessageDialog(null, "Congratulations, You got "+Score+ " right. A true couch potatoe");
}
}

}







comments? questions? or maybe you can answer some of mine?
-how can i truely use an "or" answer on answer 29?
keazy
New User
New User
 
Posts: 1
Joined: Fri Feb 08, 2013 5:29 pm
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests