1

I run a for loop in Java like for(String Day : days). I want to print data of for loop outside of for loop. How to do this?

 String[] day = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
  List<String> days = Arrays.asList(day);
  Collections.shuffle(days);
  
     for(String Day : days){
        //System.out.println(Day + " ");
     }

 String[] persons = {"john", "Bob", "Smith", "Tom", "Ryan"}; 
  List<String> names = Arrays.asList(persons);
  Collections.shuffle(names);
  
     for(String name : names){
        //System.out.println(name + " ");
     }

I want to print both like System.out.println(name+" " + Day+" "); but it showing error of cannot find symbol. How to fix it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Java is not JavaScript. Completely different languages. –  Apr 16 '21 at 18:22
  • What does "I want to print data of for loop outside of for loop" mean? The data is already right there, outside the for loop, in the `days` list; what is it you _actually_ want to do? I.e. describe the actual result, and show what code you already tried to achieve that. – Mike 'Pomax' Kamermans Apr 16 '21 at 18:27
  • What do you mean by "outside of for loop"? –  Apr 16 '21 at 18:28
  • i have two different for loops like this and I want to print them collectively like System.out.println(loop1+" " +loop2+ " " ); but its showing error of cannot find symbol. – Sukhwinder pal Singh Apr 16 '21 at 18:30
  • What is the content? Do the arrays have the same length? What text do you expect to appear? It is still not clear what you expect. Something like `Monday 1`, `Tuesday 2`...? –  Apr 16 '21 at 18:33
  • i edited my question can you please check that again? – Sukhwinder pal Singh Apr 16 '21 at 18:35
  • You have two lists of five entries each. You want to print the data from the same index of both lists iterating only once over the index 0..4. Is that what you want to do? – Yunnosch Apr 16 '21 at 18:38
  • @Yunnosch no i want random order everytime for all 5 entries. – Sukhwinder pal Singh Apr 16 '21 at 18:40
  • Please change your [mre] to show how you are doing that (including the random part) for one list, then for the other. – Yunnosch Apr 16 '21 at 18:43

1 Answers1

3

I want to print both like System.out.println(name+" " + Day+" "); but it showing error of cannot find symbol. how to fix it?

The scope of the variables, Day and name is limited to the loops where they have been declared and therefore trying to access them outside their loops will cause compilation failure.

You can fix it by accessing the elements based on indices.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String[] day = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
        List<String> days = Arrays.asList(day);
        Collections.shuffle(days);

        String[] persons = { "john", "Bob", "Smith", "Tom", "Ryan" };
        List<String> names = Arrays.asList(persons);
        Collections.shuffle(names);

        for (int i = 0; i < Integer.min(days.size(), names.size()); i++) {
            System.out.println(names.get(i) + "\t" + days.get(i));
        }
    }
}

Output:

john    Friday
Ryan    Thursday
Tom Tuesday
Bob Wednesday
Smith   Monday
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • thats what i needed. thanks alot – Sukhwinder pal Singh Apr 16 '21 at 18:43
  • @SukhwinderpalSingh - You are most welcome. Wish you success! – Arvind Kumar Avinash Apr 16 '21 at 18:43
  • along with this i want to add random numbers as well for which I have code int min = 3; int max = 10; Random rand = new Random(); int a = rand.nextInt((max-min)+1) + min; int b = rand.nextInt((max-min)+1) + min; int c = rand.nextInt((max-min)+1) + min; int d = rand.nextInt((max-min)+1) + min; int e = rand.nextInt((max-min)+1) + min; – Sukhwinder pal Singh Apr 16 '21 at 18:51
  • @SukhwinderpalSingh - You can use [`ThreadLocalRandom.current().nextInt(min, max)`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadLocalRandom.html#nextInt-int-int-) to generate a random number is a range. I suggest you post a new question regarding this requirement so that someone can answer that. The general guideline on Stackoverflow is that a question should be focused to one problem. – Arvind Kumar Avinash Apr 16 '21 at 18:56
  • I post question can you please help me in that. https://stackoverflow.com/q/67131150/15663999?sem=2 here is the link – Sukhwinder pal Singh Apr 16 '21 at 19:39