Results 1 to 8 of 8
Thread: using loop to find duplicate
- 03-06-2009, 08:54 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
[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.
GeorgeLast edited by gwithey; 03-06-2009 at 01:24 PM.
- 03-06-2009, 09:00 AM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
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.
- 03-06-2009, 09:06 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
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(); } }
- 03-06-2009, 12:24 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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();
- 03-06-2009, 01:20 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
I have now managed to create something that works very effectively.
code is as follows(brakets may be in wrong place.:
Thanx for all the helpJava 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"); }
George
- 03-06-2009, 01:37 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you trying to do with this?
Java Code:else { continue; }
- 03-06-2009, 01:43 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
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
- 03-06-2009, 01:46 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ya, it's redundant line of code. Much better to avoid such things.
Similar Threads
-
Cannot insert duplicate key row in object
By losintikfos in forum New To JavaReplies: 3Last Post: 05-07-2009, 09:43 AM -
Duplicate Files Remover
By caps_lock in forum New To JavaReplies: 0Last Post: 01-05-2009, 01:31 AM -
Counting Duplicate Variables in an Array
By Npcomplete in forum New To JavaReplies: 2Last Post: 10-24-2008, 07:33 PM -
random string are duplicate
By googgoo in forum New To JavaReplies: 3Last Post: 04-03-2008, 10:01 AM -
How to make a hashmap to allow duplicate values?
By Preethi in forum New To JavaReplies: 0Last Post: 02-08-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks