-1

I'm studying Java and am obviously very new to it. I have what appears to be a working application, however it doesn't produce any output to the console window as expected - it also doesn't give any errors which is baffling. I'm using Eclipse. I would greatly appreciate a second set of eyes to spot out what I'm doing wrong. There is some code in there from trying different approaches to get this to work - which I will delete later.

My question is: Why isn't my program producing output? As a follow up: Why isn't it producing some kind of error since it's not producing output?

I've researched for a couple of days now and the closest I found was this article which although it is similar but it doesn't really apply to this situation. System.out.print() doesn't send any output to Eclipse console. Why?

*Edited - shortened up the code & add comments on how to fix. I have 2 classes - Student and Roster.

Roster:

//**  client class....*/

import java.util.ArrayList;
import java.util.Arrays;

public class Roster {

    /* String copied from the instructions, adding info as the last item*/
    static String[] students = {"1,John,Smith,John1989@gmail.com,20,88,79,59",
            "2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
            "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87"};

static ArrayList<Student> studentProfiles = new ArrayList<Student>();

    public static void main(String[] args) {
        //creating the arraylist - rather, converting a string of arrays to an Array list of student objects

        {
            for (int i = 0; i < students.length; i++) {
                String component = students[i];
                String[] terms = component.split(",");
                String studentID = terms[0];
                String firstName = terms[1];
                String lastName = terms[2];
                String emailAddress = terms[3];
                String age = terms[4];
                int grade1 = Integer.parseInt(terms[5]);
                int grade2 = Integer.parseInt(terms[6]);
                int grade3 = Integer.parseInt(terms[7]);

                Student student = new Student(studentID, firstName, lastName, emailAddress, age, grade1, grade2, grade3);
                studentProfiles.add(student);
            }
        }
    }

    //test just to generate output, didn't work. <-- Moving this inside the main generated the output I needed.
    {
        System.out.println(studentProfiles);
    }
///
   /// enter methods here
///
//
 }

    //other print all attempt
    //-->> Put inside main 
public static void print_all()

    //-->> Put inside main  
System.out.println(j);
`enter code here`   

 //-->> Put inside main 
print_all();
print_invalid_emails();         
print_average_grade(student);
remove("3");
remove("3");
    //}
    //}



    }
}
Community
  • 1
  • 1
jdev-01
  • 3
  • 4
  • 4
    Haven't read any of your code, but I'll ask this: have you tried creating a simple hello world program, so you can get familiar with both std IO but also the IDE? – Luke Jun 28 '15 at 16:13
  • That variable had to be static because you were using it in a static context, in the main method. You would have to create an instance of the Roster class, and then refer to a nonstatic field in the context of that instance, if you didn't want a static field. – Luke Jun 28 '15 at 16:19
  • Yes, the hello world app works fine, as do the others. – jdev-01 Jun 28 '15 at 16:20

1 Answers1

0

You need to put your print inside main method.

The block that you are using:

{
   System.out.println(studentProfiles);
}

is called Initializer block and is called when you call the constructor of Roaster.

Try putting the System.out.println(studentProfiles); inside the Main() method.

Also have a look at this: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

user3245803
  • 41
  • 1
  • 3
  • Thank you!! That worked! Also, I read the oracle docs link and Luke's comments above. I get it now, the static initialization blocks essentially have more functionality. Again, thank you. That was driving me crazy. – jdev-01 Jun 28 '15 at 16:51