-1

I want to scan a sentence that the user types, but I want to scan the words separately and then perform the actions depending on the words the user has put.

}else if(choice.equals("load")){
  System.out.println("Enter the name of the file:");
  String name = input.next();
  loadFile(name);

what I want is the user to type "load file.txt" directly

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 4
    What problems are you having? – Hovercraft Full Of Eels Dec 18 '12 at 22:15
  • Show us some input and expected output. You can split string by `String.split("\\s+")` for whitespaces – Smit Dec 18 '12 at 22:16
  • this is one case of my code, so for each one of them i need the user to type the full sentence of what he wants and with one scan the program will execute. – Angelo Mico Dec 18 '12 at 22:16
  • I basically answered that question in my comment under [your other question](http://stackoverflow.com/questions/13923379/i-have-an-issue-using-tokens-scanning-multiple-inputs-and-executing-them-at-the). – jlordo Dec 18 '12 at 22:17
  • @smit the input should be "load file.txt" and the program will execute that file without the need for the user to firstly type if he wants to load and then asked again what file he wants to load – Angelo Mico Dec 18 '12 at 22:18
  • Please read up on [how to ask good questions](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx) before trying this again. You'll get better answers if you ask better questions. – Hovercraft Full Of Eels Dec 18 '12 at 22:18
  • @AngeloMico You really need to revise your question. Your headline saying something, Your code says something else and you want something different than those two. – Smit Dec 18 '12 at 22:28
  • @jlordo i know, i just have no clue how to use that string splitting , should i used it after i scan it or what?give me an example – Angelo Mico Dec 18 '12 at 22:29
  • @AngeloMico just take a look on my answer – Festus Tamakloe Dec 18 '12 at 22:31
  • just like in my [other comment](http://stackoverflow.com/questions/13923379/i-have-an-issue-using-tokens-scanning-multiple-inputs-and-executing-them-at-the#comment19195130_13923379). First read the line than split that string. I even have the code for you in [that comment](http://stackoverflow.com/questions/13923379/i-have-an-issue-using-tokens-scanning-multiple-inputs-and-executing-them-at-the#comment19195130_13923379) – jlordo Dec 18 '12 at 22:31
  • @jlordo i know you did man and thanks for that but i didn't know how to do this ---> if(input.contains("load")) sorry if I am a pain in the ass with the stupid questions,i just got here – Angelo Mico Dec 18 '12 at 22:57

2 Answers2

2

I try to write a small code for you may be this can help you a little bit

    public class Tester2 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        boolean state = true;
        while(state) {

            System.out.println("Please enter your text");
            Scanner scan = new Scanner(System.in);
            String input = scan.nextLine();
            if(input.contains("load")) {
                String fileName = getFileName(input);
                if(fileName != "" || fileName!= null) {
                    load(fileName);
                }
            }else{

            }

        }

    }

    private static String getFileName(String input) {
        String values[] = input.split("\\s+");
        String fileName = "";
        for(String s : values) {
            if(s.indexOf(".") > 0) {
                fileName = s;
                break;
            }
        }
        return fileName;
    }

}
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • Whenever you have questions, feel free to ask – Festus Tamakloe Dec 18 '12 at 22:32
  • ,yup i think its kind of what i want i just dont get this : for(String s : values) { if(s.indexOf(".") > 0) { fileName = s; break; why did you do this? – Angelo Mico Dec 18 '12 at 22:49
  • for(String s : values) ==> iteration/loop over the values if(s.indexOf(".") > 0) ==> here we want to which word has an extension in another which word contains(".") if we get one we passe it to fileName then we do not need anymore to continue (break). If your satisfy with my answer just vote up and accept it – Festus Tamakloe Dec 18 '12 at 22:50
  • ok,one more thing,I need to have some other options other than "load" like "inspect" an other class etc.. should I continue with this way? put an "else" and carry on with the next option?and do i need to do the thing i asked above again for each of the options i have? – Angelo Mico Dec 18 '12 at 23:05
  • You can do it in the same. but use else if(conditions){} try write a litte method where you can some some codes to avoid the repetition – Festus Tamakloe Dec 18 '12 at 23:10
  • yeah "else if" , alright man thanks ! I don't have enough reputation to vote up your answer though, sorry – Angelo Mico Dec 18 '12 at 23:13
0

It's too long for a comment, and I don't really understand what your last comment means. Here's some sample code. Try it with inputs like

  • load filename.txt
  • load foo bar baz
  • save foo
  • whatever ...

    System.out.println("What would you like to do");
    Scanner scanner = new Scanner(System.in);
    String line = scanner.nextLine();
    String[] input = line.split("\\s+");
    if (input[0].equals("load")) {
        for (int i = 1; i < input.length; i++) {
            System.out.println("Token #" + i + " after load: " + input[i]);
        }
    } else {
        System.out.println("You did not enter load, I ignore your input");
    }
    

If you have a question about the behavior or anything else: feel free to leave a comment.

jlordo
  • 37,490
  • 6
  • 58
  • 83