0

I'd like to know if there's a way where the Java SE allows a passage to be printed out and then in between the line we can allow the user to type the answer on the line.

To be more clear :

Here's an example:

____ reading, Alice also enjoys listening to classical music.

So, when the text is being drawn out using the buffer reader, the user is able to enter the answer on the line itself.

Here's the method of buffer reader:

    public void getCloze(){
    File file = new File("cloze.txt");
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(file));
        String text = null;

        // repeat until all lines is read
        while ((text = reader.readLine()) != null) {
            contents.append(text)
                    .append(System.getProperty(
                            "line.separator"));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
     // show file contents here
    System.out.println(contents.toString());

}}

Hope someone can advise me how to and best if there's any tutorial to show the steps.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
Karen Goh
  • 145
  • 1
  • 5
  • 19

2 Answers2

0

It is not entirely clear what your problem is, but you seem to want to know how to write a question or prompt and allow the user to enter the answer on the same line. If so, the "trick" is to use System.out.print(prompt) rather than System.out.println(prompt); i.e. DON'T output a line break after the prompt.


UPDATE - I see what you are asking now.

Well the bad news is that there is no simple way to do that. However, it is doable using something like the charva library or a "curses for Java" library - What's a good Java, curses-like, library for terminal applications?

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Actually, I wanted an entire cloze passage to be shown to the user. In the cloze passge, there are blanks to be filled up with answer - the line that represents the blanks and user can type on the line to give their answers. – Karen Goh Dec 19 '12 at 11:03
  • Thanks Stephen. I guess I'll have to spend an awful lot of time to understand the new stuff...curses like terminal application - sounds like what I'm looking for..... – Karen Goh Dec 19 '12 at 16:04
0

I dont think it is possible to prompt the user to type in between the already printed line in Java. you have to stop your priniting to take user input and then print further string to the user

vishal_aim
  • 7,636
  • 1
  • 20
  • 23