Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008, 06:56 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
the number of occurrences of each alphabetical letter in the string.
i try this code but it reapts result please help me
Code:
import java.util.*; public class Main { static Scanner console= new Scanner(System.in); public static void main(String[] args) { System.out.print("Please Enter aString "); System.out.println(); String str =console.next(); char[]third =str.toCharArray(); for(int counter =0;counter<third.length;counter++) { char letter= third[counter]; int flage=0; for (int i=0; i<third.length; i++) { if (letter==third[i]) flage++; else continue; } } } public static void counter (int x) { int []count= new int[x]; // this method to store the number of occurrences of each //alphabetical letter in the string } public static void read (int x) { // this method should recive acharacter // then update its the corresponding counter } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-13-2008, 07:19 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You have to compare each letter and update a counter.

What you mean repeats the result?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-13-2008, 07:31 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
if the frist character is a for example
and its counter is 2 then the third character is also a it output the same counter 2 i donot want this i want that he tld me that the character a counter is 2 one time only
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-13-2008, 07:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
How it happened. If the first character is 'a' the counter should be 1, and if the third character is 'a' again the counter should be 2 and so on.

What you mean,

Quote:
he tld me that the character a counter is 2 one time only
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-13-2008, 07:56 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
it is the same character i want to count uniqe character . no repitition
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-13-2008, 08:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Say the string is "this is a line of text as a string"

what is your expecting output. You mean wants to count a character at once.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-13-2008, 09:30 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
yes You mean wants to count a character at once
the output should be
t ocuures 3
h ocuures 1
i ocuures3
s ocuures4
discard i then calcualte a because i was calculated before
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-13-2008, 01:15 PM
Member
 
Join Date: May 2008
Posts: 6
kavithaprabhaker is on a distinguished road
check out this
import java.util.*;
import java.io.*;
public class example{
static Scanner console= new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Please Enter aString ");
System.out.println();
String str =console.next();


char[]third =str.toCharArray();
for(int counter =0;counter<third.length;counter++)
{ char letter= third[counter];

int flage=0;
for ( int i=0; i<third.length; i++)
{
if (letter==third[i])

flage++;


}
if(counter==0)
{
System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
continue;
}
boolean flag=false;
for(int j=counter-1;j>=0;j--)
{
if(letter==third[j])
flag=true;
}
if(!flag)
{

System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
}

}
}


}




Note: Your program will not take a whole String , i mean if u give String as
"this is a line of text as a string"
"Str" will only accepts "this" ...

Last edited by kavithaprabhaker : 05-13-2008 at 01:23 PM.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-13-2008, 01:20 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Seems to me that, you don't have think about white spaces.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-13-2008, 01:39 PM
Member
 
Join Date: May 2008
Posts: 6
kavithaprabhaker is on a distinguished road
if we consider the white space then we need to use BufferedReader instead of scanner . the code goes here

import java.util.*;
import java.io.*;
public class triangle{
//static Scanner console= new Scanner(System.in);
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please Enter aString ");
System.out.println();
String str=br.readLine();
System.out.println(str);


char[]third =str.toCharArray();
for(int counter =0;counter<third.length;counter++)
{ char letter= third[counter];

int flage=0;
for ( int i=0; i<third.length; i++)
{
if (letter==third[i])

flage++;


}
if(counter==0)
{
System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
continue;
}
boolean flag=false;
for(int j=counter-1;j>=0;j--)
{
if(letter==third[j])
flag=true;
}
if(!flag)
{

System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
}

}
}


}
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-13-2008, 01:45 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, but if you want using scanner also it's possible. Just use nextLine() on scanner object. But in that case you have to think about whitespace. nextLine() calculate those too.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-14-2008, 06:41 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
how to make method that receives a character and updates the corresponding counter
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-14-2008, 06:47 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Use switch-case statements there. But in that case you have to deal with large number of variables.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-14-2008, 06:56 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
how please show me with code
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 05-14-2008, 07:06 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Here is a Switch Statement from Sun's Java tutorial section....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 05-14-2008, 07:27 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, it's much simpler example. You can update a variable than printing values like in the example.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 05-14-2008, 07:54 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
thanks alot but i am very new to java please show me this code

Last edited by masaka : 05-14-2008 at 10:18 AM.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 05-14-2008, 10:22 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
thanks alot but i am very new to java please show me this code
REGARDS
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 05-14-2008, 10:51 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You can try something like this in very simple way. It's not much difficult at all.

Code:
private void counter(char ch) { int temp1 = 0; int temp2 = 0; int temp3 = 0; switch(ch){ case 'a': temp1++; break; case 'b': temp2++; break; case 'c': temp3++; break; default: System.out.println("Invalid"); } System.out.println("a has " + temp1 + " times\n" + "b has " + temp2 + " times\n" + "c has " + temp3 + " times\n"); }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 05-14-2008, 11:28 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
very thanks to you
regards

Last edited by masaka : 05-14-2008 at 12:45 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On