|
|
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.
|
|

05-13-2008, 06:56 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
the number of occurrences of each alphabetical letter in the string.
i try this code but it reapts result please help me
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
}
}
|
|

05-13-2008, 07:19 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-13-2008, 07:31 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
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
|
|

05-13-2008, 07:43 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
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,
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.
|
|

05-13-2008, 07:56 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
it is the same character i want to count uniqe character . no repitition
|
|

05-13-2008, 08:05 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-13-2008, 09:30 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
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
|
|

05-13-2008, 01:15 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 6
|
|
|
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.
|
|

05-13-2008, 01:20 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-13-2008, 01:39 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 6
|
|
|
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 ");
}
}
}
}
|
|

05-13-2008, 01:45 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-14-2008, 06:41 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
how to make method that receives a character and updates the corresponding counter
|
|

05-14-2008, 06:47 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-14-2008, 06:56 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
how please show me with code
|
|

05-14-2008, 07:06 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
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.
|
|

05-14-2008, 07:27 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-14-2008, 07:54 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
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.
|
|

05-14-2008, 10:22 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
thanks alot but i am very new to java please show me this code
REGARDS
|
|

05-14-2008, 10:51 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
You can try something like this in very simple way. It's not much difficult at all.
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.
|
|

05-14-2008, 11:28 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
very thanks to you
regards
Last edited by masaka : 05-14-2008 at 12:45 PM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
| | | |