xAnghellic
xAnghellic

Reputation: 11

"Illegal start of expression"

Could someone help me with debugging this please. I'm trying to get back into java so I'm not the best with errors. It seems like its an easy fix, but the last time I did Java was as a freshman in highschool, so i'm not sure what to do.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JPanel.*;
import javax.swing.JLabel.*;
import java.util.*;
import java.text.*;

public class CharacterCreator extends JApplet {



  public final String[] races = {"Human", "Dwarf"};
  public JComboBox<String>charRace = new JComboBox<String>(races);
  public static void main(String[]args)
  {

  public final String[] classes = {"Mage", "Warrior", "Ranger"};
  public final JComboBox<String>charClass = new JComboBox<String>(classes);


  int[] attPts = new int[7];

  JLabel pointPool = new JLabel("" + attPts[0]);
  public final JButton STRplus = new JButton("+");
  public final JButton STRminus = new JButton("-");
  JLabel strValue = new JLabel("" + attPts[1]);
  public final JButton CONplus = new JButton("+");
  public final JButton CONminus = new JButton("-");
  JLabel conValue = new JLabel("" + attPts[2]);
  public final JButton DEXplus = new JButton("+");
  public final JButton DEXminus = new JButton("-");
  JLabel dexValue = new JLabel("" + attPts[3]);
  public final JButton INTELplus = new JButton("+");
  public final JButton INTELminus = new JButton("-");
  JLabel intelValue = new JLabel("" + attPts[4]);

  public final String[] bonusAtt = {"Strength", "Constitution", "Dexterity",
    "Intelligence"};
  public JComboBox<String>humanAtt = new JComboBox<String>(bonusAtt);
  }

  public CharacterCreator() {


    JPanel asPanel = new JPanel();
    asPanel.setLayout(new GridLayout(6, 1));
    asPanel.add(new JLabel("Strength"));
    asPanel.add(new JLabel("Constitution"));
    asPanel.add(new JLabel("Dexterity"));
    asPanel.add(new JLabel("Intelligence"));



    JPanel mpPanel = new JPanel();
    mpPanel.setLayout(new GridLayout(6, 1));
    mpPanel.add(strValue);
    mpPanel.add(conValue);
    mpPanel.add(dexValue);
    mpPanel.add(intelValue);



     JPanel abPanel = new JPanel();
    abPanel.setLayout(new GridLayout(6, 2));
    abPanel.add(STRplus);
    abPanel.add(STRminus);
    abPanel.add(CONplus);
    abPanel.add(CONminus);
     abPanel.add(DEXplus);
     abPanel.add(DEXminus);
     abPanel.add(INTELplus);
    abPanel.add(INTELminus);



     JPanel attributes = new JPanel();
     attributes.add(new JLabel("Ability Score"), BorderLayout.NORTH);
    attributes.add(asPanel, BorderLayout.WEST);
    attributes.add(mpPanel, BorderLayout.CENTER);
     attributes.add(abPanel, BorderLayout.EAST);
     attributes.add(humanAtt, BorderLayout.SOUTH);

     JPanel masterPanel = new JPanel();
     masterPanel.setLayout(new GridLayout(6, 1));
     masterPanel.add(new JLabel("Pick your race"));
     masterPanel.add(charRace);
     masterPanel.add(new JLabel("Pick your Class"));
     masterPanel.add(charClass);
     masterPanel.add(new JLabel("Customize your ability points"));
     masterPanel.add(attributes);

    STRplus.addActionListener((ActionEvent e) -> {
        attPts[0] = subPool(attPts[0], attPts[1]);
        attPts[1] = increaseAtt(attPts[1]);
    });

    STRminus.addActionListener((ActionEvent e) -> {
        attPts[0] = addPool(attPts[0], attPts[1]);
        attPts[1] = decreaseAtt(attPts[1]);
    });

    CONplus.addActionListener((ActionEvent e) -> {
        attPts[0] = subPool(attPts[0], attPts[2]);
        attPts[2] = increaseAtt(attPts[2]);
    });

    CONminus.addActionListener((ActionEvent e) -> {
        attPts[0] = addPool(attPts[0], attPts[2]);
        attPts[2] = decreaseAtt(attPts[2]);
    });

    DEXplus.addActionListener((ActionEvent e) -> {
        attPts[0] = subPool(attPts[0], attPts[3]);
        attPts[3] = increaseAtt(attPts[3]);
    });

    DEXminus.addActionListener((ActionEvent e) -> {
        attPts[0] = addPool(attPts[0], attPts[3]);
        attPts[3] = decreaseAtt(attPts[3]);
    });

    INTELplus.addActionListener((ActionEvent e) -> {
        attPts[0] = subPool(attPts[0], attPts[4]);
        attPts[4] = increaseAtt(attPts[4]);
    });

    INTELminus.addActionListener((ActionEvent e) -> {
        attPts[0] = addPool(attPts[0], attPts[4]);
        attPts[4] = decreaseAtt(attPts[4]);
    });


     charRace.addItemListener((ItemEvent e) -> {
         int[] attPts1 = {32, 8, 8, 8, 8, 8, 8};
        if ((charRace.getSelectedIndex()) == 0) {
            humanAtt.setVisible(false);
            attPts1[1] = (attPts1[1] + 2);
        }
        if (((charRace.getSelectedIndex()) == 1) || ((charRace.getSelectedIndex()) == 4)) {
            humanAtt.setVisible(false);
            attPts1[2] = (attPts1[2] + 2);
        }
    });
  }

  public int subPool(int pool, int att) {

     if (att == 18)
       pool = pool - 0;
     else {
       if (att == 17)
          pool = pool - 4;
        else {
          if (att == 16)
            pool = pool - 3;
          else {
            if (att > 12)
              pool = pool - 2;
            else
              pool = pool - 1;
          }
        }
    }

     return pool;
  }

  public int addPool(int pool, int att) {

     if (att == 18)
       pool = pool + 4;
     else {
       if (att == 17)
          pool = pool + 3;
        else {
          if (att > 13)
            pool = pool + 2;
          else {
            if (att > 8)
              pool = pool - 2;
            else
              pool = pool - 0;
          }
        }
    }

     return pool;
  }

  public int increaseAtt(int att) {

    if (att < 18)
       att++;

    return att;
  }

  public int decreaseAtt(int att) {

    if (att > 8)
       att = att - 1;

    return att;
  }
}

The errors it gives me are

Main.java:18: error: illegal start of expression
  public final String[] classes = {"Mage", "Warrior", "Ranger"};
  ^
Main.java:19: error: illegal start of expression
  public final JComboBox<String>charClass = new JComboBox<String>(classes);
  ^
Main.java:25: error: illegal start of expression
  public final JButton STRplus = new JButton("+");
  ^
Main.java:26: error: illegal start of expression
  public final JButton STRminus = new JButton("-");
  ^
Main.java:28: error: illegal start of expression
  public final JButton CONplus = new JButton("+");
  ^
Main.java:29: error: illegal start of expression
  public final JButton CONminus = new JButton("-");
  ^
Main.java:31: error: illegal start of expression
  public final JButton DEXplus = new JButton("+");
  ^
Main.java:32: error: illegal start of expression
  public final JButton DEXminus = new JButton("-");
  ^
Main.java:34: error: illegal start of expression
  public final JButton INTELplus = new JButton("+");
  ^
Main.java:35: error: illegal start of expression
  public final JButton INTELminus = new JButton("-");
  ^
Main.java:38: error: illegal start of expression
  public final String[] bonusAtt = {"Strength", "Constitution", "Dexterity",
  ^
Main.java:40: error: illegal start of expression
  public JComboBox<String>humanAtt = new JComboBox<String>(bonusAtt);

If you could point me in the right direction, it would be greatly appreciated

Upvotes: 1

Views: 467

Answers (3)

Andy Thomas
Andy Thomas

Reputation: 86411

You're trying to declare member fields inside a method. You can't do that.

public static void main(String[]args)
{
  public final String[] classes = {"Mage", "Warrior", "Ranger"};
  ...

The variables should be member variables if they're attributes of an instance of the class, accessible by all the members of the class. They should be local variables if they are only used during one call of one method.

If you're intending to declare local variables, they don't need access control. Local variables can only be accessed from within the method definition. Remove public.

public static void main(String[]args)
{
  final String[] classes = {"Mage", "Warrior", "Ranger"};
  ...

If you're intending to declare member variables, they can't be declared inside the main() method. Move them out.

final String[] classes = {"Mage", "Warrior", "Ranger"};
...

public static void main(String[]args)
{
   ...

Upvotes: 2

Abubakkar
Abubakkar

Reputation: 15644

You cannot have access modifiers inside a method :

Below, snippet from your code, is wrong :

 public static void main(String[]args)
 {

  public final String[] classes = {"Mage", "Warrior", "Ranger"};

Solution :

  1. Take those stuff out of the main method.
  2. Remove the access modifier (in your case, remove public)

Upvotes: 2

StackFlowed
StackFlowed

Reputation: 6816

You cannot have public declaration in a method :

public final String[] classes = {"Mage", "Warrior", "Ranger"};
public final JComboBox<String>charClass = new JComboBox<String>(classes);
public final JButton STRplus = new JButton("+");
public final JButton STRminus = new JButton("-");
public final JButton CONplus = new JButton("+");
public final JButton CONminus = new JButton("-");
public final JButton DEXplus = new JButton("+");
public final JButton DEXminus = new JButton("-");
public final JButton INTELplus = new JButton("+");
public final JButton INTELminus = new JButton("-");
public final String[] bonusAtt = {"Strength", "Constitution", "Dexterity",
"Intelligence"};
public JComboBox<String>humanAtt = new JComboBox<String>(bonusAtt);

Change it to :

String[] classes = {"Mage", "Warrior", "Ranger"};
JComboBox<String>charClass = new JComboBox<String>(classes);
JButton STRplus = new JButton("+");
JButton STRminus = new JButton("-");
JButton CONplus = new JButton("+");
JButton CONminus = new JButton("-");
JButton DEXplus = new JButton("+");
JButton DEXminus = new JButton("-");
JButton INTELplus = new JButton("+");
JButton INTELminus = new JButton("-");
String[] bonusAtt = {"Strength", "Constitution", "Dexterity",
"Intelligence"};
JComboBox<String>humanAtt = new JComboBox<String>(bonusAtt);

This should fix the complier errors. If you want them to be class variables then move them outside main that should also fix your errors.

Upvotes: 1

Related Questions