This prompt scanner works outside of this program but when i integrate it, it doesn't work: All code work. just no together. Help would be much appreciated.

  Scanner in = null;
   Scanner key= new Scanner(System.in); 
   System.out.print("Input file name: ");
 try
 { 
     String filename = key.nextLine();
     in = new Scanner(new File(filename));      
 }
     catch (FileNotFoundException e) 
     {
       System.out.println ("File not found!");

       System.exit (0);
     }  

Code that works with set file.

import java.util.Scanner; 
import java.io.*;
public class Stringy {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);

        int action;
        int word = 1;
        int line = 0;
        String search;
        String nextWord;
        String newWord;
        String nextLine;
        String replaceWord;
        String replacement;
        String fileString = "";
        boolean contains = false;

        try {
            scan = new Scanner (new File("guns.dat"));
        }
        catch (FileNotFoundException e){
            System.out.println("File not found!");
            System.exit(0);
        }
        System.out.println("1.Searching for a word");
        System.out.println("2.Find a substring inside of a word");
        System.out.println("3.Search for a word and replacing it with another word.");
        System.out.println("4.Counting the number of words in the file.");
        System.out.println("5.Count the number of words that include the letter of the user's choosing.");
        System.out.println("6.Count the number of lines in the file.");
        System.out.println("7.Exit");

        System.out.print("\n");
        System.out.println("Enter the number of the method that you would like to perform: ");
        action = keyboard.nextInt();
        while(scan.hasNextLine()) {
            fileString += scan.nextLine();
            line++;
        }
        fileString = fileString.toLowerCase();

        switch(action) {
            case 1:
                String searchWord = "";
                System.out.println("Which word would you like to search for: ");
                searchWord = keyboard.next();

                if(findWord(fileString,searchWord)) {
                    System.out.print("The word is present: ");
                }
                else {
                    System.out.print("The word is not present: "); 
                }
                break;

            case 2:
                String searchCharacter = "";
                System.out.println("Which substring do you want: ");
                searchCharacter = keyboard.next();

                if(findCharacter(fileString,searchCharacter)) {
                    System.out.print("The substring is present: ");
                }
                else {
                    System.out.print("The subrting is not present: "); 
                }
                break;

            case 3: 
                System.out.println("Enter the word you would like to replace: ");
                replaceWord = keyboard.next();
                System.out.println("Enter the word you would like to replace it with: ");
                replacement = keyboard.next();

                fileString = fileString.replaceAll(replaceWord, replacement);
                System.out.println(fileString);
                break;

            case 4:
                boolean prevCharWasSpace = true;
                for (int i = 2; i < fileString.length(); i++) {
                    if (fileString.charAt(i) == ' ') {
                        prevCharWasSpace = true;
                    }
                    else{
                        if(prevCharWasSpace) word++;
                        prevCharWasSpace = false;
                    }
                }
                System.out.println("There are " +word +" words in the file.");
                break;

            case 5:
                System.out.println("Enter the character that will be counted ");
                String character = keyboard.next();
                char charCharacter = character.charAt(0);

                int counter = 0;
                for(int a = 0; a < fileString.length(); a++ ) {
                    if(fileString.charAt(a) == charCharacter) {
                        counter++;
                    } 
                }
                System.out.println("That character is found " +counter +" times.");
                break;

            case 6:
                System.out.println("There are " +line +" lines in the file.");
                break;

            default:
                System.out.println("Not one of the options. Buh-Bye");
        }   
    }
    public static boolean findWord(String fileString, String searchWord) {

        return fileString.contains(searchWord);
    }

    public static boolean findCharacter(String fileString, String searchCharacter) {

        return fileString.contains(searchCharacter);
    }
}

Are you getting an exception? if so what is the name of the exception? what happens when you run your code?

Its just that when i go on to type in an option from the menu after naming file, i hit enter it just acts like an enter key going to next line instead of going to the case.

add some print statements in your code to see what methods are being called and what the values of your variables are.

The first code is not a subset of the second, so will behave differently.

The second class doesn't prompt for a file name. There is also a problem with (some of) the results it returns.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.