1

I am new to Java and am wondering whats wrong with the code below. I am trying to create multiple objects as an array if this is possible? The code will run and ask for a name, however just end after this and im not sure why. Any help would be great, thanks in advance.

import java.util.Scanner;


public class test {
 
    public static void main(String[] args) {
     
     
     ABug[] BugObj = new ABug[3]; //Creating object BugObj of class ABug
       
     
     
     for (int i=1; i<4; i++){
     Scanner reader = new Scanner(System.in); 
        System.out.println("Please enter the name of the bug:");
  BugObj[i].name = reader.next();
     System.out.println("Please enter the species of the bug:");
     BugObj[i].species = reader.next();
      
    
     System.out.println("Name: " + BugObj[i].name);           //Printing bug information out
     System.out.println("Species: " + BugObj[i].species);
     
     }  
    }   
}

class ABug {
 int horpos, vertpos, energy, id;
 char symbol;
 String species, name;
 
}
Logic
  • 41
  • 7

3 Answers3

1

You have two issues:

  • You need to have an instance of the object you are going to use.
  • The way to manage a for loop.

You can modify your source code to this:

Scanner reader = new Scanner(System.in); // Take out this from inside for loop

for (int i = 0; i < BugObj.length; i++) { // Notice we use BugObj.length instead of a number and start index at 0.
  System.out.println("Please enter the name of the bug:");
  BugObj[i] = new ABug(); // You need to initialize the instance before use it
  BugObj[i].name = reader.next();
dguay
  • 1,635
  • 15
  • 24
x80486
  • 6,627
  • 5
  • 52
  • 111
  • 1
    Also, you should make your properties `private` in your `ABug` class and use [`getter and setter methods`](http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html) or better use a [`constructor`](https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html) and initialize your object with `name` and `species`. – dguay Oct 20 '15 at 13:54
  • 1
    If you use that `for (int i = 1; i < BugObj.length; i++)`, there will be only 2 element in the array – Aroniaina Oct 20 '15 at 14:16
  • @Aroniaina It's true, but it's more meaningful to work "safely" with array lengths instead of relying on human's memory to know how many elements you might have or want to loop over. If you need more, just change the array initialization, nothing else. – x80486 Oct 20 '15 at 19:10
0

You have to create objects to store the data first.

import java.util.Scanner;


public class test {

    public static void main(String[] args) {


        ABug[] BugObj = new ABug[3]; //Creating object BugObj of class ABug



        for (int i=1; i<4; i++){
            BugObj[i] = new ABug(); // add this line
            Scanner reader = new Scanner(System.in);
            System.out.println("Please enter the name of the bug:");
            BugObj[i].name = reader.next();
            System.out.println("Please enter the species of the bug:");
            BugObj[i].species = reader.next();


            System.out.println("Name: " + BugObj[i].name);           //Printing bug information out
            System.out.println("Species: " + BugObj[i].species);

        }
    }
}

class ABug {
    int horpos, vertpos, energy, id;
    char symbol;
    String species, name;

}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

Two errors :

  • initialize object before use : you cannot write BugObj[i].name before writing this BugObj[i] = new ABug();
  • array bounds, Java array begin at [0] but not [1], so use for (int i=0; i<3; i++) instead of for (int i=1; i<4; i++)

The final code :

public class test {
    public static void main(String[] args) {
        ABug[] BugObj = new ABug[3];        
        for (int i=0; i<3; i++){
            Scanner reader = new Scanner(System.in);
            BugObj[i] = new ABug();
            System.out.println("Please enter the name of the bug:");
            BugObj[i].name = reader.next();
            System.out.println("Please enter the species of the bug:");
            BugObj[i].species = reader.next();
            System.out.println("Name: " + BugObj[i].name);           //Printing bug information out
            System.out.println("Species: " + BugObj[i].species);
        }       
    }
}
class ABug {
    int horpos, vertpos, energy, id;
    char symbol;
    String species, name;   
}
Aroniaina
  • 1,252
  • 13
  • 31