2 Replies - 1125 Views - Last Post: 25 October 2012 - 11:46 PM Rate Topic: -----

#1 cheesecakefan741  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 12

Array Manipulations Program Question

Posted 25 October 2012 - 07:06 PM

I'm not sure how to take the student names entered and display it all under Student Roster. I would like help on this.

import java.util.Scanner;

public class StudentRoster {

	public static void main (String [] args) {
		// Declarations of variables
		Scanner reader = new Scanner (System.in);
		
		String name; 	//Student Name
		int NumStu; 	//Number of students
		
		//Prompt user for info
		System.out.print ("Enter the number of students: ");
		NumStu = reader.nextInt();
		int[] roster = new int [NumStu];
		
		for (int i = 0; i < NumStu; i++){
			System.out.print ("Enter student name: ");
			name = reader.next();
			
		}
		
		System.out.println ("\nStudent Roster");
		System.out.print (roster);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Array Manipulations Program Question

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Manipulations Program Question

Posted 25 October 2012 - 07:37 PM

name = reader.next();

should probably be

roster[i] = reader.next();
Was This Post Helpful? 0
  • +
  • -

#3 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Array Manipulations Program Question

Posted 25 October 2012 - 11:46 PM

Although that is one of the problem with the code, I also added additional comments:

import java.util.Scanner;

public class StudentRoster {

	public static void main (String [] args) {
		// Declarations of variables
		Scanner reader = new Scanner (System.in);
		
		// This variable is actually unnecessary
		String name; 	//Student Name
				
		int NumStu; 	//Number of students
		
		//Prompt user for info
		System.out.print ("Enter the number of students: ");
		NumStu = reader.nextInt();
		
		// Roster should not be declared as an array which holds integers, but as an array holding strings! (You want to store the names of a student (which is a string))
		int[] roster = new int [NumStu];  
		
		for (int i = 0; i < NumStu; i++){
			System.out.print ("Enter student name: ");
			name = reader.next(); //Read pbl's comment, this should be roster[i] = reader.next(). At i'th location, you want to store the name of the i'th student.			
		}
		
		System.out.println ("\nStudent Roster");
		
		// This will only print a memory reference, instead of the names of the student. You either need a for loop to print or you use the Arrays.toString(<your array>) method to print. The latter one requires an import from java.util.Arrays though.
		System.out.print (roster);
	}
}



Hope this helps you out ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1