• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

False output

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10785
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the proper declaration for main()
 
Carey Brown
Saloon Keeper
Posts: 10785
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10785
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8880
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8880
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic