944,056 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 363
  • Java RSS
Nov 11th, 2009
0

Contains() / Anagram Question

Expand Post »
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class pa4 {
  4. public static void main (String[] args) {
  5. int i;
  6. boolean loop = false;
  7.  
  8. do {
  9. Scanner inFromFile = null;
  10. try {
  11. inFromFile = new Scanner(new FileInputStream("words"));
  12. }
  13. catch (FileNotFoundException e) {
  14. System.out.println ("Could not open the file");
  15. System.exit (0);
  16. }
  17. Scanner in = new Scanner(System.in);
  18. String s;
  19. String x;
  20.  
  21. System.out.println("Enter some text. To quit, simply type a period.");
  22. s = in.nextLine();
  23. s = s.toLowerCase();
  24. if (s.equals(".")) loop = true;
  25. else {
  26. while (inFromFile.hasNext()) {
  27. x = inFromFile.next();
  28. if (s.contains(x)) System.out.println(x);
  29. }
  30. }
  31. } while (!loop);
  32. }
  33. }

My code is for the purpose of running through a wordlist and seeing what words it can make from the user input.

The problem I'm running into is if, say, the user types in "ball" the program will output "a", "all", and "ball" but will skip over words like "lab" because it's not in order in the world "ball".

I'm confused as to how I can solve this problem can anyone offer advice?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PuffyCloud is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: one more ArrayList Question...
Next Thread in Java Forum Timeline: web audio stream and jmf





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC