473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Java String Counter

I working on a program to analyze a string and tell the user how may of each char is in the sentence. I have it sorta working but cant fidgure it out. I get a weird last line and my method to remove the "." totally kills the program. I had java several years ago and a friend has asked me to help. Any Ideas?


Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. public class example{
  4. static Scanner console= new Scanner(System.in);
  5. public static void main(String[] args)
  6. {
  7. System.out.print("Please Enter aString ");
  8. System.out.println();
  9. console.useDelimiter("\\n");
  10. String str =console.next();
  11.  
  12. String strClean = str.replaceAll(" ","");
  13. //String strClean1 = strClean.replaceAll(".","");
  14. System.out.println (strClean);
  15.  
  16. char[]third =strClean.toCharArray();
  17.             for(int counter =0;counter<third.length;counter++)
  18.             { char letter= third[counter];
  19.  
  20.                 int flage=0;
  21.                 for ( int i=0; i<third.length; i++)
  22.                 {
  23.                     if (letter==third[i])
  24.  
  25.                         flage++;
  26.  
  27.  
  28.                 }
  29.                 if(counter==0)
  30.                 {
  31. System.out.println("The letter ="+letter+" is repeated "+flage+" # of Times ");
  32.     continue;
  33.                 }
  34.                 boolean flag=false;
  35.                 for(int j=counter-1;j>=0;j--)
  36.                 {
  37.                     if(letter==third[j])
  38.                         flag=true;
  39.                 }
  40.                 if(!flag)
  41.                 {
  42.  
  43. System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
  44.  
  45.  
  46.                 }
  47.  
  48.             }
  49.     }
  50.  
  51.  
  52. }
Oct 29 '08 #1
1 4355
JosAH
11,448 Expert 8TB
Why don't you use a Map<Character, Integer> and use that for every character
you read from the input?

kind regards,

Jos
Oct 29 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rhino | last post by:
I've been playing with Java UDFs for the last couple of days and I've got some questions about scratchpads. I'm running DB2 LUW V8 (FP8) on WinXP. Somewhere in the manuals, I found some remarks...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
2
by: loganandvicky | last post by:
i have assigned several variables in one method and i want to use them in another method. that works ok until i call the second method. i get java.lang.string error heres my work.... /** ...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
4
by: Tigger2000 | last post by:
I keep getting an error when trying to compile this for Java prog class, below is the instructions for the assignment and my code. Loop Assignment Write, compile, and run a Java program that...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
13
by: spicster | last post by:
I need help with finding an entire inventory value for a java assignment. This is what I have so far. I am new to Java and am struggling. In need of help, thanks import...
2
by: dmstn | last post by:
Hey! I've got a little problem. I have to make a web site for a university essay. I curently have to create a search engine. Users can enter a hotel name in a search bar and results have to appear in...
2
by: rayoflight | last post by:
This is how my test program works. Server will encrypt some text and sends it over to the client. The client will then decrypts it. However I have some problem decrypting it. When I play...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.