I'm trying to create a string array, but the compiler keeps giving me an error message on the lines I assign a value to each index of the array. Here's the code:

String[] greeting = new String[4];
	greeting[0] = "Why, Hello there!";
	greeting[1] = "Welcome.";
	greeting[2] = "blah blah blah";
	greeting[3] = "more useless crap";

The error messages I get are: ']' expected, ';' expected, illegal start of type, and <identifier> expected. I know creating an array is pretty basic, but I can't figure out what I'm doing wrong. Any help would be appreciated.

This certainly should work, there's no errors I can find and it compiles okay here.

"Illegal start of type" suggests that there may be a problem in the boilerplate. Check to see that you have the closing square bracket on your main() declaration:

public static void main(String args[])
//                                  ^ is this there?

The bracket on my main declaration is there. Any other suggestions? If you need to the the entire source code
, let me know.

Okay, I attached the .java file.

Attachments
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ClickyLatinHelp extends JFrame implements ActionListener, MouseListener {
	final int xpad = 10;
	final int ypad = 15;
	int x = 0;
	int y = 0;
	int list = 0;
	int arraylistindex = 0;
	int arrayelement = 0;
	boolean showeng = true;
	
	String[] greeting = new String[4];
	greeting[0] = "Why, Hello there!";
	greeting[1] = "Welcome.";
	greeting[2] = "blah blah blah";
	greeting[3] = "more useless crap";
	
	JButton List1Button = new JButton("List 1");
	JButton List2Button = new JButton("List 2");
	JButton List3Button = new JButton("List 3");
	JButton List4Button = new JButton("List 4");
	JButton List5Button = new JButton("List 5");
	JButton List6Button = new JButton("List 6");
	JButton List7Button = new JButton("List 7");
	JButton List8Button = new JButton("List 8");
	JButton List9Button = new JButton("List 9");
	JButton List10Button = new JButton("List 10");
	

	
	public ClickyLatinHelp() {
		super("Welcome to Latin Help");                 
		setBounds(50, 20, 750, 700);                  
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		
			DrawingArea canvas = new DrawingArea();
	JPanel ButtonBanner = new JPanel();

    setLayout(new BorderLayout());		
		List1Button.addActionListener(this);
		List2Button.addActionListener(this);
		List3Button.addActionListener(this);
		List4Button.addActionListener(this);
		List5Button.addActionListener(this);
		List6Button.addActionListener(this);
		List7Button.addActionListener(this);
		List8Button.addActionListener(this);
		List9Button.addActionListener(this);
		List10Button.addActionListener(this);
		
		ButtonBanner.add(List1Button);
		ButtonBanner.add(List2Button);
		ButtonBanner.add(List3Button);
		ButtonBanner.add(List4Button);
		ButtonBanner.add(List5Button);
		ButtonBanner.add(List6Button);
		ButtonBanner.add(List7Button);
		ButtonBanner.add(List8Button);
		ButtonBanner.add(List9Button);
		ButtonBanner.add(List10Button);
		add(ButtonBanner,BorderLayout.NORTH);
		add(canvas,BorderLayout.CENTER);
		setVisible(true);
	}
	
	public class DrawingArea extends JPanel {
		
		public void paintComponent(Graphics comp) {
			
    	super.paintComponent(comp);
    	Graphics2D comp2D = (Graphics2D) comp;
    	comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    	Font font = new Font("Arial", Font.BOLD, 12);
    	comp2D.setFont(font);
    	x = xpad;
    	y = 0;
    	arrayelement = 0;
    	while (arrayelement < 5) {
    		comp2D.drawString(welcome[arrayelement],x , y);
    		y = y + ypad;
    		arrayelement++;
    		}
    	}
	}

	public void actionPerformed(ActionEvent evt) {
		Object source = evt.getSource();
		if (source == List1Button){ list = 1;}
		else if (source == List2Button){ list = 2;}
		else if (source == List3Button){ list = 3;}
		else if (source == List4Button){ list = 4;}
		else if (source == List5Button){ list = 5;}
		else if (source == List6Button){ list = 6;}
		else if (source == List7Button){ list = 7;}
		else if (source == List8Button){ list = 8;}
		else if (source == List9Button){ list = 9;}
		else if (source == List10Button){ list = 10;}
		repaint();
		}
		
	public void mouseClicked(MouseEvent event) { showeng = !showeng; }
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseReleased(MouseEvent event){}
	
	public static void main(String[] arguments) {
        ClickyLatinHelp TLH = new ClickyLatinHelp();
    }
}

You have incorrectly placed these lines of code outside of methods. This means that you are declaring attributes for your class. When you declare an array, you may initiate the String array with the corresponding values at the same time. However you wrote assignments' code during declaration, which is wrong.
String[] greeting = new String[4]; // declare an attribute of STring array. It's good.
greeting[0] = "Why, Hello there!"; // Wrong. Assignment shoud be written within a method.
greeting[1] = "Welcome.";//wrong
greeting[2] = "blah blah blah";// wrong
greeting[3] = "more useless crap";// wrong

You may write one line of code to complete the task.

String[] greeting = {"Why, Hello there!","Welcome.","blah blah blah","more useless crap"};

Edited 6 Years Ago by tong1: n/a

Okay, thanks for the help, I knew I could do it like that, I just didn't want to. I guess I have to.

This question has already been answered. Start a new discussion instead.