Question Authors' Forum

Student input in Java

Student input in Java

by Pertti Suutarinen -
Number of replies: 2

Hi, 
Tried search, no luck:

How can you ask for input with Java related tasks? 
And how can you take into account that results differ based on input. For e.g "Hello firstName!" ? 

Thanks

Tags:
In reply to Pertti Suutarinen

Re: Student input in Java

by Richard Lobb -

I'm not sure I understand the question, but maybe the following will help?

Suppose you ask a student to write a program that prompts the user for their name and then prints a message like "Hello Pertti". Then you'd have a set of tests, where each test specifies the standard input ('Pertti') and the corresponding expected output.

However, there's a complication. You might expect that the expected output would be just (say) "Hello Pertti". Except that if the program prompted the user for their name, that will appear in the output, too. BUT when run on the Jobe server with standard input being taken from a file, the data read from the file does not get echoed to the standard output, as it does when reading from a file. So if the prompt was 'Enter your first name: ', what you'd see when running on a terminal would be

$ java myprogram
Enter your first name: Pertti
Hello Pertti

But when you run this in CodeRunner, you'll instead see

Enter your first name: Hello Pertti

This is confusing for the student. In Python, there's a question type python3_w_input where we simulate the echoing of keyboard characters to make the two outputs look the same, but I don't know of a way to achieve that in Java, C, C++ etc. So you either:

  1. tell students to read their input without printing a prompt, or
  2. explain to the students exactly what's happening, or
  3. get them to write methods/classes in which there's no keyboard input.