Results 1 to 8 of 8
  1. #1
    aNNuur is offline Member
    Join Date
    Jun 2007
    Posts
    1
    Rep Power
    0

    Default count character in text file as input file

    Hello,

    I am a newbie... can anyone help me to figure out this code?

    I want to count a character in the input text file and if they are more than 30 character..they will return a error message.

    but my problem is.. it fuction only for the first line of the text file and pop the message. the go to the next line and pop the message.

    how to make it function : read all the line in the input text file, if more than 30 char in that text file they will pop a message?

    import java.io.*;
    import javax.swing.JOptionPane;


    public class Main {


    static public String getContents(File aFile)throws FileNotFoundException, IOException {


    if (aFile == null ) {
    throw new IllegalArgumentException("File should not be null.");
    }
    if (!aFile.exists()) {
    throw new FileNotFoundException ("File does not exist: " + aFile);
    }

    StringBuffer contents = new StringBuffer();


    BufferedReader input = null;

    try {

    input = new BufferedReader( new FileReader(aFile) );
    String line = null;
    (( line = input.readLine()) != null){

    long numChar = 0;
    numChar += line.length();

    JOptionPane.showMessageDialog(null,numChar,"Charac ter in the Message",JOptionPane.INFORMATION_MESSAGE);


    contents.append(line);

    }
    }
    catch (FileNotFoundException ex) {
    ex.printStackTrace();
    }
    catch (IOException ex){
    ex.printStackTrace();
    }
    finally {
    try {
    if (input!= null) {

    input.close();
    }
    }
    catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    return contents.toString();
    }


    public static void main (String[] args) throws IOException {

    File testFile = new File("key1.txt");

    JOptionPane.showMessageDialog(null, getContents(testFile),"Original File",JOptionPane.INFORMATION_MESSAGE);

    }
    }


    hmmm? or there is other suggestion?

  2. #2
    dgaur's Avatar
    dgaur is offline Member
    Join Date
    Mar 2010
    Location
    Noida, india
    Posts
    7
    Rep Power
    0

    Exclamation Count Chars in file

    Dude.. I m totally lost in this code..

    Could you pls tell me where are you checking the characters count with 30??

    Any while reading the lines from file I don't see any while loop in the code, is it typo mistake??

  3. #3
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    you can get all the file in a string and then apply .lenght();

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by aNNuur View Post
    but my problem is.. it fuction only for the first line of the text file and pop the message. the go to the next line and pop the message.

    clear, your code is saying if ((line = input.readLine()) != null) then after the first line no more lines can be computed. so use a while instead. you made a little confusion in your code between the basics sequence, decision and iteration. here is a small modification that runs. i commended out some statements because i couldn't guess the purpose. perhaps you must adapt it again.

    Java Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Main {
    
    	static public long getContents(File aFile) throws FileNotFoundException,
    			IOException {
    
    		if (aFile == null) {
    			throw new IllegalArgumentException("File should not be null.");
    		}
    		if (!aFile.exists()) {
    			throw new FileNotFoundException("File does not exist: " + aFile);
    		}
    
    		StringBuffer contents = new StringBuffer();
    
    		BufferedReader input = null;
    		
    		long numChar = 0;
    
    		try {
    
    			input = new BufferedReader(new FileReader(aFile));
    			String line = null;
    			while ((line = input.readLine()) != null) {
    				numChar += line.length();
    
    //				JOptionPane.showMessageDialog(null, numChar,
    //						"Charac ter in the Message",
    //						JOptionPane.INFORMATION_MESSAGE);
    //
    //				contents.append(line);
    
    			}
    		} catch (FileNotFoundException ex) {
    			ex.printStackTrace();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} finally {
    			try {
    				if (input != null) {
    
    					input.close();
    				}
    			} catch (IOException ex) {
    				ex.printStackTrace();
    			}
    		}
    		return numChar;
    	}
    
    	public static void main(String[] args) throws IOException {
    
    		File testFile = new File(
    				"D:/temp/testfile.txt");
    
    		if (getContents(testFile) <= 30) {
    		JOptionPane.showMessageDialog(null, getContents(testFile),
    				"Original File", JOptionPane.INFORMATION_MESSAGE);
    		} else {
    			JOptionPane.showMessageDialog(null, "Too much characters!",
    					"Original File", JOptionPane.ERROR_MESSAGE);
    		}
    
    	}
    }

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,024
    Blog Entries
    7
    Rep Power
    20

    Default

    You don't have to open and read any file contents; have a look at the File.length() method.

    kind regards,

    Jos

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    You don't have to open and read any file contents; have a look at the File.length() method.

    good point. i noticed, that the number of chars the File.length gives back is higher then the one of numChar += line.length(); are cr and file end also counted?

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    I don't think length() method will help to find gthe number of characters.
    Ramya:cool:

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,024
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    good point. i noticed, that the number of chars the File.length gives back is higher then the one of numChar += line.length(); are cr and file end also counted?
    Yep, it includes \r and \n characters too; if you don't want that you have to actually read the entire file and ignore those characters (possibly implicitly by reading entire lines).

    kind regards,

    Jos

Similar Threads

  1. How to call the input value from different file?
    By xiongling in forum Networking
    Replies: 6
    Last Post: 04-07-2008, 10:05 AM
  2. Replies: 0
    Last Post: 04-06-2008, 07:45 PM
  3. 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
  4. Finding the character set of a FILE
    By javaplus in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 06:36 AM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 04:22 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
  •