Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

find out digits and spaces in String

 
syed quadri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic