Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37
  1. #21
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by jurka View Post
    So how far are you with text reading ?
    Currently, the program can only read a textfile, wana make it to read two textfiles at the same time and then sum up the value line by line.
    Now I was having problem with initializing both inLine1 and inLine2 which declared in the main part there to either null or "", as I get either all null or all blank ouputs.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Please copy and paste the errors if you need help getting the program to compile.
    Otherwise please explain your problem and post your code and/or the output.

  3. #23
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please copy and paste the errors if you need help getting the program to compile.
    Otherwise please explain your problem and post your code and/or the output.
    Java Code:
    import java.util.ArrayList;
    import java.io.*;
    public class ReadTextFiles
    {
      public static void main(String[] args)
        throws java.io.IOException
      {
        String fileName1 = "InputFile1.txt";
        String fileName2 = "InputFile2.txt";
        int i;
        String inLine1=null;
        String inLine2=null;
    
          // set up input stream1
        FileReader fr1 = new
            FileReader(fileName1);
          // buffer the input stream
        BufferedReader br1 =
            new BufferedReader(fr1);
    
          // set up input stream2
        FileReader fr2 = new
            FileReader(fileName2);
          // buffer the input stream
        BufferedReader br2 =
            new BufferedReader(fr2);
    
          // read and display1
        String buffer1 = "";
    
        ArrayList<String> File1 = new ArrayList<String>();
        while ((buffer1 = br1.readLine()) != null) {
        	File1.add(buffer1);
          System.out.println("File1 values: "+ inLine1);  // display the line
      }
      br1.close();
    
    
    
        //Now read the second file or make for this separate method
          // read and display2
        String buffer2 = "";
    
        ArrayList<String> File2 = new ArrayList<String>();
        while ((buffer2 = br2.readLine()) != null) {
        	File2.add(buffer2);
          System.out.println("File2 values: " + inLine2);  // display the line
      }
      br2.close();
    
        //Read all the lines in array or list
        //After that you can calculate them.
     }
    
    }
    Output at the program:
    Java Code:
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File1 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    File2 values: null
    I tried to initialized both inLine1 and inLine2 which declared in the main part there to either null or "", then I get either all null or all blank.

    Should be getting something like this:
    Java Code:
    InputFile1   InputFile2
    1.2		1.3
    0.1		9.1
    2.5		2.9
    4.6		0.6
    9.1		9.1
    10.3		10.8
    12.6		17.6
    2.2		22.2
    7.1		7.11
    4.51		3.51
    4.6		0.6
    19.1		9.1
    10.3		0.3
    0.0		0.01
    1.22		1.22
    -2.1		-2.1
    9.5		9.51
    12		2
    43		4.3
    77.1		7.1
    0.5

  4. #24
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Ok i post my file reading class here, it should make your life easier
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class FileReaderByLine {
    	
    	private File file;
    	private BufferedReader br;
    	private FileReader fr;
    	
    	
    	public FileReaderByLine(File f) throws FileNotFoundException {
    		this.file = f;
    		fr = new FileReader(file);
    		br = new BufferedReader(fr);
    		
    	}
    	
    	public ArrayList<String> readFile() throws IOException {
    		String s;
    		ArrayList<String> fileList = new ArrayList<String>();
    		
    		while ((s = br.readLine()) != null) {
    			fileList.add(s.trim());
    		}
    		br.close();
    		return fileList;
    	}
    }
    Java Code:
    FileReaderByLine fileOne = FileReaderByLine(new File("Here write your location"));
    FileReaderByLine fileSecond = FileReaderByLine(new File("Here write your location"));
    
    ArrayList<String> firstList = fileOne.readFile();
    ArrayList<String> secondList = fileSecond.readFile();
    
    //Now you can calculate the values.

  5. #25
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by jurka View Post
    Ok i post my file reading class here, it should make your life easier
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class FileReaderByLine {
    	
    	private File file;
    	private BufferedReader br;
    	private FileReader fr;
    	
    	
    	public FileReaderByLine(File f) throws FileNotFoundException {
    		this.file = f;
    		fr = new FileReader(file);
    		br = new BufferedReader(fr);
    		
    	}
    	
    	public ArrayList<String> readFile() throws IOException {
    		String s;
    		ArrayList<String> fileList = new ArrayList<String>();
    		
    		while ((s = br.readLine()) != null) {
    			fileList.add(s.trim());
    		}
    		br.close();
    		return fileList;
    	}
    }
    Java Code:
    FileReaderByLine fileOne = FileReaderByLine(new File("Here write your location"));
    FileReaderByLine fileSecond = FileReaderByLine(new File("Here write your location"));
    
    ArrayList<String> firstList = fileOne.readFile();
    ArrayList<String> secondList = fileSecond.readFile();
    
    //Now you can calculate the values.
    Can I know how do you make the FileReaderByLine class to work with the main program in order to run?

  6. #26
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Ok one way is making another file named FileReaderByLine.java and copy there that class. Second way is to copy that class in same file where you got main method, be careful don't but it inside the main class and dont forgot to delete modifier public. Implementation of this is show at second code block, copy it under main method. And there's no output, if you want see the lists you need loop them.

  7. #27
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by jurka View Post
    Ok one way is making another file named FileReaderByLine.java and copy there that class. Second way is to copy that class in same file where you got main method, be careful don't but it inside the main class and dont forgot to delete modifier public. Implementation of this is show at second code block, copy it under main method. And there's no output, if you want see the lists you need loop them.
    I have chosen the first method you mentioned. Making two files isn't it?
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class FileReaderByLine {
    
    	private File file;
    	private BufferedReader br;
    	private FileReader fr;
    
    
    	public FileReaderByLine(File f) throws FileNotFoundException {
    		this.file = f;
    		fr = new FileReader(file);
    		br = new BufferedReader(fr);
    
    	}
    
    	public ArrayList<String> readFile() throws IOException {
    		String s;
    		ArrayList<String> fileList = new ArrayList<String>();
    
    		while ((s = br.readLine()) != null) {
    			fileList.add(s.trim());
    		}
    		br.close();
    		return fileList;
    	}
    }

    Java Code:
    import java.util.ArrayList;
    import java.io.*;
    public class ReadTextFiles
    {
      public static void main(String[] args)
        throws java.io.IOException
      {
    
    
        FileReaderByLine fileOne = FileReaderByLine(new File("InputFile1.txt"));
    	FileReaderByLine fileSecond = FileReaderByLine(new File("InputFile2.txt"));
    
    	ArrayList<String> firstList = fileOne.readFile();
    	ArrayList<String> secondList = fileSecond.readFile();
    	double list1, list2, total;
    
    	list1 = Double.parseDouble(firstList);
    	list2 = Double.parseDouble(secondList);
    
    	total= list1 + list2;
    
    
    
        System.out.println("File1 values: "+ list1);  // display the lines
        System.out.println("File2 values: " + list2);  // display the lines
        System.out.println("Total values: " + total);  // display the lines
    
    
     }
    
    }
    Can you check for me what I have missed out to link them.
    I got those errors as below.
    Java Code:
    ReadTextFiles.java:10: cannot find symbol
    symbol  : method FileReaderByLine(java.io.File)
    location: class ReadTextFiles
        FileReaderByLine fileOne = FileReaderByLine(new File("InputFile1.txt"));
                                   ^
    ReadTextFiles.java:11: cannot find symbol
    symbol  : method FileReaderByLine(java.io.File)
    location: class ReadTextFiles
            FileReaderByLine fileSecond = FileReaderByLine(new File("InputFile2.txt"
    ));
                                          ^
    ReadTextFiles.java:17: parseDouble(java.lang.String) in java.lang.Double cannot
    be applied to (java.util.ArrayList<java.lang.String>)
            list1 = Double.parseDouble(firstList);
                          ^
    ReadTextFiles.java:18: parseDouble(java.lang.String) in java.lang.Double cannot
    be applied to (java.util.ArrayList<java.lang.String>)
            list2 = Double.parseDouble(secondList);
                          ^
    4 errors
    Did I import correctly?
    Last edited by matt_well; 07-20-2008 at 11:14 PM.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    = FileReaderByLine(new File("InputFile1.txt"));
    With the above the compiler is looking for a method called: FileReaderByLine(..).
    To create a new object use 'new'.
    ReadTextFiles.java:18: parseDouble(java.lang.String) in java.lang.Double cannot
    be applied to (java.util.ArrayList<java.lang.String>)
    list2 = Double.parseDouble(secondList);
    Here the compiler is telling you that the parseDouble() method requires a String as argument but you have entered a java.util.ArrayList<java.lang.String>.

    Change the args to parseDouble() to be a String.

    Java is very fussy about types.

  9. #29
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    With the above the compiler is looking for a method called: FileReaderByLine(..).
    To create a new object use 'new'.

    Here the compiler is telling you that the parseDouble() method requires a String as argument but you have entered a java.util.ArrayList<java.lang.String>.

    Change the args to parseDouble() to be a String.

    Java is very fussy about types.
    Ok, I have included 'new' in the code and fine now.
    Java Code:
        FileReaderByLine fileOne = new FileReaderByLine(new File("InputFile1.txt"));
    	FileReaderByLine fileSecond = new FileReaderByLine(new File("InputFile2.txt"));
    But, another two errors I am stucked,
    Java Code:
    ReadTextFiles.java:17: parseDouble(java.lang.String) in java.lang.Double cannot
    be applied to (java.util.ArrayList<java.lang.String>)
            list1 = Double.parseDouble(firstList);
                          ^
    ReadTextFiles.java:18: parseDouble(java.lang.String) in java.lang.Double cannot
    be applied to (java.util.ArrayList<java.lang.String>)
            list2 = Double.parseDouble(secondList);
                          ^
    2 errors
    Did you mean that I need to convert back using Double.toString?
    I still can't get another two errors meaning.
    Last edited by matt_well; 07-21-2008 at 01:53 AM.

  10. #30
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    You have to get the string back out of the array list
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  11. #31
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    You have to get the string back out of the array list
    Which String do you mean?
    I declared (list1, list2, total) and then parse them to double for calculation. I still didn't get the errors meaning.


    Java Code:
    	ArrayList<String> firstList = fileOne.readFile();
    	ArrayList<String> secondList = fileSecond.readFile();
    	double list1, list2, total;
    
    	list1 = Double.parseDouble(firstList);
    	list2 = Double.parseDouble(secondList);

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    ^
    ReadTextFiles.java:18: parseDouble(java.lang.String) in java.lang.Double cannot
    be applied to (java.util.ArrayList<java.lang.String>)
    list2 = Double.parseDouble(secondList);
    Read the API docs for the parseDouble() method.
    What parameter type does it take?
    The compile thinks it should be a java.lang.String (String for short)
    What have you used for the parameter?
    The compiler thinks it is: java.util.ArrayList<java.lang.String>
    and is unhappy that it's not String and gives you the error message.

    If the ArrayList contains a String that you want to parse into a double, you need to use one of the ArrayList methods to get the String out so that parseDouble() can convert it.

  13. #33
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Read the API docs for the parseDouble() method.
    What parameter type does it take?
    The compile thinks it should be a java.lang.String (String for short)
    What have you used for the parameter?
    The compiler thinks it is: java.util.ArrayList<java.lang.String>
    and is unhappy that it's not String and gives you the error message.

    If the ArrayList contains a String that you want to parse into a double, you need to use one of the ArrayList methods to get the String out so that parseDouble() can convert it.
    I have read the parseDouble() method, and it mentioned that "Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double."

    ArrayList<String> firstFile = new ArrayList<String>(); - this is litlle more advanced but this mostly same as array but its dynamical so you dont need to know the size, in the current code i dont know the size of lines in file, so its best use of it. <String> is generic so we dont need casting and it only stores String type data.
    "ArrayList<String>" is not a string?
    So, what ArrayList methods to get the String out so that parseDouble() can convert it? As I know, I need to convert String to double before do the calculation right? Correct me for my mistake.

  14. #34
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by matt_well View Post
    Which String do you mean?
    I declared (list1, list2, total) and then parse them to double for calculation. I still didn't get the errors meaning.


    Java Code:
    	ArrayList<String> firstList = fileOne.readFile();
    	ArrayList<String> secondList = fileSecond.readFile();
    	double list1, list2, total;
    
    	list1 = Double.parseDouble(firstList);
    	list2 = Double.parseDouble(secondList);
    Heehe you are at right way, but you cant just parse all list, i think so. You need loop every list value and then parse it.

  15. #35
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by jurka View Post
    Heehe you are at right way, but you cant just parse all list, i think so. You need loop every list value and then parse it.
    Any guides how to loop every list value?

  16. #36
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Use a for loop, limit will be the list size get each value parse it into double and then store it in anoother for loop

    Java Code:
    		ArrayList list1 = new ArrayList();
    		List list2 = new List();
    		list1.add("1");
    		list1.add("2");
    		for(int i=0;i<list1.size();i++){
    			Double value = (Double)list1.get(i);
    			list2.add(value.toString(), i);
    		}
    To finish sooner, take your own time....
    Nivedithaaaa

  17. #37
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Niveditha View Post
    Use a for loop, limit will be the list size get each value parse it into double and then store it in anoother for loop

    Java Code:
    		ArrayList list1 = new ArrayList();
    		List list2 = new List();
    		list1.add("1");
    		list1.add("2");
    		for(int i=0;i<list1.size();i++){
    			Double value = (Double)list1.get(i);
    			list2.add(value.toString(), i);
    		}
    How do you implement it in the code? Can you explain ?

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 05:45 PM
  2. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  3. Applet - reading text files packed into JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:15 AM
  4. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 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
  •