# Homework Help: Magic Eight Ball

Page 1 of 1

## 4 Replies - 241 Views - Last Post: 22 May 2019 - 06:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=416195&amp;s=5a0d42d1300daa87b475c286f0654387&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 moonwolf332

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 21-May 19

# Homework Help: Magic Eight Ball

Posted 21 May 2019 - 07:10 PM

In my computer science class we were given the assignment of basically creating a Magic Eight Ball using constructors to create an array where the user inputs the amount of values in the array, a method where the user inputs the values themselves in the array using a loop, along with another method that for returning a response. After writing the code, I tested it and found that instead of outputting the value of array like it was supposed to it, it instead outputs null. I'm still a beginner at coding so I have no clue how to fix it so any help would be much appreciated.

Main class:

import static java.lang.System.*;
import java.util.Scanner;
import java.util.Arrays;

public class MagicEightBallTest

{
public static void main (String[] args)

{

Scanner input = new Scanner(System.in);

String answer = "a";
String question = " ";
int currentResponse = 0;

out.println("How many respsonses would you like there to be?");
int numResponses = input.nextInt();
input.nextLine();

MagicEightBall ball = new MagicEightBall(numResponses);

ball.questions();

out.println("Thank you for using the Magic Eight Ball");

Other class:
import static java.lang.System.*;
import java.util.Scanner;
import java.util.Arrays;

public class MagicEightBall

{

MagicEightBall(int num)
{
numResponses = num;
}

public int currentResponse;

private String question = " ";
private int numResponses;
private String answer = " ";

Scanner input = new Scanner(System.in);

public void shake(int numResponses)
{
currentResponse = (int)(Math.random()*numResponses);
out.println(currentResponse);
}

{
String[] responses = new String[numResponses];

for (int i = 0; i < numResponses; i++)
{
out.println("Enter an answer: ");
}
}

//So this is the method that's giving me the problem I think:
public void questions()
{
MagicEightBall ball = new MagicEightBall(numResponses);
String[] responses = new String[numResponses];

while (!question.equalsIgnoreCase("stop"))
{
out.println("What is your question?");
question = input.nextLine();

ball.shake(numResponses);

out.println(responses[currentResponse]);
}
}

}

The output that I'm aiming for is something like this:
How many responses would you like there to be?
4 //user input
a
s
d
f
What is your question?
asdfv
1 //The array index number
s //The array value for the array index above
What is your question?
aefvg
0
a
What is your question?
aegrv
0
a

Instead, I'm getting this:
How many responses would you like there to be?
4 //user input
a
s
d
f
What is your question?
asdfv
1 //The array index number
null //The array value for the array index above
What is your question?
aefvg
0
null
What is your question?
aegrv
0
null

Is This A Good Question/Topic? 0

## Replies To: Homework Help: Magic Eight Ball

### #2 g00se

• D.I.C Lover

Reputation: 3638
• Posts: 16,706
• Joined: 20-September 08

## Re: Homework Help: Magic Eight Ball

Posted 22 May 2019 - 01:32 AM

Quote

MagicEightBall ball = new MagicEightBall(numResponses);

Get rid of that in questions(). It creates a new instance, which you don't want

btw method names should be verbs, not nouns. So getAnswers() etc.

This post has been edited by g00se: 22 May 2019 - 01:33 AM

### #3 moonwolf332

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 21-May 19

## Re: Homework Help: Magic Eight Ball

Posted 22 May 2019 - 06:00 AM

But then it gives me this error because I don't have it instanced there

Quote

/MagicEightBall.java:50: error: cannot find symbol
ball.shake(numResponses);
^
symbol: variable ball
location: class MagicEightBall
1 error

### #4 g00se

• D.I.C Lover

Reputation: 3638
• Posts: 16,706
• Joined: 20-September 08

## Re: Homework Help: Magic Eight Ball

Posted 22 May 2019 - 06:05 AM

Well, obviously you need to get rid of that too (ball)

This post has been edited by g00se: 22 May 2019 - 06:06 AM
Reason for edit:: Clarification

### #5 moonwolf332

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 21-May 19

## Re: Homework Help: Magic Eight Ball

Posted 22 May 2019 - 06:25 AM

Okay so I got rid of that too and changed the code to try and get the array to each method by passing it as a parameter and it worked so thank you so much!!!!