-3

I am trying to call an object, yet I can't because I am using an if statement to declare the object. Help?

I am trying to make two robots fight, and it was easiest for me to display the robots using objects and a toString() method. After doing this though, it was difficult to make the two robots fight because I couldn't determine which robots I was using, the only way I could figure out was to make two new robot objects but in order to determine the correct robot stats, I needed to use a new object.

Here is my code:

import javax.swing.JOptionPane;

public class RobotDriver
{                                              //armor health regen damage critical missile        

public static void main(String[] args)
{
Robot Annihilator = new Robot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20,    .5);
Robot Gladiator = new Robot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
Robot Deadshot = new Robot("Deadshot", 10, 325.9, 0, 55, .80, .15);
Robot Tank = new Robot("Tank", 45, 1300, 0, 24.9, .2, 0);

String robot1 = "";
String robot2 = "";

String Robot1 = JOptionPane.showInputDialog(Annihilator + "\n" + Gladiator + "\n" + Deadshot + "\n" + Tank + "Choose First Robot: ");


String Robot2 = JOptionPane.showInputDialog(Annihilator + "\n" + Gladiator + "\n" + Deadshot + "\n" + Tank + "Choose Second Robot: ");


System.out.println("Robot #1: " + Robot1 + "\n\nRobot #2: " + Robot2);


if(Robot1.equals(Annihilator.getName()))
{
Robot firstRobot = new Robot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20, .5);
}
if(Robot1.equals(Gladiator.getName()))
{
Robot firstRobot = new Robot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
}
if(Robot1.equals(Deadshot.getName()))
{
Robot firstRobot = new Robot("Deadshot", 10, 325.9, 0, 55, .80, .15);
}
if(Robot1.equals(Tank.getName()))
{
Robot firstRobot = new Robot("Tank", 45, 1300, 0, 24.9, .2, 0);
}



if(Robot2.equals(Annihilator.getName()))
{
Robot secondRobot = new Robot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20, .5);
}
if(Robot2.equals(Gladiator.getName()))
{
Robot secondRobot = new Robot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
}
if(Robot2.equals(Deadshot.getName()))
{
Robot secondRobot = new Robot("Deadshot", 10, 325.9, 0, 55, .80, .15);
}
if(Robot2.equals(Tank.getName()))
{
Robot secondRobot = new Robot("Tank", 45, 1300, 0, 24.9, .2, 0);
}



boolean fight = true;


while(fight)
{
   //this is where the error occurs because the objects aren't declared
   fight(firstRobot, secondRobot);

}
Andy
  • 49,085
  • 60
  • 166
  • 233
Crayne
  • 1
  • 1
  • You need to declare it outside the `if` and assign it inside. – shmosel Nov 16 '17 at 05:26
  • @shmosel how would I go about assigning it inside? I have my variables as private, would I need to use getters and setters? – Crayne Nov 16 '17 at 05:27
  • They're not private, they're local. It's very simple: `Robot firstRobot; if (...) { firstRobot = ...; }` – shmosel Nov 16 '17 at 05:29
  • @Enzokie I looked at that other link trying to get an answer to my question and am still very confused.. – Crayne Nov 16 '17 at 06:12
  • @Enzokie my question is to do with a non-static method, non-static variables are different. – Crayne Nov 16 '17 at 06:15
  • @Crayne I made a mistake sorry for that, I am supposed to link [this](https://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error). – Enzokie Nov 16 '17 at 06:21
  • Do not replace your question with a different one. Instead ask a new question. – Andy Nov 16 '17 at 06:48
  • *Please*, indent your code and use one [indenting style](https://en.wikipedia.org/wiki/Indent_style) consistently throughout your code. Doing so makes it **much** easier to read/maintain. Doing so for code you place on Stack Overflow makes it much more likely both that users will up-vote your posts and that people will put time into Answering your Questions. It doesn't really matter which style your choose (although, for some languages, some styles are more appropriate than others). But, *pick one* and *use it consistently* for all code in a single project. – Makyen Nov 16 '17 at 07:01

1 Answers1

1

Declare the variables outside the conditional statements, then update the value in the conditional statements

import javax.swing.JOptionPane;

public class RobotDriver
{                                              //armor health regen damage critical missile        

public static void main(String[] args)
{
Robot Annihilator = new Robot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20,    .5);
Robot Gladiator = new Robot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
Robot Deadshot = new Robot("Deadshot", 10, 325.9, 0, 55, .80, .15);
Robot Tank = new Robot("Tank", 45, 1300, 0, 24.9, .2, 0);

String robot1 = "";
String robot2 = "";

String Robot1 = JOptionPane.showInputDialog(Annihilator + "\n" + Gladiator + "\n" + Deadshot + "\n" + Tank + "Choose First Robot: ");


String Robot2 = JOptionPane.showInputDialog(Annihilator + "\n" + Gladiator + "\n" + Deadshot + "\n" + Tank + "Choose Second Robot: ");


System.out.println("Robot #1: " + Robot1 + "\n\nRobot #2: " + Robot2);

firstRobot = new Robot()
if(Robot1.equals(Annihilator.getName()))
{
firstRobot.setRobot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20, .5);
}
if(Robot1.equals(Gladiator.getName()))
{
firstRobot.setRobot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
}
if(Robot1.equals(Deadshot.getName()))
{
firstRobot.setRobot("Deadshot", 10, 325.9, 0, 55, .80, .15);
}
if(Robot1.equals(Tank.getName()))
{
firstRobot.setRobot("Tank", 45, 1300, 0, 24.9, .2, 0);
}

Have a setRobot method in the Robot class

  • Thanks I appreciate it, I'm new to code so have used setters like once, didn't even think about setting all the variables with one method. – Crayne Nov 16 '17 at 05:32
  • you can still define the variable outside the if conditions and set it using firstRobot = new Robot(..) - i prefer to keep classes immutable. don't have a setRobot() method. it means 1. the new Robot() is incomplete until setRobot() is called. 2. robots can be redefined at any point. you might think that's good, but it's not. – slipperyseal Nov 16 '17 at 05:35
  • Setting data using methods is very convenient in my experience. You can also define different versions of the same method to take different numbers of parameters. Have fun coding ^_^ – Khanh Nghiem Nov 16 '17 at 05:36
  • I changed my question, now I ran into a new error, could I recieve some help again Khanh? – Crayne Nov 16 '17 at 06:12
  • @KhanhNghiem help please :D – Crayne Nov 16 '17 at 06:18
  • Sure, can you describe your problem? – Khanh Nghiem Nov 16 '17 at 16:24