This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

False output  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bluej java program to print the position of the character which has double letters......
Example: Little star shinning bright
It is tt and nn

here is my program:
import java.io.*;
 
Saloon Keeper
Posts: 4064
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the proper declaration for main()
 
Carey Brown
Saloon Keeper
Posts: 4064
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will not work in cases where a double letter is repeated more than once in the input. E.g. "little little little"
Besides, 'i' is already the index.
 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dipanjan Pramanik wrote:A bluej java program to print the position of the character which has double letters......
Example: Little star shinning bright
It is tt and nn

here is my program:
import java.io.*;
class Abc
{
    static void main() throws IOException
    {
    BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Input a String");
    String s= obj.readLine();
    s=s+" ";
    for(int i=0;i<(s.length()-1);i++)
    {
         System.out.println("The chars to check:"+s.charAt(i)+" "+s.charAt(i+1));
        if( s.charAt(i)== s.charAt(i+1))
        {
            System.out.println("The position of the character is:"+(s.indexOf(s.charAt(i+1))));
        }
}}}



Dipanjan, use available data structures, put it in a Map<Char, Integer>, get the stream of that and use a lambda to get at a Collection<Char> with the condition that the Integer in that Map would be > 1, something like that. Or use loops. But definitely use a Map<Char, Integer>.

Then, loop through the char array of that String and make another Map<Char, List<Integer>> and add to that list when you encounter that char...

You will end up with a Map that has a Char key and a List of its locations with the String you're parsing.

You can then use that data for further analysis on these chars.

With best regards,

Anton.
 
Carey Brown
Saloon Keeper
Posts: 4064
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anton,
what you're proposing is not in line with the requirements. "little" has a double letter 't', and "tight" has no double letters, but would show up as 't' in your proposal.
 
Marshal
Posts: 5593
384
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anton Golovin wrote:Map that has a Char key


Surely you mean Character. Char class doesn't exist in Java.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try reading in a line and tokenize the line just read and use your existing logic to determine double letter existence in every word in that line. Repeat the same logic for every line until EOF.

What you really want is - if the double letter existence in a word but not in a line. Latter would not serve the purpose at all. If what I understand you want is right.

 
Liutauras Vilda
Marshal
Posts: 5593
384
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dipanjan Pramanik wrote:A bluej java program to print the position of the character which has double letters......
Example: Little star shinning bright
It is tt and nn


Do you have any issues with your app? We don't see any questions. And if there are 'ttt' word, what position should be printed? (0 or 1) or (0 and 1) or (none)?

Carey though already mentioned that code wont compile, so it is one of the problems you are having currently. Also it has been suggested how to fix 'main' in order to have a valid starting point.

Code formatting and indentation are terrible, I'd fix that too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!