aspose file tools*
The moose likes Beginning Java and the fly likes find out digits and spaces in String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "find out digits and spaces in String" Watch "find out digits and spaces in String" New topic
Author

find out digits and spaces in String

syed quadri
Greenhorn

Joined: Nov 11, 2012
Posts: 10
hi i am facing some problem in my code please help me.

//code

public static void main(String[] args) {
int charCounter=0;
int digCounter=0;
int spcCounter=0;
try{




File f = new File("E:\\abc.txt");
f.createNewFile();
FileWriter br = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(br);
bw.write("Arsaedf123");
bw.newLine();
bw.write("abcd");
bw.newLine();
bw.write("Ayaan");
for(int i=0;i<bw.length();i++){
char ch = bw.charAt(i);

if (Character.isLetter(ch)) {
charCounter++;
}
//
if(Character.isDigit(ch)){
digCounter++;
}
if(Character.isSpaceChar(ch)){
spcCounter++;
}
//
}

System.out.println("Characters="+charCounter);
System.out.println("digits="+digCounter);
System.out.println("space="+spcCounter);

bw.close();
}
catch (IOException e){
}
}
}

in the above program i am using for loop in that for loop how can i check that particular line contains any space or digits
please solve my problem.

Actually sir i am having a proble to count spaces and digits in a line
so i am using a for loop to check weather the given line is contain digits and spaces or not which are as follows

\\code
[java=code]
for(int i=0;i<bw.length();i++){
char ch = bw.charAt(i);

if (Character.isLetter(ch)) {
charCounter++;
}
//
if(Character.isDigit(ch)){
digCounter++;
}
if(Character.isSpaceChar(ch)){
spcCounter++;
}
//
[\code]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
Doesn't the code already do that? If not, what *does* it do, and what would you have it do instead?

As an aside, you should never ignore I/O exceptions - at least print something to where you will see it.


Ping & DNS - updated with new look and Ping home screen widget
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12


Once again, I have put code tags around your java so it is readable. Please read this.
please solve my problem.

First, we don't solve problems for you. will will help you solve it, but if you are expecting someone to give you the solution, you are in the wrong place.

Next, in order for us to help you, you need to tell us what EXACT problem you are having.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: find out digits and spaces in String
 
Similar Threads
regular expression
converting to XML file
Putting an object instance into an ArrayList
Can not find my error ? Help
Putting an object instance into an ArrayList