Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 41
  1. #21
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  2. #22
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    
    public class Testing
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 5000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
    	   int j = 0;
    	   double y[];
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
                   i++;
               }
              fi.close();
    
              try{
    		  FileWriter fw = new FileWriter("C:\\output.txt");
    		  BufferedWriter out = new BufferedWriter(fw);
    		  String Temp = null;
    		  	   for(j=0; j<= i; j++){
    				   Temp += y[j] + "\n";}
    		  out.write(Temp.substring(4));
    		  out.close();
                            }catch (Exception A){}
    
       }
    
    }
    After corected and compiled, another errors come again.
    Testing.java:32: variable y might not have been initialized
    y[j] = ((4.2*a[i]/1010)-1.35);
    ^
    Testing.java:42: variable y might not have been initialized
    Temp += y[j] + "\n";}
    ^
    2 errors
    Why it said variable y might not have been initialized? I might did something wrong there. I am not sure whether my array has been declared properly. Please correct me thanks.

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It seems to me that your method of programming is:

    1) find an error and post it here.
    2) fix what is stated here, then go back to step one.

    I think that somewhere in this loop you have to have:

    3) try to debug it myself.

    Don't you think?

  4. #24
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Even get advice 100 times why these guys don't listen that. Try yourself at least a simple error like this. Error says that 'y' is not initialized. Go through the code and see is it true? If yes initialized it is.

  5. #25
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Yup you can figure that one. Try looking into the tutorials on declaration. Look for how to declare an array. ^_^
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  6. #26
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    
    public class Testing
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 5000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
    	   int j = 0;
    	   final double y[]= new double[PCOUNT];
    
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
                   i++;
               }
              fi.close();
    
              try{
    		  FileWriter fw = new FileWriter("C:\\output.txt");
    		  BufferedWriter out = new BufferedWriter(fw);
    		  String Temp = null;
    		  	   for(j=0; j<= i; j++){
    				   Temp += y[j] + "\n";}
    		  out.write(Temp.substring(4));
    		  out.close();
                            }catch (Exception A){}
    
       }
    
    }
    Hi, I have initialized for the array and now the program can run finally and save into the output.txt file.
    But the there's only a value save in the output.txt file in a row?
    Please guide my problems. Thanks.

    5.0830693069306940.00.00.00.00.00.00.00.00.00.00.0 0.00.00.00.00.00.00.00.00.00.00.00.00.00.0

  7. #27
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You mean that, you want to write line by line, not in a same row? So write a new line in each case.

  8. #28
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Yes, how to make the answer of the calculated value in the output.txt like this?

    3.764851485148515
    3.848019801980198
    5.249405940594061
    6.891980198019802
    3.9062376237623764
    6.347227722772278
    2.8583168316831684
    4.9957425742574255
    5.257722772277228
    5.199504950495049
    3.8812871287128714
    3.8812871287128714
    3.7814851485148515
    5.257722772277228
    6.463663366336634
    6.833762376237624
    5.490594059405941
    4.796138613861386
    4.326237623762376
    5.087227722772278
    3.83970297029703
    4.796138613861386
    3.8771287128712877
    5.2909900990099015
    5.083069306930694

  9. #29
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Add the following line,

    Java Code:
    out.write(System.getProperty("line.separator"));
    after the line,

    Java Code:
    out.write(Temp.substring(4));
    in your code.

  10. #30
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Hi, Eranga. Still the same. I get this in the output.txt file after run.

    5.0830693069306940.00.00.00.00.00.00.00.00.00.00.0 0.00.00.00.00.00.00.00.00.00.00.00.00.00.0
    I wana make the calculated value to be save into the output.txt file,
    3.764851485148515
    3.848019801980198
    5.249405940594061
    6.891980198019802
    3.9062376237623764
    6.347227722772278
    2.8583168316831684
    4.9957425742574255
    5.257722772277228
    5.199504950495049
    3.8812871287128714
    3.8812871287128714
    3.7814851485148515
    5.257722772277228
    6.463663366336634
    6.833762376237624
    5.490594059405941
    4.796138613861386
    4.326237623762376
    5.087227722772278
    3.83970297029703
    4.796138613861386
    3.8771287128712877
    5.2909900990099015
    5.083069306930694

    I have done some research on array, but cannot get it.
    Please guide the problems. Thanks

  11. #31
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  12. #32
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    
    public class Testing
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 5000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
    	   int j = 0;
    	   final double y[]= new double[PCOUNT];
    
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
                   i++;
               }
              fi.close();
    
              try{
    		  FileWriter fw = new FileWriter("C:\\output.txt");
    		  BufferedWriter out = new BufferedWriter(fw);
    		  String Temp = null;
    		  	   for(j=0; j<= i; j++){
    				   Temp += y[j] + "\n";}
    		  out.write(Temp.substring(4));
    		  out.write(System.getProperty("line.separator"));
    		  out.close();
                            }catch (Exception A){}
    
       }
    
    }
    I have added the code after that, but still the same at my C:\output.txt
    which I get tis answers is not correct:
    5.0830693069306940.00.00.00.00.00.00.00.00.00.00.0 0.00.00.00.00.00.00.00.00.00.00.00.00.00.0

  13. #33
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First check that you logic is working fine or not. I can see in your code, only the first time do the calculation and after that nothing is changed. Actually the above output is not what you get, isn't it? You get a output stream with an annoying character like '[]', isn't it? Error is not with the new line print, it's an error with your application logic.

  14. #34
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    hehehe i got your problem. I used \n for next line. if you used notepad to open the file you would see a straigth line. try using Wordpad ^_^ Wordpad can interpret text formatting such as \n. ^_^

    Now for the zero output. There is just one wrong variable in the logic. you just have to look for it. Its a simple error ^_^ Try finding it ^_^

    HINT: It is inside the while loop
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  15. #35
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Eku View Post
    HINT: It is inside the while loop
    ;) You got the point. Let see our friend can solve it out.

  16. #36
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Hi, thanks Eku and Eranga. I got the right answers now.

    The code part changed,
    Java Code:
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
                   i++;
                   j++;
               }
    I have one more problem is tat the calculated value is not in row by row.

    All the output is in one very long row only.
    3.764851485148515
    3.8480198019801985.2494059405940616.89198019801980 23.90623762376237646.3472277227722782.858316831683 16844.99574257425742555.2577227722772285.199504950 4950493.88128712871287143.88128712871287143.781485 14851485155.2577227722772286.4636633663366346.8337 623762376245.4905940594059414.7961386138613864.326 2376237623765.0872277227722783.839702970297034.796 1386138613863.87712871287128775.29099009900990155. 083069306930694
    0.0
    Can you guide me how to make them in row by row like this?
    3.764851485148515
    3.848019801980198
    5.249405940594061
    6.891980198019802
    3.9062376237623764
    6.347227722772278
    2.8583168316831684
    4.9957425742574255
    5.257722772277228
    5.199504950495049
    3.8812871287128714
    3.8812871287128714
    3.7814851485148515
    5.257722772277228
    6.463663366336634
    6.833762376237624
    5.490594059405941
    4.796138613861386
    4.326237623762376
    5.087227722772278
    3.83970297029703
    4.796138613861386
    3.8771287128712877
    5.2909900990099015
    5.083069306930694

  17. #37
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Your data written part should be like this.

    Java Code:
        try{
            FileWriter fw = new FileWriter("C:\\output.txt");
            BufferedWriter out = new BufferedWriter(fw);
    
            for(j=0; j<= i; j++) {
                double Temp = 0;
                Temp += y[j];
                String temp = Double.toString(Temp);
                out.write(temp);
                out.write(s);
                out.flush();
            }
            out.close();
            fw.close();
        }
        catch (Exception A) {
        }

  18. #38
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Hi Eranga, I get this error.

    Testing.java:48: cannot find symbol
    symbol : variable s
    location: class Testing
    out.write(s);
    ^
    1 error
    Where should I declare the variable "s"?

  19. #39
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ah, that's the new line character I used. Replace it with following.

    Java Code:
    out.write("\n");

  20. #40
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Eranga, I still got this in the text file. It is not in row by row.

    It works fine when I open the "output.txt" file using other text editor such as textpad.
    How to make it so that when I open with in text file mode, the output numbers can be in row by row.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Need a solution to read and store data from a file
    By sheetalnri in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 06:43 AM
  2. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 02:22 PM
  3. [SOLVED] getting values from a text file
    By dav9999 in forum New To Java
    Replies: 8
    Last Post: 04-01-2008, 01:51 AM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM
  5. How to read attributes and values in a xml file using servlet
    By pragathi_forum in forum Advanced Java
    Replies: 1
    Last Post: 12-18-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •