Results 1 to 8 of 8
  1. #1
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Question [Solved] Using loop to find duplicate

    i am new to java and am experienceing difficulty in writing a simple loop.

    what i would like to say is:
    // if line != ""(blank line) continue loop;
    // if duplicate lines entered by user sout("Duplicate: " + line)
    // if the line is == to "" print Bye

    i am getting in a mess trying and have been for several hours, help would be appreciated, i think its a case of not understanding the logic.

    George
    Last edited by gwithey; 03-06-2009 at 01:24 PM.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    What exactly do you want? Explain it less cryptic than you've done now. And show us what you've done so far.

    Also you want the loop to stop if it's a blank line ("") AND print it Bye? How do you store all lines? In an array, or only the last?
    I die a little on the inside...
    Every time I get shot.

  3. #3
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    it only looks at the previous line to check if it is the same.

    If a line exactly equal output
    Duplicated: ..line....
    program continues till a blank line entered. if it is it will output Bye and exit

    code i have tried:

    Java Code:
    String end = "";
    String name;
    while(name != end)
    {
         if (name.equalsIgnoreCase(end))
    {
    System.out.println("Bye");
    break;
    }
    else
    {
    if (name.equals(name = scan.nextLine()));
    System.out.println("Duplicated:" + name);
    else
    name = scan .nextLine();
    }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    1. How did you going to change the variable name?
    2. However you should assign new value to the variable name before working on.

    Java Code:
    name = scan .nextLine();

  5. #5
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    I have now managed to create something that works very effectively.

    code is as follows(brakets may be in wrong place.:

    Java Code:
    public static void main(String[] args)
    {
    	String line, end = ("");
    	Scanner scan = new Scanner(System.in);
    
    	System.out.println("Please enter lines of text: ");
    	line = scan.nextInt;
    
    while(!line.equals(end))
    {
    	if(line.equals(line = scan.nextLine()))
    	{
    	System.out.println("Duplictated: " + line);
    	}
    	else
    	{
    	continue;
    	}
    	}
    if (line.equalsIgnoreCase(end))
    System.out.println("Bye");
    }
    Thanx for all the help
    George

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Ah thanx i see you point that is a pointless bit of code. Have now removed it and it is still fully functioning. The if is in the while so it will continue anyway until the condition is false.

    George

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Cannot insert duplicate key row in object
    By losintikfos in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 09:43 AM
  2. Duplicate Files Remover
    By caps_lock in forum New To Java
    Replies: 0
    Last Post: 01-05-2009, 01:31 AM
  3. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 07:33 PM
  4. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 10:01 AM
  5. How to make a hashmap to allow duplicate values?
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 12:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •