-6

I understand that this question was asked multiple times on this site but the answers given don't seem to help me. I am very new to Java.

import javax.swing.JOptionPane;

public class Lab10 {

    String species, color, injuries, name, wiput, liput, locationFound, oiput;
    char gender;
    float weight, length;
    boolean hasShots, needsShots;
    int tagNumber, cageNumber, outcome;

    public static void main(String[] args) {
        strayAnimal(species, color, weight, length, injuries, locationFound, name);

    }

    public static void strayAnimal(String species, String color, float weight, float length, String injuries, String locationFound, String name) {
        species = JOptionPane.showInputDialog("Please enter the species of the animal: ");
        color = JOptionPane.showInputDialog("Please enter the color of the animal: ");
        wiput = JOptionPane.showInputDialog("Please enter the weight of the animal");
        weight = Float.parseFloat(wiput);
        liput = JOptionPane.showInputDialog("Please enter the lenght of the animal: ");
        length = Float.parseFloat(liput);
        locationFound = JOptionPane.showInputDialog("Please enter the location the animal was found(City, County, State): ");
        injuries = JOptionPane.showInputDialog("Please enter the injuries the animal has seperated with commas: ");

    }
    //public static char strayAnimal() {

    //}
    public static void setOutcome(int outcome) {
        oiput = JOptionPane.showInputDialog("Please enter a '0' if the animal has or will be terminated or a '1' if the animal has been saved: ");
        outcome = Integer.parseInt(oiput);
    }
    public void giveShots() {
        boolean giveShots;
        if (giveShots = true) {

        }
    }
    public static void needShots() {
        boolean needShots;
    }
}
azurefrog
  • 10,785
  • 7
  • 42
  • 56
smmbam
  • 15
  • 1
  • 1
  • 3

4 Answers4

3

You have a static method e.g. setOutcome that wants to access non-static variables like outcome. That is not allowed. You have to make the variable outcome static or the method non-static.

Markus
  • 1,141
  • 1
  • 9
  • 25
1

it does not make sense to have strayAnimal pass in any arguments.
After that you should be good.

It also helps to have the running class outside of the implementation

public class Runner {
    public static void main(String[] args) {
        Lab10 lab = new Lab10();
        lab.strayAnimal();
    }
}

And the class be

public class Lab10 {

    String species, color, injuries, name, wiput, liput, locationFound, oiput;
    char gender;
    float weight, length;
    boolean hasShots, needsShots;
    int tagNumber, cageNumber, outcome;


    public void strayAnimal(String species, String color, float weight, float length, String injuries, String locationFound, String name) {
        species = JOptionPane.showInputDialog("Please enter the species of the animal: ");
        color = JOptionPane.showInputDialog("Please enter the color of the animal: ");
        wiput = JOptionPane.showInputDialog("Please enter the weight of the animal");
        weight = Float.parseFloat(wiput);
        liput = JOptionPane.showInputDialog("Please enter the lenght of the animal: ");
        length = Float.parseFloat(liput);
        locationFound = JOptionPane.showInputDialog("Please enter the location the animal was found(City, County, State): ");
        injuries = JOptionPane.showInputDialog("Please enter the injuries the animal has seperated with commas: ");

    }

    public void setOutcome(int outcome) {
        oiput = JOptionPane.showInputDialog("Please enter a '0' if the animal has or will be terminated or a '1' if the animal has been saved: ");
        outcome = Integer.parseInt(oiput);
    }
    public void giveShots() {
        boolean giveShots;
        if (giveShots = true) {

        }
    }
    public void needShots() {
        boolean needShots;
    }
}
Bojan Petkovic
  • 2,406
  • 15
  • 26
0

Below the variable must be static

String species, color, injuries, name, wiput, liput, locationFound, oiput;
char gender;
float weight, length;
boolean hasShots, needsShots;
int tagNumber, cageNumber, outcome;

Because you cant use nonstatic variables, in a static method,

Mustafa Çil
  • 766
  • 8
  • 15
0
String species, color, injuries, name, wiput, liput, locationFound, oiput;
char gender;
float weight, length;
boolean hasShots, needsShots;
int tagNumber, cageNumber, outcome;

are all non-static class members.

a method like strayAnimals is static. You cannot reference any non-static member from a static method. You can either define an instance of the class and use that to access the class members

class Lab10 {
    public Lab10 instance;

    public static Lab10 getInstance() {
         if (instance == null) instance = new Lab10();
         return instance;
    }

    ...

    public static void strayAnimal(...) {
        Lab10 instance = Lab10.getInstance();
        // now you can use instance.wiput;
    }
}

or make the members static. However, the second option has the side effect of breaking your non-static methods.

Tyler Sebastian
  • 9,067
  • 6
  • 39
  • 62