Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Reading data from a delimited file in to a vector

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a delimited file using the |

for example

param1|param2|param3 and so on

what I want to do is read in each line and parse the string into a vector so that I can use the values later in the code

Does anyone have any snippits?

Thanks in advance
 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the split method of java.lang.String.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... or alternatively use one of the open source CSV libraries such as http://opencsv.sourceforge.net/ . Google will find several others.
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dennis Grimbergen wrote:Take a look at the split method of java.lang.String.



I know I can use the split function or the tokinizer. How would you set each token to a vector?

Let's say I have 10 parameters on each line separated by the | chacter.

I would like to be able to set the tokens to something like param.Param1 and param.Param2 and so on.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gary Wittlock wrote:

Dennis Grimbergen wrote:Take a look at the split method of java.lang.String.



I know I can use the split function or the tokinizer. How would you set each token to a vector?


What mandates the use of a vector and why cannot you just use the array of String produced by teh split() method?


Let's say I have 10 parameters on each line separated by the | chacter.

I would like to be able to set the tokens to something like param.Param1 and param.Param2 and so on.


I don't understand what you mean by this last sentence!
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard Tookey wrote:

Gary Wittlock wrote:

Dennis Grimbergen wrote:Take a look at the split method of java.lang.String.



I know I can use the split function or the tokinizer. How would you set each token to a vector?


What mandates the use of a vector and why cannot you just use the array of String produced by teh split() method?


Let's say I have 10 parameters on each line separated by the | chacter.

I would like to be able to set the tokens to something like param.Param1 and param.Param2 and so on.


I don't understand what you mean by this last sentence!



Could I create a class something like
public class Params
{
public Params( String param1, double param2, double param3,
int param4)
{
m_param1 = param1;
m_param2 = param2;
m_param3 = param3;
m_param4 = param4;

}

public String param1() { return m_param1; }
public double param2() { return m_param2; }
public double param3() { return m_param3; }
public int param4() { return m_param4; }



private String m_param1;
private double m_param2;
private double m_param3;
private int m_param4;

}

I would like to know how to set each token to a parameter in the params class.
I need to search the delimted file in order to find a match for the first and second parametr.

Does this clear up my question?
The file will actually have 10 parameters per line I am just condesing it foe the disscussion.
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
COULD you? yes.

SHOULD you? I don't think so.

Your solution isn't really scaleable. What happens if you need 50 params? or 100? or 1000? Why do you want to explicitly name each param? and why do you want to give them such meaningless names? You'll get confused - "wait...is m_param83 a String or a long? Which param is the expiration date?"

Can you instead create an object that holds all your params and gives them useful names? You could then do something like

myParamList.getExpirationDate();
myParamLIst.getAge();

which is MUCH clearer than "m_param24"
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:COULD you? yes.

SHOULD you? I don't think so.

Your solution isn't really scaleable. What happens if you need 50 params? or 100? or 1000? Why do you want to explicitly name each param? and why do you want to give them such meaningless names? You'll get confused - "wait...is m_param83 a String or a long? Which param is the expiration date?"

Can you instead create an object that holds all your params and gives them useful names? You could then do something like

myParamList.getExpirationDate();
myParamLIst.getAge();

which is MUCH clearer than "m_param24"



OH by all means I am naming them a much more usefull name. I just used those names to explain what I am trying to do.

So I am wondering how to set the token to (as you say) myParamList.Expiration date?
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have a file which contains the following:

//First Name|Last Name|Car|Color|Automatic
Fred|Flintstone|Ford|Red|True
Barney|Rubble|Chevy|Blue|False
Darth|Vader|Toyota|Black|True

I read in the file using the following code:

{

public void main(String[] args) {


String paramFile = "C:/Java_Test/params.txt";
BufferedReader br = null;
String line = "";
String delimiter = "|";

try {

br = new BufferedReader(new FileReader(paramFile));
while ((line = br.readLine()) != null) {

// use comma as separator
String[] params = line.split(delimiter);

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

System.out.println("Done");
}

}

So what I would like to do is the parsed data to something like

params.FirstName
params.LastName
params.Car
params.Color
params.Automatic (which is a boolean)

I am getting lost on how to create the params and name them

Any Advice?
 
Marshal
Posts: 69806
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code button; that code is very difficult to read.
You cannot use | as a delimiter, because | is a meta-character. Go through a regular expressions tutorial, e.g. this one, and you find you have to escape it. You probably have to write \\|
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this better to read

{

public void main(String[] args) {


String paramFile = "C:/Java_Test/params.txt";
BufferedReader br = null;
String line = "";
String delimiter = "|";

try {

br = new BufferedReader(new FileReader(paramFile));
while ((line = br.readLine()) != null) {

// use comma as separator
String[] params = line.split(delimiter);

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

System.out.println("Done");
}

}
 
Campbell Ritchie
Marshal
Posts: 69806
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gary Wittlock wrote: . . .

params.FirstName
params.LastName
params.Car
params.Color
params.Automatic (which is a boolean)
. . .

That looks like an embryonic XML file. That looks like code to write a Java class automatically by putting all those data, which are not typed (‍), into a text file and naming it Foo.java. That looks like code to create a data structure on the fly. It looks like quite a lot of things.

The one thing it does not look like is an ordinary Java class. (As you have already been told.)
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops I forgot to paste it between

 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Gary Wittlock wrote: . . .

params.FirstName
params.LastName
params.Car
params.Color
params.Automatic (which is a boolean)
. . .

That looks like an embryonic XML file. That looks like code to write a Java class automatically by putting all those data, which are not typed (‍), into a text file and naming it Foo.java. That looks like code to create a data structure on the fly. It looks like quite a lot of things.

The one thing it does not look like is an ordinary Java class. (As you have already been told.)



Yes it does write a java class automatically.
This is how java files are run in the 3rd party app I am using.
the 3rd party app compiles the java when it is executed.
We can use all of the standard functions in java we just do not compile it.

So I guess (based on your comment "on the fly") that I can not do what I would like.
I just figured that because the file will have a Set number of parameters that I could read them in to a vector or something.

Guess I will just keep plugging away at.
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I will marked this as resolved even though I did not get a soloution.
No sense leaving the post open just to see what is wrong and no solution for the question.
 
Campbell Ritchie
Marshal
Posts: 69806
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can write a text file and call it Foo.java, with those fields. You can call the compiler using Runtime.exec. It ain't easy, but it is possible.
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, you can write a text file and call it Foo.java, with those fields. You can call the compiler using Runtime.exec. It ain't easy, but it is possible.



I believe that I have not explain my issue well enough to get the solution I need.

The isuue is not how the file is compiled or how it is called. The issue is just plain java routines. At least I believe it is.

What I am needing to do is to read a delimited file that has 10 paramewters per line seprated by the vertical bar

I want to assign the values of the parameters to variables. those variables will be something like param.variablename where the variablename is something meaningfull for each of the 10 parameters.

I think my question only has to do with creating a vector or arraylist or something which as I said is just plain java code.

how I use the code is not an issue with me. I am just looking for some information on the basic java code issue.

Thanks for your response
 
Campbell Ritchie
Marshal
Posts: 69806
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a Map where you link the name of the variable to its value, but you cannot tell from the file that the last value is Boolean and the others Strings. You can write your own calss to encapsulate those data, but it would be different for every file you are reading.
 
Gary Wittlock
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to thank everyone, I have figuired out the solution.
 
He does not suffer fools gladly. But this tiny ad does:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic