0

I have the following code

public class ShufflingListAndArray
{
  public static void main(String[] args) throws IOException

{
    List services = new ArrayList (


    Arrays.asList("COMPUTER", "DATA", "PRINTER"));

   Random rnd=new Random();
  String s = services.get(rnd.nextInt(services.size()));

    Collections.shuffle(list);


    //Collections.sort(list);


    System.out.println("List sorting :"+ list);
  }
} 

I get the following error when compiling the above program.

C:\>javac ShufflingListAndArray.java
ShufflingListAndArray.java:12: asList(java.lang.Object[]
nnot be applied to (java.lang.String,java.lang.String,ja
    Arrays.asList("COMPUTER", "DATA", "PRINTER"));


          ^
ShufflingListAndArray.java:15: cannot resolve symbol
symbol  : variable rnd
location: class ShufflingListAndArray
  String s = services.get(rnd.nextInt(services.size()));
                          ^
ShufflingListAndArray.java:15: incompatible types
found   : java.lang.Object
required: java.lang.String
  String s = services.get(rnd.nextInt(services.size()));
                         ^
ShufflingListAndArray.java:17: cannot resolve symbol
symbol  : variable list
location: class ShufflingListAndArray
    Collections.shuffle(list);
                        ^
ShufflingListAndArray.java:19: cannot resolve symbol
symbol  : variable list
location: class ShufflingListAndArray
    System.out.println("List sorting :"+ list);
                                         ^
5 errors

Please help me to resolve the errors.Thanks a lot....

5 Answers 5

1
...
Arrays.asList("COMPUTER", "DATA", "PRINTER"));

As you can see from the compiler error, it takes an array as input, not a series of Strings. You can pass:

...
Arrays.asList(new String[] {"COMPUTER", "DATA", "PRINTER"}));

Second, your 'services' reference is not using generic types, so its contents have the compile-time type Object and not String. Instead you want:

List<String> services = new ArrayList<String>(

Third, the name of your variable is 'services' not 'list', so:

Collections.shuffle(services);

And likewise in the final statement. In this case the compiler is pretty much telling you exactly what the problem is. Did you read its output?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks to varargs, Arrays.asList() will happily take a series of strings as input.
True, actually that compile error seems due to conflicting type inference -- the constructor call causes it to want the generic type to bind to Object instead of String.
0

Try:

public class ShufflingListAndArray
{
  public static void main(String[] args) throws IOException

{
    List services = Arrays.asList("COMPUTER", "DATA", "PRINTER");

   Random rnd=new Random();
  String s = services.get(rnd.nextInt(services.size()));

    Collections.shuffle(list);


    //Collections.sort(list);


    System.out.println("List sorting :"+ list);
  }
} 

And see if you still get your rnd error messages, if you do, can you post the full message, it seems that there are parts of it cut off.

Comments

0

Also you haven't declared the list variable: Collections.shuffle(list);

Comments

0

Working fine for me. use services instead of list, in shuffle and while printing

Comments

0

This solves all your errors:

    List<String> services = Arrays.asList("COMPUTER", "DATA", "PRINTER");
    Random rnd = new Random();
    String s = services.get(rnd.nextInt(services.size()));
    Collections.shuffle(services);
    // Collections.sort(services);
    System.out.println("List sorting :" + services);

BTW The String s is not used anywhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.