Math.random error! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Math.random error!

Here's a copy of my code: ... import java.util.Scanner; import java.util.Random; public class Program { public static void main(String[] args) { String[] words = {"With","Hi","Down"}; int pos = (int ) Math.random() * words[].length; } } ... The math.Random() section keeps throwing an error -- something about expecting a class. Anyway, I've been trying different variations of this for a while, following the general guidelines given here and on other places in the internet. Help?

12th Feb 2017, 1:37 AM
Mikolaj Figurski
Mikolaj Figurski - avatar
1 Answer
+ 5
Nope not error.Just it must be import java.util.Scanner; import java.util.Random; public class Program { public static void main(String[] args) { String[] words = {"With","Hi","Down"}; int pos = (int ) Math.random() * words.length; } } Remove array bracket of words.length
12th Feb 2017, 2:32 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar