-2

So I searched if someone before me wanted to know how a typing text works and found this here how-to-add-java-typing-text but the code from Vogel612 gives me errors on "Random.nextInt(40)..." and specialChars.contains...". Btw im a beginner so pls answer like you would answer a beginner xD.

Here is the code I have in my class:

import java.util.Random;
public class Buildingtext {
public static void main(String[] args) {

    // TODO Auto-generated method stub
    String text="bla zzz  ´` asdasdasda y yyyx sdsy sddx";
    for (char c : text.toCharArray()) {
        // additional sleeping for special characters
        if (specialChars.contains(c)) {
           Thread.sleep(Random.nextInt(40) + 15);
        }
        System.out.print(c);
        Thread.sleep(Random.nextInt(90) + 30);
    }
}
}

The "specialChars" error says : specialChars cannot be resolved.

Both Random.nextInt() errors say : cannot make a static reference to the non-static method nextInt(int) from the type Random.

I really need help thanks guys D´:

Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39
  • The snippet assumes you have declared a variable `Random random = new Random();`. As for `specialChars`, see [this comment](https://stackoverflow.com/questions/35242635/how-to-add-java-typing-text#comment58211530_35244982) beneath the answer. – shmosel Dec 28 '17 at 00:32

2 Answers2

1

1- Random is the class name, and you can not call non-static method using class name, in this case you have to create instance before and use it to call non-static method nextint

example :

Random random = new Random();// create new object form Random

random .nextInt(40); // call nextint method 

please read this for more info /difference-between-static-methods-and-instance-method

2- specialChars cannot be resolved, because you are not declared that filed/variable on your code, you have to do something like this.

for example :

String specialChars ="!@#$%^&*";

and change this condition if (specialChars.contains(c)) to be if (specialChars.contains(String.valueOf(c)))

Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39
0

nextInt is not static method of Random class

Random c = new Random();

c.nextInt(40);

Community
  • 1
  • 1
Umair Afzal
  • 181
  • 1
  • 4