Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie to Java--Help with IndexOf in 2 dimensional array

Status
Not open for further replies.

BLKDAWG

Technical User
Aug 24, 2001
10
US
I have the below .dat file and I need to break it out into various components (firstname,lastname, gender, scores). I'm able to break out the first 3 components, but want the 5 scores of each person to be put into a 2 dimensional array. I can't figure out how to tell it the end of the line for each person. I've tried '\n' and "\n" but keep getting -1 or null. Please see below file and code:

MARY SMITH:F;100,33,90,80,70 BILL WILLIAMS:M;90,98,99,89,88 RALPH SUMMERS:M;22,33,54,56,78 HOLLY MASTERS:F;70,72,76,80,100 JAMES WATER:M;45,54,67,72,71 ANTHONY ROBERTSON:M;80,80,87,78,90 ALICIA SOMERSET:F;24,67,67,55,90 MARIA TANZELLI:F;55,77,78,54,44 ROBERTA KINK:F;32,66,90,4,12 MALCOLM MORRISON:M;55,76,90,90,87

for (a=0;a<=9;a++)
{
s = data_in.readUTF();
int index = s.indexOf(&quot; &quot;);
int index2 = s.indexOf(&quot;:&quot;);
int index3 = s.indexOf(&quot;;&quot;);
int index4 = s.indexOf(&quot;,&quot;); //I'm not sure what to use here

first[a] = s.substring(0,index);
last[a] = s.substring(index,index2);
gender[a] = s.substring(index2+1,index3);
for(b=0;b<=4;b++)
scores[a] = s.substring(index3+1,index4);

out = out + first[a] + last[a] + &quot; &quot; + gender[a] + &quot;\n&quot; + scores[a] + &quot;\n&quot;;
}

Thanks!!!

 

BLKDAWG,

This should do what you're after:

Code:
<html>
<head>
<script language=&quot;javascript&quot;>
<!--

	var originalData = 'MARY SMITH:F;100,33,90,80,70BILL WILLIAMS:M;90,98,99,89,88RALPH SUMMERS:M;22,33,54,56,78HOLLY MASTERS:F;70,72,76,80,100JAMES WATER:M;45,54,67,72,71ANTHONY ROBERTSON:M;80,80,87,78,90 ALICIA SOMERSET:F;24,67,67,55,90MARIA TANZELLI:F;55,77,78,54,44ROBERTA KINK:F;32,66,90,4,12MALCOLM MORRISON:M;55,76,90,90,87';
	var tempData = originalData.split('');
	var firstName = new Array();
	var lastName = new Array();
	var sex = new Array();
	var scores = new Array();

	function printData()
	{
		var htmlStr = '<hr>';
		for (var peopleLoop=0; peopleLoop<tempData.length; peopleLoop++)
		{
			var _index = tempData[peopleLoop].indexOf(' ');
			var _index2 = tempData[peopleLoop].indexOf(':');
			var _index3 = tempData[peopleLoop].indexOf(';');
	
			firstName[peopleLoop] = tempData[peopleLoop].substring(0, _index);
			lastName[peopleLoop] = tempData[peopleLoop].substring(_index+1, _index2);
			sex[peopleLoop] = tempData[peopleLoop].substring(_index2+1, _index3);
			scores[peopleLoop] = tempData[peopleLoop].substring(_index3+1).split(',');

			htmlStr += 'Person Number: ' + peopleLoop + '<br>';
			htmlStr += 'First Name: ' + firstName[peopleLoop] + '<br>';
			htmlStr += 'Last Name: ' + lastName[peopleLoop] + '<br>';
			htmlStr += 'Sex: ' + sex[peopleLoop] + '<br>';
			htmlStr += 'Scores: ' + scores[peopleLoop].join(', ') + '<hr>';
		}
		document.getElementById('dataContainer').innerHTML = htmlStr;
	}

//-->
</script>
</head>
<body onload=&quot;printData();&quot;>
	<div id=&quot;dataContainer&quot;></div>
</body>
</html>

If you get the chance to refomat slightly, I'd take the opportunity to:

- Remove the initial
Code:

from the data, and
- Remove the spaces before each
Code:


You might want to have a look at a good reference about the JavaScript 'Array' object and its properties (certainly 'split' and 'join' that I've used).

You should be able to find this by searching on Google or MSDN for 'array object javascript', I'd say.

Hope this helps,

Dan
 
Thanks so much for your help! I didn't think about using 'split'. Mucho Gracias!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top