Results 1 to 8 of 8
- 06-18-2007, 06:46 AM #1
Member
- Join Date
- Jun 2007
- Posts
- 1
- Rep Power
- 0
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?
- 03-25-2010, 01:37 PM #2
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??
- 03-25-2010, 01:49 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
you can get all the file in a string and then apply .lenght();
- 03-25-2010, 02:03 PM #4
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); } } }
- 03-25-2010, 03:32 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You don't have to open and read any file contents; have a look at the File.length() method.
kind regards,
Jos
- 03-25-2010, 03:53 PM #6
- 03-25-2010, 03:59 PM #7
I don't think length() method will help to find gthe number of characters.
Ramya:cool:
- 03-25-2010, 04:01 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
How to call the input value from different file?
By xiongling in forum NetworkingReplies: 6Last Post: 04-07-2008, 10:05 AM -
How to get the count of all the lines in a file
By Java Tip in forum java.ioReplies: 0Last Post: 04-06-2008, 07:45 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
Finding the character set of a FILE
By javaplus in forum Advanced JavaReplies: 1Last Post: 01-22-2008, 06:36 AM -
Converting text file(.txt) to JPG file(.jpg) in java
By javadeveloper in forum Advanced JavaReplies: 0Last Post: 11-09-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks