• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructing an array.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}

}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want this:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic