java


Test-Driven Development, Java, stuck with an assertion error


I was given a test suite for a test-driven development assignment. the program plays a game of Rock, Paper, Scissors and I was also given a skeletal code for the actual program. I am not supposed to change the test suite, but instead, I have to change, or make the code in the actual program so that the tests pass. I am having problem in the getting past the getInput method.
This is the code for the actual program:
public static char getInput(String prompt, char[] options, Scanner sc) {
// getInput method
// prompts user for an input that matches one of the given characters
// if its not one of those, repeat. (use contains (above))
char c;
boolean flag = false;
do {
c = sc.next().charAt(0);
if (contains(c, options)) {
System.out.println(prompt + " ( y, n, q ):");
flag = true;
}
}
while (!flag);
return c;
}
This is the method in the test suite to test the getInput method:`
private static void testGetInput() {
OutputStream out;
out = resetSystemOut();
assert 'y' == RPS.getInput("Choose", new char[] {'y','n','q'}, new Scanner("y\n"));
assertOutput("Choose ( y, n, q ):\n", out);
out = resetSystemOut();
assert 'n' == RPS.getInput("Alice", new char[] {'y','n','q'}, new Scanner("n\n"));
assertOutput("Alice ( y, n, q ):\n", out);
out = resetSystemOut();
assert 'q' == RPS.getInput("Bob", new char[] {'y','n','q'}, new Scanner("q\n"));
assertOutput("Bob ( y, n, q ):\n", out);
out = resetSystemOut();
assert 'q' == RPS.getInput("Cloe", new char[] {'y','n','q'}, new Scanner("x\nw\nq\n")); //line 81
assertOutput("Cloe ( y, n, q ):\n" +
"Cloe ( y, n, q ):\n" +
"Cloe ( y, n, q ):\n", out);
out = resetSystemOut();
assert 'v' == RPS.getInput("Doug", new char[] {'v'}, new Scanner("vvvv\nv\n"));
assertOutput("Doug ( v ):\n" +
"Doug ( v ):\n", out);
}
This is the error that I'm getting:
Exception in thread "main" java.lang.AssertionError: 54 18
Cloe ( y, n, q ):
Cloe ( y, n, q ):
Cloe ( y, n, q ):
Cloe ( y, n, q ):
at RPSTester.assertOutput(RPSTester.java:226)
at RPSTester.testGetInput(RPSTester.java:81)
at RPSTester.main(RPSTester.java:25) //line 25 has a call to the testGetInput method
I've visited the instructor during office hours but there's only so much he can tell me before revealing the whole answer to the question. I would appreciate any help, hints, pointers to my mistakes; anything is appreciated.
You should do something like this
while(scanner.hasNext()) {
String input = scanner.next();
//check if input equals to y or n or q . If yes then break
// otherwise System.out.println(prompt + " ( y, n, q ):")
}
You need to build the print statement by concatenating the prompt and the characters from options and then print that statement as many number of times as the characters in the Scanner input. You may have to adjust the appending of new line character \n to match your test assertions.
This should help:
public static char getInput(String prompt, char[] options, Scanner sc) {
// getInput method
// prompts user for an input that matches one of the given characters
// if its not one of those, repeat. (use contains (above))
char c;
boolean flag = false;
String printStr = prompt + " (";
for (int i = 0; i < options.length; i++) {
printStr += " " + options[i] + ",";
}
printStr = printStr.substring(0, printStr.length() - 1) + " ):\\n";
do {
c = sc.next().charAt(0);
System.out.println(printStr);
if (contains(c, options)) {
flag = true;
}
}
while (!flag);
return c;
}

Related Links

My Java code is not retrieving all product details,How do I solve this?
What does mean “specified for property 'resDir' does not exist.”
IntelliJ IDEA CreativeProcess error=193, %1 is not a valid Win32 application
Jasper Studio repeating block
Generating sublists from a list with complexity less than O(n2)
Java OPC UA org.eclipse.milo.examples.client.ClientExampleRunner error
How to save sequence number state across multiple invocations of an Android app?
Records saving multiple times
Bold String with underscore from spring mvc to .txt file
Jetty 9.2 configure ssl in module
Xamarin JAR Binding Library - Metadata.xml
Simple if-clause not working, can't find the mistake
Working with marklogic with java client API?
Add a common folder to both of my modules using Maven
Configuring resource server with RemoteTokenServices in Spring Security Oauth2
Finding a File Using Static Recursion

Categories

HOME
xamarin
openlayers
cloud
coq
plot
fft
yahoo-oauth
callback
django-imagekit
in-app-purchase
gorm
vaadin7
reactcsstransitiongroup
evopdf
code-review
grails3
zapier
csrf-protection
firebase-crash-reporting
fish
centos6.5
kannel
tooltipster
elasticsearch-net
log4js-node
wtx
librato
pdb
avcapturesession
windows-dev-center
subset-sum
form-data
wallpaper
http-referer
simplexml
scaffold
withings
gulp-sourcemaps
janrain
node-gyp
slick-3.0
fault
paxos
python-webbrowser
powercli
togetherjs
namecoin
epson
arrow-keys
lync-client-sdk
merge-conflict-resolution
prettytensor
ubuntu-10.04
bgp
hexdump
execute
eclipse-clp
emailrelay
sysinternals
appfabric-cache
graph-drawing
proj4js
bridge.net
dd
tablelayout
coldbox
android-nested-fragment
preferences
bitcoinj
tld
aapt
htmlcleaner
didselectrowatindexpath
mbr
centos5
html5-notifications
.aspxauth
dbconnection
sabredav
html-editor
clipper
browser-detection
fireworks
mod-auth
xmlspy
xtype
disclosure
iphone-web-app
xsdobjectgen
preference
anti-piracy
premature-optimization

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App