Results 1 to 3 of 3
Thread: Class expected error with array
- 12-12-2012, 05:53 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 57
- Rep Power
- 0
Class expected error with array
I am doing what should be a simple program in Java to populate a combobox with array values, but it won't compile....
.gif)
Here is the code I have so far:
Unfortunately, when I try to compile this in the command prompt I get the following error:Java Code:import javax.swing.*; import java.awt.*; import java.util.*; public class Box extends JFrame{ private JComboBox box; private JLabel pic; int[] stuff = new int[7]; public static void getvalue(in[] array){ for (int i = 0; int<array.length; i++){ array[i] = i;}} public Box(){ super("title"); setLayout(new FlowLayout()); box = new JComboBox(stuff);}}
"arraybox.java:10: error: .class' expected
for<int i = 0; int<array.length; i++){
1 error"
Can anyone please tell me what I am doing wrong here??
- 12-12-2012, 06:14 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Class expected error with array
The error said to you clearly that you have error in line 10, which is the for-loop expression. It should be easy to spot.
Website: Learn Java by Examples
- 12-12-2012, 06:26 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Class expected error with array
Please see my comments in the code below:
Java Code:import javax.swing.*; import java.awt.*; import java.util.*; public class Box extends JFrame{ private JComboBox box; private JLabel pic; private static Integer[] getArray(){ // <<<< change "in" to be "int" Integer[] array = new Integer[7]; // "int" is a primitive. What you need here is an array of Objects like Integer for (int i = 0; i<array.length; i++){ // <<< change "int" to be "i" array[i] = i; } return array; } public Box(){ super("title"); setLayout(new FlowLayout()); box = new JComboBox(getArray()); // <<<< we want to call getArray() and have it return the array with filled in data add(box); // <<<< add box to the window setSize(500,500); // <<<< set the size of the window setVisible(true); // <<<<< you have to make the JFrame visible } public static void main(String[] args) { // <<< you need a main() method new Box(); } }
Similar Threads
-
class interface or enum expected Imports java.io.*; error
By prathamkool in forum New To JavaReplies: 2Last Post: 10-02-2011, 04:53 PM -
Error: '.class' expected
By Jill in forum New To JavaReplies: 5Last Post: 07-18-2011, 01:45 PM -
keep getting error message class, interface, or enum expected
By darr in forum New To JavaReplies: 2Last Post: 10-02-2010, 02:13 AM -
Class Expected error
By Radman23 in forum New To JavaReplies: 11Last Post: 01-14-2009, 04:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks