Results 1 to 20 of 21
- 05-13-2008, 04:56 AM #1
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
the number of occurrences of each alphabetical letter in the string.
i try this code but it reapts result please help me
Java 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 } }
- 05-13-2008, 05:19 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to compare each letter and update a counter.
What you mean repeats the result?
- 05-13-2008, 05:31 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
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, 05:43 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
- 05-13-2008, 05:56 AM #5
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
it is the same character i want to count uniqe character . no repitition
- 05-13-2008, 06:05 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-13-2008, 07:30 AM #7
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
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, 11:15 AM #8
Member
- Join Date
- May 2008
- Posts
- 6
- Rep Power
- 0
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 11:23 AM.
- 05-13-2008, 11:20 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Seems to me that, you don't have think about white spaces.
- 05-13-2008, 11:39 AM #10
Member
- Join Date
- May 2008
- Posts
- 6
- Rep Power
- 0
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, 11:45 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-14-2008, 04:41 AM #12
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
how to make method that receives a character and updates the corresponding counter
- 05-14-2008, 04:47 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Use switch-case statements there. But in that case you have to deal with large number of variables.
- 05-14-2008, 04:56 AM #14
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
how please show me with code
- 05-14-2008, 05:06 AM #15
Here is a Switch Statement from Sun's Java tutorial section....
freedom exists in the world of ideas
- 05-14-2008, 05:27 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, it's much simpler example. You can update a variable than printing values like in the example.
- 05-14-2008, 05:54 AM #17
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
thanks alot but i am very new to java please show me this code
Last edited by masaka; 05-14-2008 at 08:18 AM.
- 05-14-2008, 08:22 AM #18
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
thanks alot but i am very new to java please show me this code
REGARDS
- 05-14-2008, 08:51 AM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can try something like this in very simple way. It's not much difficult at all.
Java 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"); }
- 05-14-2008, 09:28 AM #20
Member
- Join Date
- Mar 2008
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
how to convert String number to int
By gabriel in forum New To JavaReplies: 5Last Post: 08-02-2009, 03:46 PM -
Drive letter sniff/open explorer window
By iiz in forum New To JavaReplies: 1Last Post: 04-04-2008, 03:50 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
Help with insertName(String name) and deleteName(String name)
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks