//string :args?

Hi,

im new to java and im trying to display the strings saved in the
strings args array in the main method.

I got this piece of code

for(string s1: args)
{
System.out.println(s1)
{
i understand the system print bit but not the first line I have also tried to incorp this into my assignment as below without success

Can anyone help me with this :slight_smile:

public class Main {

public static void main(String[] args){
 Main X = new Main();
  //new Main();
}

private void addStudent(){

    Scanner sname = new Scanner(System.in);  //system input
    System.out.println("\

\ Add New Student");
System.out.println(“Please enter Students full name:”); //prompt for details
String studentName = sname.nextLine(); //add student to the datastore
System.out.println();
System.out.printf(studentName +"
has been added successfully!
");
}

private void listStudents(){
    out.println("\

\ Student Listing");
System.out.println(studentName);

}

That’s form 2 of the for loop, new as of Java 5.0.

The typical for loop works this way:


String[] array = { "one", "two", "three" };
[COLOR="Blue"]for( int i = 0; i < array.length; ++i ) {
  String string = array[i];[/COLOR]
  System.out.println( string );
}

We can rewrite it as


String[] array = { "one", "two", "three" };
[COLOR="Blue"]for( String string: array ) {[/COLOR]
  System.out.println( string );
}

As you can see, form 2 greatly simplifies the code and even eliminates a line.

It also works with Collections.

Worth reading: An enhanced for loop for the Java™ Programming Language

This is what ive done …Im still getting errors any ideas?

private void addStudent(){

    Scanner sname = new Scanner(System.in);  //system input
    System.out.println("\

\ Add New Student");
System.out.println(“Please enter Students full name:”); //prompt for details
String studentName = sname.nextLine(); //add student to the datastore
System.out.println();
System.out.printf(studentName +"
has been added successfully!
");

}

[COLOR=“Blue”] private void listStudents(){
out.println("
\ Student Listing");
String studentName = null;

    String[] args = studentName();
    for( int i = 0; i &lt; studentName.length; ++i ) {
    String string = studentName[i];
    System.out.println( studentName );[/COLOR]}
    //list all students from the datastore
}

Netbeans suggested this im not sure what it means.
private String[] studentName() {
throw new UnsupportedOperationException(“Not yet implemented”);
}
}

do you have any method by name of “studentName()”?

if not than what are you trying to do by this line: -

“String args = studentName();”

The same is being suggested by NetBeans…it asks to create a method by name of
“studentName();” which returns String

im trying to get the args array to display…listing the students names/command line arguments in a list.

Im so exhausted with confusion…

I thought that i had made a studentName method in the addStudent part???

Define a method by name of “studentName()” and return type of this method should be String

I’ve written a program (based on your code) that will ask for a student name then print that name to the screen using an array and a method each for asking and printing.

I commented most of the lines and removed my code.

Your assignment: write the code that goes with these comments and post it.
(note: it might look messy, but the indentation of the lines is significant.)
(note 2: this will only ask for and print one name, we’ll get to looping later)


// class declaration
  // declare a String[] to store the student names in
  // main method
    // create an instance of your class
  // class constuctor
    // call addStudent()
    // call printStudent()
  // define addStudent()
    // create a line scanner
    // ask for input
    // get the next line and assign it to element 0 of the array
    // print success message
  // define printStudent()
    // print element 0 of the array to screen