Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Whats wrong in my code?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Whats wrong in my code?

    Hi friends,

    I am trying to find the Count continuous repeated occurrence of string in string array.
    (eg)
    input: now now how cow how
    output: now---->2


    but i d't know what is wrong in my code,so please any one help me to find the soluction..



    public class Stringmaxrepeate {
    public static void main(String args[])
    {
    Scanner in=new Scanner(System.in);
    String[] s=new String[10];
    int count=1;
    Map <String,Integer> h = new HashMap<String,Integer>();

    System.out.println("enter the 10 String to find the Max Strint Repeate");
    for(int i=0;i<5;i++)
    {
    s[i]=in.nextLine();
    }
    for(int j=1;j<5;j++)
    {

    if (s[j]==s[j-1])
    {
    count++;

    }
    else {
    if (count > 1) {
    h.put( s[j],count );
    count=0;
    }
    }

    count = 1;

    }
    System.out.println(h);
    int max = Collections.max(h.values());

    System.out.println(max);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: Whats wrong in my code?

    i d't know what is wrong in my code
    Please explain why you think there is a problem. Post the program's input and output and add some comments saying what is wrong.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    One problem: the code uses == to compare Strings. It should use the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Whats wrong in my code?

    Also use meaningful variable names: h, s and j mean nothing to someone else reading your code.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong in my code?

    Quote Originally Posted by Norm View Post
    Please explain why you think there is a problem. Post the program's input and output and add some comments saying what is wrong.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    One problem: the code uses == to compare Strings. It should use the equals() method.
    I got out put tanq.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: Whats wrong in my code?

    Please copy the full contents of the console from when the program was executed and paste it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong in my code?

    import java.util.*;
     
    public class Stringmaxrepeate {
    	public static void main(String args[])
    	{
    		Scanner in=new Scanner(System.in);
    		String[] s=new String[10];
    		int count=1;
    		Map <String,Integer> dupmap = new HashMap<String,Integer>();
     
    		System.out.println("enter the 10 String to find the Max Strint Repeate");
    		for(int i=0;i<10;i++)
    		{
    		s[i]=in.nextLine();
    		}
    		for(int j=1;j<10;j++)
    		{
     
    				if (s[j].equals(s[j-1])) 
    				{
    					count++;
     
    				}
    				 else {
    			            if (count > 1) {
    			            	dupmap.put( s[j-1],count );
    			            	count=1;
    			            }
    				 }
     
     
     
    			}
    		System.out.println(dupmap);
    		int max = Collections.max(dupmap.values());
     
    		System.out.println(max);
    		}
    	}

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Whats wrong in my code?

    Do you have a question?
    Improving the world one idiot at a time!

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong in my code?

    Tan q for all who all are response me,I got a perfect output..

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Whats wrong in my code?

    I don't speak whatever language that is. Perhaps you mean thankyou!
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] Im not sure whats wrong with my code
    By tomlisi92 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 25th, 2013, 09:08 PM
  2. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  3. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 09:59 PM
  4. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 12:42 PM