Results 1 to 3 of 3
  1. #1
    NixasMuraki is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default Array Code difficulties

    I'm creating a mini program that makes a roster. I haven't finished it yet, but I'm writing it in BlueJ and it keeps yelling at me and saying that a class is expected for the line that I will mark in the below code. Also I want to make it so the user can enter in how many elements the array will have, but I don't know how to do that either...it's really kind of sad. The code that's marked out and commented at the top was my sad attempt even though I knew it wouldn't work I was kind of hoping...anyway here it is:

    import javax.swing.*;

    public class Roster{

    /*System.out.println("Please type in the number of students in your class.");
    Scanner students=new Scanner(System.in);
    int intstudents=students.nextLine()-1;*/

    String[]names=newString[100/*intstudents*/]; /* Ijust figured that 101 students would be enough this way the teacher will have enough elements for the number of students*/
    int[]age=new int[100/*intstudents*/]; //these are parallel arrays

    public static void main(String[]args){
    new Roster().theWholeClass();
    }

    public void the WholeClass(){

    for(int i=0; i<names.length;i++){
    names[i]=JOptionPane.showInputDialog("Type in student's name");
    age[i]=int.parseInt(JOptionPane.showInputDialog("Type in student's age");
    }
    printData();
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    int is a primitive data type and not a class nor a valid variable name.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    public class Roster{
    
    /*System.out.println("Please type in the number of students in your class."); 
    Scanner students=new Scanner(System.in);
    int intstudents=students.nextLine()-1;*/
    You cannot have code like this floating about in the class. Only instance variable declarations and static initialiser blocks can be outside methods and constructors.

Similar Threads

  1. difficulties with IF statement condition
    By maas in forum JavaServer Pages (JSP) and JSTL
    Replies: 11
    Last Post: 10-12-2010, 08:38 AM
  2. Difficulties with many-to-many association
    By berlindutza in forum Web Frameworks
    Replies: 0
    Last Post: 03-03-2010, 04:53 PM
  3. Difficulties using Hibernate + MySQL
    By berlindutza in forum Web Frameworks
    Replies: 3
    Last Post: 02-17-2010, 09:23 AM
  4. Difficulties randomizing images in an array
    By phb5004 in forum New To Java
    Replies: 2
    Last Post: 12-11-2009, 04:45 AM
  5. JOptionPane Display Difficulties
    By kewlgeye in forum New To Java
    Replies: 7
    Last Post: 05-09-2008, 08:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •