5 Replies - 4243 Views - Last Post: 07 February 2012 - 03:43 PM Rate Topic: -----

#1 mindlessjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-February 12

Displaying names from an array

Posted 07 February 2012 - 01:58 PM

Hello,

My assignment is to create a Java application to organize the list of graduates. The user will enter the student names and their associated concentration (1,2,3,4). The application should count the number of students in each concentration in a method DIFFERENT than the one where you're inputting the values. The output should include the list of student names and the count in each concentration

My problem: I can't figure out how to display the names. I used a for loop to run through the length of the array and concatenate them to a String variable. When I use i < array.length the output is a long list of nulls. But if I put i < 4, for example it works fine.

Any tips on what I'm doing wrong?


static final String[] concentrations= {"math", "science","english","web"};

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] names = new String[500];	//stores  student's names
		int[] stuConc = new int[500];	//stores student's concentration pick
		int[] concentCount = new int[concentrations.length];

		input(names, stuConc);
		count( stuConc, concentCount);
		output(names, concentCount, concentrations);
		


	}//end main

	private static void input(String[] stuNames, int[] stuPick)
	{
		int i = 0;
		do
		{

			stuNames[i] = JOptionPane.showInputDialog("Student's name: ");
			stuPick[i] = Integer.parseInt(JOptionPane.showInputDialog("Your concentration: \n1. DTP\n2. INFS\n3. NTEL\n4. WDM"));
			i++;
		}
		while(JOptionPane.showConfirmDialog(null, "Another student?")==JOptionPane.YES_OPTION);
	}//end method



private static void output(String[] names, int[] concentCount, String[] concent)
	{
		String nameList="";
		String concentCountList ="";
		
		for(int i =0; i < names.length; i++)   //where my problem is
		{
			nameList += names[i] + "\n";

		}//end for

		for(int i=0; i<concent.length; i++)
		{
			concentCountList += concent[i]+ ": "+ concentCount[i]+ "\n";
		}//end for
		nameList = nameList + concentCountList;
		JOptionPane.showMessageDialog(null, nameList );


	}//end output method




Thank you

I'm stumped on the method 'output'

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying names from an array

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,102
  • Joined: 19-March 11

Re: Displaying names from an array

Posted 07 February 2012 - 02:18 PM

Quote

When I use i < array.length the output is a long list of nulls. But if I put i < 4, for example it works fine.


Does the long list of nulls start with the names stored in the early slots of the array?
array.length is 500, and it'll run through all of them. If there's in array[350] (for example) you'll see a null. If there's nothing in array[51] through array[499], you'll see a long list of nulls.
Was This Post Helpful? 1
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Displaying names from an array

Posted 07 February 2012 - 02:21 PM

When you create that names array, you are allocating the space in memory so it can be filled by strings. However you are not filling the space with anything in particular. So it will default each index to null. (since nothing is there).

So lets say you add 4 students, well that is only 4 out of 500 slots filled, now if you loop through the entire array, it will count to 500, however once it reaches the 5th element, there will be nothing there. Java will output null to inform you.

you have 2 options.
  • Keep a counter of the number of students you add, and then do a for loop where i less than that counter
  • Use an if statement on names[i]. if it equals null then you can either skip it and move to the next element, or if you can gaurentee thats the last name, you can just break the lopp and be don there

Was This Post Helpful? 2
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Displaying names from an array

Posted 07 February 2012 - 02:24 PM

jon is right. A possible way to solve it, would be to store an instance variable that holds the count of students in the array. Else you could just check if the element is null, and then stop looping.
Was This Post Helpful? 0
  • +
  • -

#5 mindlessjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-February 12

Re: Displaying names from an array

Posted 07 February 2012 - 02:44 PM

Alright thanks!

I added an if to that for loop

for(int i =0; i < names.length; i++)
		{
			if(names[i]!=null)
			{
				nameList += names[i] + "\n";
			}
				



I ran it once and it seems to work.

Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,102
  • Joined: 19-March 11

Re: Displaying names from an array

Posted 07 February 2012 - 03:43 PM

If you're sure that your names will be added in a bunch up front, and that there will be no null names in the list, you could break when you get to the first null. 500 array accesses isn't a lot, but it's never a bad idea to be lazy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1