The Artima Developer Community
Sponsored Link

Java Answers Forum
HELP simple problem with chars

3 replies on 1 page. Most recent reply: May 29, 2003 11:12 PM by Rahul

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

HELP simple problem with chars Posted: May 29, 2003 7:36 PM
Reply to this message Reply
Advertisement
The error says that char cannot be dereferenced to this line:
if(current.equals(c))





Here is the rest of the function:
public void getFreq(String s, int stIndx ){
		
		freqTable=new char[s.length()][s.length()];//2d-array same size as the string arg.
		char current;
		int row=0, col=0;
		
		while(stIndx!=s.length()+1){
			
			int freqCount=0;
			current=s.charAt(stIndx);
			freqTable[row][col]=current;
			for(int i=0; i<=s.length(); ++i){
				char c=s.charAt(i);
				if(current.equals(c))
					freqCount++;
			}
			char fc=(char)freqCount;
			//fc=freqTable;
		}
		
	}   


Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: HELP simple problem with chars Posted: May 29, 2003 9:10 PM
Reply to this message Reply
'current' is char and not a String. equals() method relates to objects and not data types.

Use: current==c, this should work.

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: HELP simple problem with chars Posted: May 29, 2003 9:35 PM
Reply to this message Reply
Thanks, that did the trick. I have another question now, how can I remove a character in a string and move the order down.
ex.
remove the first 's' in this sentence,
"susie says its easy"

to make it:
"usie says its easy"
????

Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: HELP simple problem with chars Posted: May 29, 2003 11:12 PM
Reply to this message Reply
To remove an s from the string, use indexOf(int ch) method of String class:

Here's a method that removes all s's from a string:

String s = "susie says its easy";
 
for (int i=0; i< s.length() ;i++ )
{
     if (i==s.indexOf('s')){
	if (i==0)
		s = s.substring(i+1,s.length());
	else
		s = s.substring(0,i) + s.substring(i+1,s.length());
 
     }
}


This makes it generic.

You may put a 'break;' after the first 'if' is encountered. It ensures that only the first s is removed. You can of course customize the code above to your needs.

Flat View: This topic has 3 replies on 1 page
Topic: Swings in Java Previous Topic   Next Topic Topic: Newbie needs help desperately!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use