Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
Join Date: Oct 2009
Posts: 38
Java String test

Hi
I am trying to write a java program which reads a string from the keyboard and then reports how many time each letter occurs within that string. Non-letters which are included in the string should also be reported. Please check my code for further details and please help me by replying. I am waiting for your reply. Thanks in advance.
Code:
Import B102.*;  
class_prog1
{
public static void UserInput()
{
public String dt = new String()
Screen.out.println("Please enter some characters values:");
String.toLowerCase()
dt=keybd.in.readchar();
return dt;
}
public static void Calculate()
{
private char calcArray[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
private int index;
for(index=0; i<?; index++)
}
Reply With Quote
  #2  
Old 09-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Java String test

Hi
Instead you can use the Character.isLetter() method which can make your program more perfect. Also do not initialize fy string by creating a new String. Strings are immutable, so doing that is waste of time and memory. See the following block of code below, it may help you, if any queries post back again.
Code:
String dt = "test string";
int letterCount = 0;
for (int i = 0; i < dt.length(); i++)
{
    if (Character.isLetter(dt.charAt(i))
    {
          letterCount++;
     }
}
Reply With Quote
  #3  
Old 09-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Java String test

Hi
There is a good way out for your first part of the question. You can "cheat" taking the advantage of underlying Unicode representation of characters. But for that you will have to convert the String to all lowercase or all uppercase first. Check this code out, try it and see if it works with you.
Code:
int[] lettersCount = new int[26];
char letter = 'a';
lettersCount[(int)letter-'a']++;
Reply With Quote
  #4  
Old 09-11-2009
Member
 
Join Date: Oct 2009
Posts: 38
Re: Java String test

Thanks a lot guys.
Still I have not written the full code, but I think I got the idea what you guys are talking about. And I think I can implement them now in my code. I will try all possible way for it so that my code is perfect and simple to read. By the way thanks again.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java String test"
Thread Thread Starter Forum Replies Last Post
Definition of a String in Java? cloud101 Software Development 5 28-07-2014 10:14 AM
The String Class in Java blueprats Guides & Tutorials 3 12-03-2010 02:44 PM
String to uppercase in java ISAIAH Software Development 5 24-02-2010 12:33 AM
Separating a string in java Aaliya Seth Software Development 5 10-02-2010 04:27 AM
How to convert string into int in Java Zool Software Development 3 09-11-2009 12:41 PM


All times are GMT +5.5. The time now is 04:14 AM.