This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes find out digits and spaces in String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 35241
    
    7
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.


Android appsImageJ pluginsJava web charts
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6


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.


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: find out digits and spaces in String
 
Similar Threads
Can not find my error ? Help
converting to XML file
regular expression
Putting an object instance into an ArrayList
Putting an object instance into an ArrayList