I cannot understand this error, despite my research throughout this website. At first the program ran, but it gave me a “y” as a line, and the validator choices were “n”,”n”,”n” all in one. So, I had a look around in my program, found it, solved it. Now I’m faced with this actual and format arguments lists differ in length problem. I’ve tried various things, like changing the variable type, using different variables… so on so forth.
Here’s my code.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package roshambo;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Owner
*/
public class Roshambo {
private String name;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String choice = "y";
String player;
Scanner sc = new Scanner(System.in);
Random random = new Random();
char[] ch = new char[]{'r','p','s'};
int r = random.nextInt(ch.length);
char raCh = ch[r];
while (choice.equalsIgnoreCase("y")) {
System.out.println("Welcome to Roshambo!");
System.out.println("");
//System.out.print("Please enter your name here - ");
//String player = sc.nextLine();
String player1 = Validator.getRequiredString("Please enter your name here - ",player);
String roshambo = sc.nextLine();
String roshambo1 = Validator.getChoiceString1("Please choose Rock, Paper or Scissors. (R/P/S) - ",'r','p','s');
choice = getChoiceString("Play Again? (y/n): ","y","n");
System.out.println("");
}
}
public static String getChoiceString(String prompt, String str1, String str2)
{
boolean isValid = false;
Scanner sc = new Scanner(System.in);
String choice = "";
while (isValid == false) {
System.out.print(prompt);
choice = sc.next();
if (choice.equalsIgnoreCase(str1) || choice.equalsIgnoreCase(str2)) {
isValid = true;
}
sc.nextLine();
}
return choice;
}
}
What am I doing wrong? I’ll also add in my Validator code, as I think the problems lies between those two.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package roshambo;
import java.util.Scanner;
/**
* @author Owner
*/
public class Validator {
// force the user to enter a string
public static String getRequiredString(String prompt) {
Scanner sc = new Scanner(System.in);
String input = "";
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
input = sc.nextLine();
if (input.equals("")) {
System.out.println("Error! This entry is required. Try again.");
}
else {
isValid = true;
}
}
return input;
}
// force the user to enter one of three strings
public static String getChoiceString1(String prompt, String str1, String str2, String str3) {
String input = "";
boolean isValid = false;
while (isValid == false) {
input = getRequiredString(prompt);
if (input.equalsIgnoreCase(str1) || input.equalsIgnoreCase(str2) || input.equalsIgnoreCase(str3)) {
isValid = true;
}
else {
System.out.println("Error! Entry must be '"+ str1 +"', '"+ str2 +"', or '"+ str3 +"'. Try again.");
}
}
return input;
}
}
Thanks in advance for the help!
Your getRequiredString method in Validator class is expecting a single String argument, you are passing two arguments, String and a Player(you should however be getting another compiler error, saying cant find player, as you commented it out) in your method call.
Just pass prompt from your method call and the error would disappear.