String Parsing and File I/O

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 651 Views - Last Post: 17 May 2011 - 12:03 AM Rate Topic: -----

#1 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

String Parsing and File I/O

Posted 15 May 2011 - 09:47 PM

public void run() {


      double result = 0.0;
      int count = 0;
      double firstValue = 0, nextValue;
      try {
                        
      FileWriter writer = new FileWriter(file);
      BufferedReader r = new BufferedReader(new FileReader(File));
      StreamTokenizer stok = new StreamTokenizer(r);
      
      // edit the inner if statements for your desired results
      if (stok.ttype == StreamTokenizer.TT_NUMBER ) {
                                    
      if (count == 0) {
       firstValue = stok.nval;
      } else if (stok.ttype == StreamTokenizer.TT_WORD) {
      nextValue = stok.nval;
      stok.sval = stok.sval.substring(stok.sval.indexOf(".m") + 1).replaceAll("meta", "");
      result = firstValue - nextValue;
      double round = Roundoff((float) result, 4);
      firstValue = nextValue;
      writer.append(round + "\n");

      }
       count++;
     }
                                
      }
       writer.close();
       r.close();// always close the buffered reader// end of method
       }
   } catch (IOException ex) {
     Logger.getLogger(P__C.class.getName()).log(Level.SEVERE, null, ex);
   }
}


What this code does, first it will read a file. Then the will take the first line's value of the file minus away the second line's value of the file follow by next line. However the problem I facing is at this part :
 else if (stok.ttype == StreamTokenizer.TT_WORD) {
       nextValue = stok.nval;
       stok.sval = stok.sval.substring(stok.sval.indexOf(".m") + 1).replaceAll("meta", "");
       result = firstValue - nextValue;
       double round = Roundoff((float) result, 4);
       firstValue = nextValue;
       writer.append(round + "\n");
}


The reason I use .substring(stok.sval.indexOf(".m") + 1) as I want to remove the file name.
Because the contents of the file will be something like :
Name of the file : 8546.meta  value :5.64
Name of the file : 5863.meta  value :5.46

I need to remove the name of the file so the value 5.64 will only minus 5.46 instead of 85465.64 - 58635.46.
How can I possible solve this problem ?

This post has been edited by macosxnerd101: 15 May 2011 - 09:55 PM
Reason for edit:: Title renamed to be more descriptive. Please avoid using titles such as "help me", "I'm stuck", or "I'm a noob".


Is This A Good Question/Topic? 0
  • +

Replies To: String Parsing and File I/O

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: String Parsing and File I/O

Posted 15 May 2011 - 10:34 PM

You could be using a Properties file for this (look it up in the java api)

If you want only the value then you can do it like
BufferedReader r = new BufferedReader(new FileReader(File));

String line = "";
while ( r.ready() ) {
	line = r.readLine();
	int value1 = Integer.parseInt( line.substring( line.lastIndexOf( ":" ) ));
}


This post has been edited by CasiOo: 15 May 2011 - 11:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: String Parsing and File I/O

Posted 15 May 2011 - 10:53 PM

public void run() {


      double result = 0.0;
      int count = 0;
      double firstValue = 0, nextValue;
      try {
                        
      FileWriter writer = new FileWriter(file);
      BufferedReader r = new BufferedReader(new FileReader(File));
      
      StreamTokenizer stok = new StreamTokenizer(r);
      String line = "";
      while ( r.ready() ) {
	line = r.readLine();
	line = line.substring( line.lastIndexOf( ":" ) );
       }
      
      // edit the inner if statements for your desired results
      if (stok.ttype == StreamTokenizer.TT_NUMBER ) {
                                    
      if (count == 0) {
       firstValue = stok.nval;
      } else if (stok.ttype == StreamTokenizer.TT_WORD) {
      nextValue = stok.nval;
      stok.sval = stok.sval.substring(stok.sval.indexOf(".m") + 1).replaceAll("meta", "");
      result = firstValue - nextValue;
      double round = Roundoff((float) result, 4);
      firstValue = nextValue;
      writer.append(round + "\n");

      }
       count++;
     }
                                
      }
       writer.close();
       r.close();// always close the buffered reader// end of method
       }
   } catch (IOException ex) {
     Logger.getLogger(P__C.class.getName()).log(Level.SEVERE, null, ex);
   }
}
Ok I had add them into my code. Isit like this that you are try to say ?
How can implement the String line to the stok.nal ? so that it will be notifiate.


Change 


BufferedReader r = new BufferedReader(new FileReader(File));
String line = "";
while ( r.ready() ) {
line = r.readLine();
int value1 = line.substring( line.lastIndexOf( ":" ) );
}
to
stok.sval =stok.sval.substring(stok.sval.lastIndexOf(":"));

????

This post has been edited by xxxjayxxx: 15 May 2011 - 11:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: String Parsing and File I/O

Posted 15 May 2011 - 11:07 PM

public void run() {


      double result = 0.0;
      int count = 0;
      double firstValue = 0, nextValue;
      try {
                        
      FileWriter writer = new FileWriter(file);
      BufferedReader r = new BufferedReader(new FileReader(File));
      
      StreamTokenizer stok = new StreamTokenizer(r);
      String line = "";
      while ( r.ready() ) {
	line = r.readLine();
	line = line.substring( line.lastIndexOf( ":" ) );
       }
      
      // edit the inner if statements for your desired results
      if (stok.ttype == StreamTokenizer.TT_NUMBER ) {
                                    
      if (count == 0) {
       firstValue = stok.nval;
      } else if (stok.ttype == StreamTokenizer.TT_WORD) {
      nextValue = stok.nval;
      stok.sval = stok.sval.substring(stok.sval.indexOf(".m") + 1).replaceAll("meta", "");
      result = firstValue - nextValue;
      double round = Roundoff((float) result, 4);
      firstValue = nextValue;
      writer.append(round + "\n");

      }
       count++;
     }
                                
      }
       writer.close();
       r.close();// always close the buffered reader// end of method
       }
   } catch (IOException ex) {
     Logger.getLogger(P__C.class.getName()).log(Level.SEVERE, null, ex);
   }
}




Ok I had add them into my code. Isit like this that you are try to say ?
How can implement the String line to the stok.nal ? so that it will be notifiate.


Change


BufferedReader r = new BufferedReader(new FileReader(File));
String line = "";
while ( r.ready() ) {
	line = r.readLine();
	int value1 = line.substring( line.lastIndexOf( ":" ) );
}


to

stok.sval =stok.sval.substring(stok.sval.lastIndexOf(":"));

????

This post has been edited by xxxjayxxx: 15 May 2011 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: String Parsing and File I/O

Posted 15 May 2011 - 11:08 PM

Sorry I was a little bit too quick. I forgot Integer.parseInt( ) on the substring you get.

I think you are making this task far more complex than it actually is.

BufferedReader r = new BufferedReader(new FileReader(File));
    String line = "";
    int value1, value2;
    int count = 1;
    while ( r.ready() ) {
      line = r.readLine();
      if (count % 2 == 0) {
        value2 = Integer.parseInt( line.substring( line.lastIndexOf( ":" ) ));
        System.out.println( value1 - value2 );
      }
      else
        value1 = Integer.parseInt( line.substring( line.lastIndexOf( ":" ) ));
      count++;
    }


This post has been edited by CasiOo: 15 May 2011 - 11:23 PM

Was This Post Helpful? 0
  • +
  • -

#6 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: String Parsing and File I/O

Posted 15 May 2011 - 11:25 PM

brb

This post has been edited by xxxjayxxx: 15 May 2011 - 11:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: String Parsing and File I/O

Posted 15 May 2011 - 11:33 PM

Ok thx.. Just to double check with you my final coding will be :
try {
                        if (Save_Tanimoto_Difference.getText().trim().length() == 0) {
                            JOptionPane.showMessageDialog(null, "\t\tPlease select a directory to save the Tanimoto Difference results!", "Missing Directory", JOptionPane.WARNING_MESSAGE);
                        } else if (Tanimoto_file.length() != 0) {
                            FileWriter writer = new FileWriter(file);
                            BufferedReader r = new BufferedReader(new FileReader(Tanimoto_file));

                            String line = "";
                            int value1 = 0, value2;
                            int count = 1;
                            while (r.ready()) {
                                line = r.readLine();
                                if (count % 2 == 0) {
                                    value2 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 1));
                                    System.out.println(value1 - value2);
                                    
                                } else {
                                    value1 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 1));
                                }
                                count++;
                            }
                            // count++;

                            writer.close();
                            r.close();// always close the buffered reader// end of method
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(FYP__ZZC.class.getName()).log(Level.SEVERE, null, ex);
                    }


But there some error when I debug. Which is :
Exception in thread "Thread-5" java.lang.NumberFormatException: For input string: ":0.4552"



Which I click on the error it brings me to :
 value1 = Integer.parseInt(line.substring(line.lastIndexOf(":")));


and the content of my file is :
Name: M6628.meta value:0.4552
Name: H9523.meta value:0.4316
Name: M-109.meat value:0.4043
Name: I1899.meta value:0.3966
Name: H8876.meta value:0.3956
Name: H9772.meta value:0.3911
Name: 291552.meta value:0.3777

This post has been edited by xxxjayxxx: 16 May 2011 - 12:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: String Parsing and File I/O

Posted 16 May 2011 - 12:30 AM

You will have to use + 1 to go one character aftr the :

value1 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 1));

or the String will contains the : as the reported error very clearly says
string: ":0.4552"
Was This Post Helpful? 0
  • +
  • -

#9 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: String Parsing and File I/O

Posted 16 May 2011 - 12:42 AM

View Postpbl, on 16 May 2011 - 12:30 AM, said:

You will have to use + 1 to go one character aftr the :

value1 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 1));

or the String will contains the : as the reported error very clearly says
string: ":0.4552"


ok thx .. I had try to troublesome using yr method but still the same.

while (r.ready()) {
      line = r.readLine();
      if (count % 2 == 0) {
      value2 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 1));
      System.out.println(value1 - value2);
      } else {
      value1 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 1));
      }
      count++;
     }


Exception in thread "Thread-5" java.lang.NumberFormatException: For input string: "0.4552"




while (r.ready()) {
      line = r.readLine();
      if (count % 2 == 0) {
      value2 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 2));
      System.out.println(value1 - value2);
      } else {
      value1 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 2));
      }
      count++;
     }



Exception in thread "Thread-5" java.lang.NumberFormatException: For input string: ".4552"



while (r.ready()) {
      line = r.readLine();
      if (count % 2 == 0) {
      value2 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 3));
      System.out.println(value1 - value2);
      } else {
      value1 = Integer.parseInt(line.substring(line.lastIndexOf(":") + 3));
      }
      count++;
     }



Exception in thread "Thread-5" java.lang.NumberFormatException: For input string: " 4552"

Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: String Parsing and File I/O

Posted 16 May 2011 - 01:12 AM

when you write 0.456 for example then it needs to be a double or float. It will give you an error because it cannot be parsed to an int. Instead parse it to a double :)

Double.parseDouble ( insert here! );

This post has been edited by CasiOo: 16 May 2011 - 01:13 AM

Was This Post Helpful? 0
  • +
  • -

#11 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: String Parsing and File I/O

Posted 16 May 2011 - 01:28 AM

View PostCasiOo, on 16 May 2011 - 01:12 AM, said:

when you write 0.456 for example then it needs to be a double or float. It will give you an error because it cannot be parsed to an int. Instead parse it to a double :)

Double.parseDouble ( insert here! );


Thanks bro :) ...
??

This post has been edited by xxxjayxxx: 16 May 2011 - 01:29 AM

Was This Post Helpful? 0
  • +
  • -

#12 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: String Parsing and File I/O

Posted 16 May 2011 - 01:36 AM

View Postxxxjayxxx, on 16 May 2011 - 01:28 AM, said:

View PostCasiOo, on 16 May 2011 - 01:12 AM, said:

when you write 0.456 for example then it needs to be a double or float. It will give you an error because it cannot be parsed to an int. Instead parse it to a double :)

Double.parseDouble ( insert here! );


Thanks bro :) ...
??


The questionmarks tells us that you now are more confused than you were to begin with? Sorry for that...
Was This Post Helpful? 0
  • +
  • -

#13 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: String Parsing and File I/O

Posted 16 May 2011 - 01:42 AM

ok one last question ... If lets say the content for the file is :
Name:5678.meta  value:0.8
Name:6789.meta  value:0.6


How can I let the output to be like:
5678.meta - 6789.meta = 0.2

??

I try to code it but still unable to get:
String line = "";
      float value1 = 0, value2;
      String Name1 = null , Name2;
      int count = 1;
      while (r.ready()) {
      line = r.readLine();
 if (count % 2 == 0) {
      value2 = Float.parseFloat(line.substring(line.lastIndexOf(":") + 1));
      Name2 = line.substring(line.indexOf(" ") + 10).replaceAll("Name:", "");
     System.out.println(Name1 + "-" + Name2);
      System.out.println(value1 - value2);
     } else {
       value1 = Float.parseFloat(line.substring(line.lastIndexOf(":") + 1));
       Name1 = line.substring(line.indexOf(" ") + 10).replaceAll("Name:", "");
       }
       count++;
       }


Btw sorry I just notice that the code I uses only can achieve :
]Name: M6628.meta value:0.4552
Name: H9523.meta value:0.4316
Name: M-109.meat value:0.4043
Name: I1899.meta value:0.3966
Name: H8876.meta value:0.3956
Name: H9772.meta value:0.3911
Name: 291552.meta value:0.3777



0.4552 - 0.4316 
0.4043 - 0.3966
0.3956 - 0.3911




instead of:
0.4552 - 0.4316
0.4316 -0.4043
0.4043 - 0.3966
0.3966 - 0.3956
0.3011 - 0.3777


What I was trying to say it can the value 1 - value 2 ; value 2 - value 3 ; value 3 - value 4 ?
is it gonna do with :
if (count % 2 == 0)


??

This post has been edited by xxxjayxxx: 16 May 2011 - 02:50 AM

Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: String Parsing and File I/O

Posted 16 May 2011 - 02:53 AM

Using a StreamTokenizer in your case is not really appropriate. Use Scanner and process lines
Was This Post Helpful? 0
  • +
  • -

#15 xxxjayxxx  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 71
  • Joined: 01-August 10

Re: String Parsing and File I/O

Posted 16 May 2011 - 05:42 AM

yup I had change to :
String line = "";
      float value1 = 0, value2;
      String Name1 = null , Name2;
      int count = 1;
      while (r.ready()) {
      line = r.readLine();
 if (count % 2 == 0) {
      value2 = Float.parseFloat(line.substring(line.lastIndexOf(":") + 1));
      Name2 = line.substring(line.indexOf(" ") + 10).replaceAll("Name:", "");
     System.out.println(Name1 + "-" + Name2);
      System.out.println(value1 - value2);
     } else {
       value1 = Float.parseFloat(line.substring(line.lastIndexOf(":") + 1));
       Name1 = line.substring(line.indexOf(" ") + 10).replaceAll("Name:", "");
       }
       count++;
       }

but I now stuck again
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2