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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filtering and rearranging text files

Status
Not open for further replies.

bos

Technical User
Oct 1, 2002
50
US
I have a text file which displays information in row and column form as follows:

1, 1 Optimal etc etc etc
1, 2 Optimal etc etc etc
1, 3 Replaced etc etc etc

I would like to be able to take the information from this text file and put it in another text file in the following manner:

1
1
Optimal
1
2
Optimal

Basically I want to write the first number and second number without the comma. Then I'd like the status to be written. The other information on the chart I want to ignore. Is there a way that I can filter this information and display it in the way that I want? Please let me know how. Thanks in advance.
 
Use this code to open and read the file:

String fileName = ????;
FileReader fr = null;
try
{
fr = new FileReader( new File( fileName ) );
} catch( Exception e ){}
BufferedReader br = new BufferedReader( fr );

boolean done = false;
while( !done )
{
String line = br.readLine();
// Parse the line here
}

You can parse each line you want using a StringTokenizer where the token for parsing is a ", ". That will seperate the line based on , and " "'s.

Hope that helps.

-gc "I don't look busy because I did it right the first time."
 
Thanks for the advice. To be honest I'm not familiar with using string Tokenizers or parsing lines. Could you possibly give me an example of how to separate the lines if a ", " or a space exists? Thank you.
 
Given the follwing String:

String example = "1, 1 Optimal etc etc etc";

You can use the StringTokenizer like this:

StringTokenizer st = new StringTokenizer( example, " ," );
while( st.hasMoreTokens() )
{
System.out.println( st.nextToken() );
}

This will print out:

1
1
Optimal
etc
etc
etc

Hope that helps.

-gc "I don't look busy because I did it right the first time."
 
I tried what you wrote and it worked great. I just have one last concern. The program I implemented the Tokenizer in was a User Interface. When I wrote the lines to System.out.print everything worked great although the interface would not come up. I tried appending the lines to a text area on the interface after that yet the interface not longer appears after running the program. What might have cause the interface to no longer appear after I entered in the code you gave me? Thanks.
 
You may have to explain to me a little better what is going on because I'm not quite sure what you mean. If you could post your code, I could probably look at it, and tell you what the problem is.

-gc "I don't look busy because I did it right the first time."
 
Sorry for not being clearer. Here is the code for the whole program. The code you gave me is nearer to the bottom with /////////////// on the top on bottom.

Basically the problem is:

When I use the code at the bottom surrounded by ///////
the User Interface will not open. When I block out the code it will open. Thanks very much for your time and help.


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.border.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.util.*;

public class fail2 extends JFrame {
ImageIcon controller = new ImageIcon("./controller3.gif");
ImageIcon dricon = new ImageIcon("./drive.gif");
JFrame frame;
JPanel mainpanel = new JPanel();
JPanel drivepanel = new JPanel();
JPanel selectpanel = new JPanel();
JPanel optionpanel = new JPanel();
JScrollPane drivescroll = new JScrollPane(drivepanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane selectscroll = new JScrollPane(selectpanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JButton[][] drive = new JButton[6][15];
JPanel[] tray = new JPanel[6];
JButton conta = new JButton(controller);
JButton contb = new JButton(controller);
JLabel a = new JLabel("A");
JLabel b = new JLabel("B");
JPanel contpanel = new JPanel();
JTextField[] traynum = new JTextField[6];
JRadioButton fail = new JRadioButton("Fail", true);
JRadioButton revive = new JRadioButton("Revive");
JRadioButton alt = new JRadioButton("Alternate");
JRadioButton allsleep = new JRadioButton("Sleep");
JButton sleep = new JButton("Sleep");
JButton anydrive = new JButton("Drive");
JTextField drivefield = new JTextField(3);
JButton clear = new JButton("Clear");
ButtonGroup bg1 = new ButtonGroup();
JTextField sleepfield = new JTextField(3);
int vv1;
int vv2;
JLabel[] message = new JLabel[1001];
String[] thecode = new String[1001];
int count=0;
String infostring="";
JTextArea area51 = new JTextArea(7, 50);


public fail2() {
super("TAF");
setSize(750, 900);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 5, 5);
FlowLayout flow2 = new FlowLayout(FlowLayout.LEFT, 15, 15);
FlowLayout flow3 = new FlowLayout(FlowLayout.LEFT, 7, 25);
FlowLayout flow4 = new FlowLayout(FlowLayout.LEFT, 15, 5);
fail.setBounds(10, 10, 40, 25);
revive.setBounds(60, 10, 60, 25);
alt.setBounds(140, 10, 90, 25);
sleep.setBounds(10, 45, 70, 25);
sleepfield.setBounds(90, 45, 30, 20);
sleepfield.setText("10");
allsleep.setBounds(140, 45, 80, 25);
anydrive.setBounds(10, 80, 70, 25);
drivefield.setBounds(90, 80, 30, 20);
drivefield.setText("1");
clear.setBounds(140, 80, 70, 25);
bg1.add(fail);
bg1.add(revive);
contpanel.setLayout(null);
contpanel.setBackground(new Color(250, 250, 250));
contpanel.setBorder(new EtchedBorder());
contpanel.setPreferredSize(new Dimension(385, 95));
conta.setBorder(BorderFactory.createEmptyBorder());
conta.setBounds(25, 10, 250, 30);
a.setBounds(10, 10, 15, 30);
conta.setBackground(new Color(250, 250, 250));
contb.setBorder(BorderFactory.createEmptyBorder());
contb.setBackground(new Color(250, 250, 250));
contb.setBounds(25, 50, 250, 30);
b.setBounds(10, 50, 15, 30);
contpanel.add(a);
contpanel.add(conta);
contpanel.add(b);
contpanel.add(contb);
selectpanel.setLayout(flow4);
selectscroll.setBackground(new Color(150, 150, 150));
selectscroll.setBounds(13, 290, 247, 509);
selectscroll.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLoweredBevelBorder(), new EtchedBorder()));
selectpanel.setPreferredSize(new Dimension(220, 300));
drivepanel.setLayout(flow2);
drivepanel.setBackground(new Color(250, 250, 250));
drivepanel.setBounds(270, 135, 446, 664);
drivepanel.setBackground(new Color(250, 250, 250));
drivepanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLoweredBevelBorder(), new EtchedBorder()));
optionpanel.setLayout(null);
optionpanel.setBounds(13, 135, 247, 150);
optionpanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), new EtchedBorder()));
optionpanel.add(fail);
optionpanel.add(revive);
optionpanel.add(alt);
optionpanel.add(sleep);
optionpanel.add(sleepfield);
optionpanel.add(allsleep);
optionpanel.add(anydrive);
optionpanel.add(drivefield);
optionpanel.add(clear);
area51.setBounds(7, 7, 400, 80);
mainpanel.setLayout(null);
mainpanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), new EtchedBorder()));
mainpanel.setBounds(5, 5, 729, 857);
mainpanel.add(selectscroll);
mainpanel.add(drivepanel);
mainpanel.add(optionpanel);
drivepanel.add(contpanel);
mainpanel.add(area51);


for (vv1=0; vv1<=5; vv1++) {
tray[vv1] = new JPanel();
tray[vv1].setLayout(flow3);
tray[vv1].setBorder(new EtchedBorder());
tray[vv1].setBackground(new Color(250, 250, 250));
tray[vv1].setPreferredSize(new Dimension(385, 75));
traynum[vv1] = new JTextField(2);
traynum[vv1].setText(&quot;&quot; + vv1);
tray[vv1].add(traynum[vv1]);
for (vv2=1; vv2<=14; vv2++) {
drive[vv1][vv2] = new JButton(dricon);
drive[vv1][vv2].setBorder(BorderFactory.createEmptyBorder());
drive[vv1][vv2].setBackground(new Color(250, 250, 250));
drive[vv1][vv2].setToolTipText(&quot;Drive: &quot;+vv2);
tray[vv1].add(drive[vv1][vv2]);
final int tr=vv1;
final int dr=vv2;
drive[tr][dr].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (alt.isSelected()) {
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: <font color=990000>Fail Drive &quot;+traynum[tr].getText()+&quot;-&quot;+dr+&quot; </html>&quot;);
selectpanel.add(message[count]);
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: Sleep &quot;+sleepfield.getText()+&quot; </html>&quot;);
selectpanel.add(message[count]);
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: <font color=blue>Revive Drive &quot;+traynum[tr].getText()+&quot;-&quot;+dr+&quot; </html>&quot;);
selectpanel.add(message[count]);
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: Sleep &quot;+sleepfield.getText()+&quot; </html>&quot;);
selectpanel.add(message[count]);
selectpanel.setVisible(false);
selectpanel.setVisible(true);
} else {
if (fail.isSelected()) {
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: <font color=990000>Fail Drive &quot;+traynum[tr].getText()+&quot;-&quot;+dr+&quot; </html>&quot;);
selectpanel.add(message[count]);
if (allsleep.isSelected()) {
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: Sleep &quot;+sleepfield.getText()+&quot; </html>&quot;);
selectpanel.add(message[count]);
}
}
if (revive.isSelected()) {
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: <font color=blue>Revive Drive &quot;+traynum[tr].getText()+&quot;-&quot;+dr+&quot; </html>&quot;);
selectpanel.add(message[count]);
if (allsleep.isSelected()) {
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: Sleep &quot;+sleepfield.getText()+&quot; </html>&quot;);
selectpanel.add(message[count]);
}
}
}
selectpanel.setVisible(false);
selectpanel.setVisible(true);
if (count > 25) {
selectpanel.setPreferredSize(new Dimension(220, 2000));
}
if (count > 100) {
selectpanel.setPreferredSize(new Dimension(220, 8000));
}
if (count> 400) {
selectpanel.setPreferredSize(new Dimension(220, 20000));
}
}
});


}
drivepanel.add(tray[vv1]);
}
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectpanel.removeAll();
selectpanel.setVisible(false);
selectpanel.setVisible(true);
count=0;
selectpanel.setPreferredSize(new Dimension(220, 300));
}
});
sleep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count=count+1;
message[count] = new JLabel(&quot;<html><b><font color=black>Step &quot;+count+&quot;: Sleep &quot;+sleepfield.getText()+&quot; </html>&quot;);
selectpanel.add(message[count]);
selectpanel.setVisible(false);
selectpanel.setVisible(true);
}
});



//////////////////////////////////////////////

//String filename = &quot;profile2.txt&quot;;
//FileReader fr = null;
//try {
// fr = new FileReader(new File(filename));
// } catch (Exception e){}
//BufferedReader br = new BufferedReader(fr);
//boolean done=false;
//while(!done)
// {
// try {
// String line = br.readLine();
//StringTokenizer st1 = new StringTokenizer(line, &quot;, &quot;);
//while (st1.hasMoreTokens())
// {
// infostring=infostring+line;
// area51.append(line);
//}
//} catch (Exception e){}

//}

////////////////////////////////////////////////

pane.setLayout(null);
pane.add(mainpanel);
setVisible(true);
}

public static void main(String[] arguments) {
fail2 fa = new fail2();
//Toolkit tk = Toolkit.getDefaultToolkit();
//Image image1 = tk.getImage(&quot;./failmin.gif&quot;);
//fa.setIconImage(image1);
}
}
 
Guess I should have explained my code a little better. When you load in the file you are using, you should probably handle the exception.

The way the code sits now, if you have an exception in one of the try-catch blocks, it is just throw away. My guess is there's an exception being thrown somewhere in there and that is causing your program to act a little wierd.

If after looking into that you still have problems, send me a sample profile file and I will try to run it with the StringTokenizer code commented back in.

-gc &quot;I don't look busy because I did it right the first time.&quot;
 
ok... Here's the problem. The reason your GUI isn't showing up is because you are going into an infinite loop, int the commented out code. Specifically the while( st.hasMoreTokens() ) loop. The reason for that is that the string tokenizer always has more tokens because you never use st.nextToken() which will advance the pointer to the next token. Try adding st.nextToken() to your code, and you may get some better results.

-gc &quot;I don't look busy because I did it right the first time.&quot;
 
Alright, last post for ya. Sorry, about the previous posts, but I was posting as I was thinking about your problem. I think the code you want is this:

//////////////////////////////////////////////
String filename = &quot;profile2.txt&quot;;
FileReader fr = null;
try
{
fr = new FileReader(new File(filename));
} catch (Exception e){ e.printStackTrace( System.out ); }

BufferedReader br = new BufferedReader( fr );
boolean done = false;
while( !done )
{
try
{
String line = br.readLine();
if( line != null )
{
StringTokenizer st1 = new StringTokenizer( line, &quot;, &quot; );
area51.append( st1.nextToken() + &quot; &quot; + st1.nextToken() + &quot; &quot; + st1.nextToken() + &quot;\n&quot; );
}
else
done = true;
} catch (Exception e){ e.printStackTrace( System.out ); }
}
////////////////////////////////////////////////

See if that works.

-gc &quot;I don't look busy because I did it right the first time.&quot;
 
That is exactly what I needed. Thank you for answering my questions so completely and accurately. You've been a huge help.
 
no problemo [thumbsup]

-gc &quot;I don't look busy because I did it right the first time.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top