Current or formal argument lists differs in length

Asked

Viewed 932 times

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 you on Main is giving error(see photo).inserir a descrição da imagem aqui

  • 2

    From what I understand verifyWord(String wordChosen, Scanner reader) has two input parameters and you are calling it without any; that’s not the point?

  • 1

    You have created a method with two arguments, but you are not passing any parameters to it

  • I’ve tried that too and gives equal error. what solution suggest?

  • In time: your comparison will not return the expected result. The equal operator == checks if the reference of the objects is the same, not the content. Therefore, no matter what is typed, this operation will return false

  • Diana, the point of the function call has already been answered by Wictorchaves and Sorack. Doubts on other points (such as why even typing the right word the program does not detect) shall be published in another

  • @Jeffersonquesado no Java it makes no difference to use == or equals.

  • Is this question by any chance a duplicate of this? https://answall.com/questions/240649/problems-createclass

  • @Sorack does yes. new Integer(1) == new Integer(1). This expression returns false

  • @Jeffersonquesado was referring to String in the case...

  • @Sorack, isn’t that dependent on some string cache optimization? If the behavior is not as I hope, I will open a new question here at Sopt about this

  • 1

    @Jeffersonquesado take a look in this answer of the SO... will be the same, but is not very reliable.

Show 6 more comments

3 answers

4

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

3


The error says:

Current or formal argument lists differs in length

That is to say

The lists of real or formal arguments differ in length

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

...
verifyWord();
...

You need to define first the words that will be searched and the Scanner which will be used for data entry. Therefore I suggest your method main is amended to read as follows::

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);
}

A change also you can make in the method verifyWord is to adjust 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 method main 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 your method verifyWord to the following, aiming 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 keep the correct word before shuffling 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;

}
  • but the word is randomly managed!

  • @Dianamadeira what is returned in the method arrayWord?

  • generates a random word of an array.

  • @Dianamadeira see the way I put it now in the answer

  • does not work. the data entry I already have it all worked. the verifyWord method is to check whether what the user entered (input) is correct or not. I will update the question with the input method I created.

  • I ended up putting the complete code!

  • @Dianamadeira the way you are trying to do you will have to create two instances of Scanner. I believe the approach I suggested is simpler and works better for your case.

  • ,Actually Even though the Answer is correct says the Answer is Wrong.can you help Please?

Show 4 more comments

2

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

To use Scanner, you must import:

import java.util.Scanner;

An example of using taking an input via console:

Scanner sc1 = new Scanner(System.in);

Reading the part of a string:

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

Taking 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();
  • Assign the values to the function.

  • verifyWord(String wordChosen,Reader scanner); so?? sorry but I’m new to the subject matter!

  • @Does Wictorchaves have an example code? I know your answer is correct, but she could add more if she had the creation of Scanner cli

  • If I could, I would give another +1 in response after editing

Browser other questions tagged

You are not signed in. Login or sign up in order to post.