-2

I am having an issue, i need to scan different inputs that the user will insert on the screen at the same time. For example i have a sort of a menu that is printed on the screen and the user will select if he wants to load a file and type the name of file.I have done this by asking the user to type load and after that asked him what file to load by I need it to be done at the same time. The user just types "load S.txt" and it selects the loads option and opens the file at the same time.My professor told me I am supposed to use token but I'm stuck.(in java) Thanks .

          private static void inspecting (){
    System.out.println("inspect word or byte?");
    Scanner input3 = new Scanner( System.in );
    String insp = input3.next();
    if(insp.equals("word")){
        System.out.println("select the adrress you want to inspect the value");
        Scanner input4 = new Scanner( System.in );
        int addr = input4.nextInt();

             what i need is the user to type "ispect word at adrress x" withou doing it step by step.
  • 1
    Can you provide us an example of what you have so far? – sdasdadas Dec 17 '12 at 22:39
  • show us where you are stuck. We can help with existing code, but we won't write the complete thing for you. There are plenty tutorials on how to use `Scanner` and so on. – jlordo Dec 17 '12 at 22:39
  • you may want to read this answer here : http://stackoverflow.com/a/13886922/610305 – Amar Dec 17 '12 at 22:41
  • @sdasdadas i have edited ,added a part of my code. hope you find it useful :) – Angelo Mico Dec 17 '12 at 23:39
  • @jlordo I have no problem with scanner,look above a part of my code and see if you can help me,if its still unclear for you what I want to do please tell me so i can tell you more, i really need this to work :) – Angelo Mico Dec 17 '12 at 23:42
  • 2
    you don't have to create a new scanner for every input. You can call `String line = input.nextLine(); String[] inputs = line.split("\\s+);` and than you have everything the user entered in the string array. You can work with that values. – jlordo Dec 17 '12 at 23:44
  • @Amar check the edited version of my question,are you sure that answer will help me? – Angelo Mico Dec 17 '12 at 23:45
  • jlordo is right, you only need one Scanner but you will also need it to break on newlines. But if you create a new Scanner every time it's like buying a new car every time you want to go to the grocery store. – sdasdadas Dec 17 '12 at 23:48
  • hmmm okay, If you could be more specific that would be awesome ,I dont really know how the line.split works, If Its too much to explain tell me what should i study to understand it :) thanks for replying btw – Angelo Mico Dec 17 '12 at 23:50
  • It's all explained in [the documentation](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)). – jlordo Dec 18 '12 at 22:36

1 Answers1

0

If I understand correctly your Scanner is breaking on spaces. You want it to break on new lines:

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter(System.getProperty("line.separator")); // this scans for lines
while (scanner.hasNext()) {
    String input = scanner.next();
    System.out.println(input); // take a look for yourself
}
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • Look i want to scan the user input which will be like "ispect word at adrress x" and then from that sentence i want to take each token separately because each of them are differnet choices,but i dont want to create errors if the user types more than one space. – Angelo Mico Dec 18 '12 at 12:05
  • @jlordo I want to scan the user input which will be like "ispect word at adrress x" and then from that sentence i want to take each token separately because each of them are differnet choices,but i dont want to create errors if the user types more than one space. – Angelo Mico Dec 18 '12 at 12:08
  • is this how you scan tokens?please check my previous comments – Angelo Mico Dec 18 '12 at 12:19
  • So scan in a string (as I do above) and then split the string on a space. You should take a look at the Java String function split: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split%28java.lang.String%29 – sdasdadas Dec 18 '12 at 17:23