Current or formal argument lists differs in length

1

I have the following code snippet:

import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.io.*;
import java.lang.String;



public class findthewordgame {
   static Random random = new Random();


public static String arrayWord () { 

    String[] wordgame = { //array
        "TEA",
        "COFFEE",
        "BOAT",
        "SEA",
        "SUN"
    };

    int idx = random.nextInt(wordgame.length);
    String wordChosen = (wordgame[idx]);

    //System.out.println(wordChosen); //prints the random word
    //System.out.println(scramble(wordChosen));
    return (scramble(wordChosen));

}


public static String scramble(String inputString ) 
{
    // Convert string into a simple char array:
    char[] a = inputString.toCharArray();

    // Scramble the letters using the standard Fisher-Yates shuffle, 
    for( int i=0 ; i<a.length ; i++ )
    {
        int j = random.nextInt(a.length);

        //shuffle the characters
        char temp = a[i]; 
        a[i] = a[j];  
        a[j] = temp;
    }       

    return new String(a);
}

public static String input() {
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter the correct answer: ");
    String n = reader.next();
    return n;
} 


public static void verifyWord(String wordChosen, Scanner reader){
    boolean answeredCorrectly = false;
    int tries = 1;
    //String wordChosen = random.nextInt();
    String answer = wordChosen;    
    while (tries>0 && answeredCorrectly == false) {
        answer = reader.next();
        if (wordChosen == answer) {
            System.out.println("You got it right");
            answeredCorrectly = true;
        }
        else if (answer != wordChosen) {
            System.out.println("Wrong");
        }
    }

}

I do not understand when I try to call it on Main it is giving error (see photo).

    
asked by anonymous 26.09.2017 / 18:42

3 answers

3

The error says:

  

Current or formal argument lists differs in length

That is

  

Actual or formal argument lists differ in length

Your verifyWord method receives two parameters, a String and a Scanner . In the main method you are calling without passing any parameters:

...
verifyWord();
...

You need to first define words to search for, and Scanner that will be used for data entry. So I suggest that your main method be changed to the following:

public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  String word = arrayWord();

  System.out.println("Welcome to the Find the Word game");
  System.out.println("Guess the word: " + scramble(word));

  verifyWord(word, input);
}

One more change you can make in the verifyWord method is to set the loop to do while , since it must be executed at least once:

public static boolean verifyWord(String wordChosen, Scanner reader) {
  String answer;
  boolean answeredCorrectly = false;

  answer = reader.next();

  if (wordChosen.equals(answer)) {
    System.out.println("You got it right");
    answeredCorrectly = true;
  } else if (answer != wordChosen) {
    System.out.println("Wrong");
  }

  return answeredCorrectly;
}

Another way is to change your main method to the following:

public static void main(String[] args) {
  String word = arrayWord();

  System.out.println("Welcome to the Find the Word game");
  System.out.println("Guess the word: " + scramble(word));

  verifyWord(word, input());
}

And its method verifyWord to the following, in order to maintain the current data entry:

public static boolean verifyWord(String wordChosen, String answer) {
  Boolean correct = wordChosen.equals(answer);

  if (correct) {
    System.out.println("You got it right");
  } else {
    System.out.println("Wrong");
  }

  return correct;
}

By the way, you should save the correct word before you shuffle it. To do this, change the following method:

public static String arrayWord() {

  String[] wordgame = { //array
    "TEA",
    "COFFEE",
    "BOAT",
    "SEA",
    "SUN"
  };

  int idx = random.nextInt(wordgame.length);
  String wordChosen = (wordgame[idx]);

  return wordChosen;

}
    
26.09.2017 / 18:59
4

The method was declared with 2 parameters and in the call, within the main method, none of the required parameters were passed.

    
26.09.2017 / 18:50
2

It is asking if the arguments (Parameters) are assigned, this is the cause of the error.

To use Scanner, you should import:

import java.util.Scanner;

An example usage using an entry via console:

Scanner sc1 = new Scanner(System.in);

Reading the part of a string:

String texto = "Meu texto";
Scanner sc2 = new Scanner(textoString); 

Getting values and assigning variables:

Scanner sc = new Scanner(System.in);

float numero_com_ponto = sc.nextFloat();
int inteiro = sc.nextInt();
byte byte = sc.nextByte();
long numero_longo = sc.nextLong();
boolean verdadeiro_falso = sc.nextBoolean();
double numero_com_ponto = sc.nextDouble();
String texto = sc.nextLine();
    
26.09.2017 / 18:44