File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Constructing an array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Constructing an array." Watch "Constructing an array." New topic
Author

Constructing an array.

Johnny Woosh
Greenhorn

Joined: May 16, 2005
Posts: 10
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

Joined: Apr 14, 2004
Posts: 10336

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?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Johnny Woosh
Greenhorn

Joined: May 16, 2005
Posts: 10
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

Joined: Mar 30, 2005
Posts: 225

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...


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You want this:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructing an array.