• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

find out digits and spaces in String

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic