0

As of right now, I have a text file called lol.txt and my code prints out a line in the file with a "|" between each word. For example, the first line prints "hello|howdy|hola" and I want to split this at the pipe so I can randomly pick a greeting from that line. I am confused on why my delimiter is not working and how I would go about converting it to an array so I can take the index of the first line so greeting[0] would print "hello".

    Scanner sc = new Scanner(System.in);

    // open file and prompt for file name
    System.out.print("Enter a file name: ");
    String fileName = sc.nextLine();
    File infile = new File(fileName);
    Scanner readIt = new Scanner(infile);

    //prints <greeting> of text file. Ex. hello|howdy|etc...
    String greeting = readIt.nextLine();
    greeting.split("|");
    System.out.println(greeting);
John Doe
  • 23
  • 4
  • If you're saying what I think you're saying, you have it backwards. BNF describes the grammar you want to use. After you have the BNF, you write a program to parse the grammar in that format. So you need the BNF first (which comes from you personally), then you read the `lol.txt` file according to that BNF. – markspace Apr 28 '16 at 04:36
  • @markspace Yes, I have the grammar format I need in lol.txt. I am confused on how to parse the grammar I guess – John Doe Apr 28 '16 at 05:00
  • @markspace I adjusted the question accordingly to my progress – John Doe Apr 28 '16 at 19:54
  • `greeting.split("|")` will not change `greeting`, but returns a new array containing the result of the split. Thus, you should assign the result (e.g. `String[] greetingSplit = greeting.split("|");`) and use that variable for your random picker. – Rick Apr 28 '16 at 19:57
  • Furthermore: http://stackoverflow.com/q/10796160/2137833 – Rick Apr 28 '16 at 20:06
  • @Rick when I do that, it breaks it up into [h,e,l,l,o,|...etc] – John Doe Apr 28 '16 at 20:08
  • @Rick, after reading your link, the split("\\|") was what I needed. Thank you very much – John Doe Apr 28 '16 at 20:11
  • Just realized I gave so many parts away, that I might as well formulate a full answer ;) – Rick Apr 28 '16 at 20:12

1 Answers1

0

First of all, you should store the result from the split in a new variable, as it will return a new String array (String[]). Second, you need to escape the pipe delimiter (Splitting a Java String by the pipe symbol using split("|")).

The resulting code will then be:

Scanner sc = new Scanner(System.in);

// open file and prompt for file name
System.out.print("Enter a file name: ");
String fileName = sc.nextLine();
File infile = new File(fileName);
Scanner readIt = new Scanner(infile);

String greeting = readIt.nextLine();
System.out.println(greeting); //prints hello|howdy|etc...
String[] greetingSplit = greeting.split("\\|");
System.out.println(greetingSplit[0]); //prints hello

You can then pick a random number between 0 and greetingSplit.length to find the index of your random word and print the word.

Community
  • 1
  • 1
Rick
  • 443
  • 2
  • 10
  • so now, I got it to read random words to put into random greetings. So if I enter "4", it will print 4 random greetings but they are all the same. Any idea in why each greeting isn't randomized in my for loop? – John Doe May 01 '16 at 00:17
  • @JohnDoe we really need some code to answer that. You can better open a new question for that ;) – Rick May 01 '16 at 06:10