473,396 Members | 1,773 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,396 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 4356
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.