Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructing an array.

 
Johnny Woosh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have problems constructing an array. Well, i haf a .ord file which contains entries like "MEASURE Waist_Full=33.19". So,im supposed to extract the string and the float out into two different arrays, meaning "MEASURE Waist Full" is in one array and "33.19" is in another array. How do i go about doing it? any help will be appreciated. thanks.

This is wat i tried. is it correct?

import java.io.*;
import java.util.ArrayList;
import javax.sql.*;

class FileIO3 {
/* Main method */

public static void main(String[] args) throws IOException {

FileReader file = new FileReader("limss_h_limss.ord");
BufferedReader fileInput = new BufferedReader(file);

String text;
ArrayList words = new ArrayList();
words.add("file");

double text2;
ArrayList numbers = new ArrayList();
numbers.add("file");

System.out.println(text2);

fileInput.close();
}

}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are heading the right direction. You need to read each line in the file. A BufferedReader gives you a method to do that - read is JavaDocs and you should see. Once you've got each line, you need to parse it to spilt out the measure and value. You could use a StringTokenizer for this, or you could use the String.split() method, using the "=" as your delimiting character. Once you have each part, just add them to the ArrayLists.

A some things you might think about - are two Lists the best collections to use to store what is essentially key:value data? What happens if the measure=value string spans more than one line? Can the delimiting character appear anywhere else in your file?
 
Johnny Woosh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey paul, thanks for the help.but im not too sure bout my codes using the split method.

import java.io.*;
import java.util.ArrayList;
import javax.sql.*;

class FileIO3 {
/* Main method */

public static void main(String[] args) throws IOException {

FileReader file = new FileReader("limss_h_limss.ord");
BufferedReader fileInput = new BufferedReader(file);

String text;
ArrayList words = new ArrayList();
//words.add("file");

double text2;
ArrayList numbers = new ArrayList();
//numbers.add("file");

String line = "" ;
while((line = fileInput.readLine())!=null){
String[] parts = line.split("=") ;
words.add(part[0]) ; //add string part
numbers.add(part[1]) ; //add float part
}
is this the correct way? im not too sure.any help will be appreciated as im working on a tight deadline.thankx!
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Johny..
if u are not sure abt 'split()' in String..
Y wont u use StringTokensier (it will spilit the string in so many tokens depending on delimeter )and rest getting the data is smae as enumarator..
Try and find the appropriate mthds in javadoc...
 
Tony Morris
Ranch Hand
Posts: 1608
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic