954,116 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Contains() / Anagram Question

import java.io.*;
import java.util.Scanner;
public class pa4 {
  public static void main (String[] args) {
    int i;
    boolean loop = false;
    
    do {
      Scanner inFromFile = null;
      try {
        inFromFile = new Scanner(new FileInputStream("words"));
      }
      catch (FileNotFoundException e) {
        System.out.println ("Could not open the file");
        System.exit (0);
      }
      Scanner in = new Scanner(System.in);
      String s;
      String x;
      
      System.out.println("Enter some text. To quit, simply type a period.");
      s = in.nextLine();
      s = s.toLowerCase();
      if (s.equals(".")) loop = true;
      else {
        while (inFromFile.hasNext()) {
          x = inFromFile.next();
          if (s.contains(x)) System.out.println(x);
        }
      }
    } while (!loop);
  }
}


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?

PuffyCloud
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You