0

I'm trying to create a system that picks randomly from a list, and then, depending on which string it picked, picks another random string from a separate group, and then another one from there.

Basically, I'm trying to give it a bunch of paths and then, depending on what choice it makes, narrow the subsequent paths down two more times.

I've got the random code going, but I can't get it to accept if/then/else statements.

Here is my code. It works for its current purpose, which is generating one of those 4 letters randomly. But because it doesn't call a variable, it won't accept if/then/else statements. If I try to make a variable, it complains about being unable to convert the variable to Boolean, but if I define it as Boolean the entire code breaks.

package randomlist;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Randomlist {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Random generate = new Random();
        String[] name = {"A", "B", "C", "D"};
        System.out.println(name[generate.nextInt(4)]);
        String nesting = (name[generate.nextInt(4)]);
        if (nesting = "A") {
            Random generate = new Random();
            String[] name = {"E", "F", "G", "H"};
        }
        else if (nesting = "B") {
            Random generate = new Random();
            String[] name = {"I", "J", "K", "L"};
        }
    }
}

EDIT: Seems nobody is able to be helpful without also being needlessly sarcastic and demeaning. Apparently, it's not okay for people to learn how to do things and everyone is expected to be an expert from the beginning. (Also, if the code compiles, it works. It might not work as intended, but it works. I do not understand the point of arguing with someone about that. Do not tell me my code won't function when I have tested it and it does, in fact, compile without errors).

Fortunately, I solved my own problem, mostly out of spite. It requires new variables every time and a lot of very short if statements, but it does what I need it to do.

Edit #2: it turns out it's willing to reuse the same variables for each nested level, so while it won't let me reuse (name) for the lower level, I can use (name1) for each of the lower nests and (name2) for the third-order ones.

  • 2
    "Here is my code. It works for its current purpose" - no, it really doesn't. It won't compile for various reasons, and you really need to read https://stackoverflow.com/questions/513832. It's not clear to me why you're trying to redeclare local variables which are already in scope, but you can't do that... and I'd *very strongly* advise you to use consistent formatting which will make the structure of the code much clearer. – Jon Skeet Aug 19 '23 at 07:02
  • 1
    [The Java Tutorials](https://docs.oracle.com/javase/tutorial/) – Abra Aug 19 '23 at 08:43
  • 1
    Yes, it does compile. It compiles without a problem and outputs a random letter. The code works; I just can't get it to work *in this specific context*. – Richey Baumann Aug 19 '23 at 13:44
  • What is the overall purpose of this code? There may be a better way to accomplish it. – DevilsHnd - 退職した Aug 19 '23 at 22:35
  • 1
    There probably is, but everyone has decided to be unhelpful and condescending instead of providing that information. The purpose of the code is to pick ideas and then pick more specific ideas based on those. So it'll draw one, then based on that one it'll draw another, more specific one, and so on. – Richey Baumann Aug 20 '23 at 02:55
  • 1
    I recommend that you learn about methods. When you have an "and then..." this almost always means you can implement what came before as one method and what comes next as another method. – Code-Apprentice Aug 20 '23 at 03:27
  • "*compile without errors*" ?!? `if (nesting = "A")` should never compile in Java; neither the re-declaration of `generate` - if "*everyone has decided to be unhelpful*" maybe the problem is that you have not posted the correct code. (if every car is driving on the wrong side of the road, I would ask myself, if I am the one on the wrong side...) This was already given in first comment, but seems like you did not interpret it as help, but only as confrontation :-( – user16320675 Aug 20 '23 at 07:03
  • 1
    The code is correct. So was my response. The code was compiling without issues, so everyone had decided to focus on an issue that wasn't an issue. Instead of providing solutions or asking for context, they assumed that they were right and made a bunch of determinations from there. The code *did* compile; the one person who actually answered the question *did* provide a useful, if needlessly sarcastic and demeaning, answer. And someone else, on a different question, took the time to help me condense the code into 12 lines. – Richey Baumann Aug 20 '23 at 18:48
  • 6 compile errors: https://ideone.com/7Qnmrf ([screenshot](https://i.stack.imgur.com/5RQwb.png)) – user16320675 Aug 20 '23 at 19:57

1 Answers1

1

You better should start with some Java lessons:

  • The =-operator in any case will assign the right value to the variable on the left side.
  • For comparison of two primitive types (int, double) you have to use the ==-operator. But since string are objects you'll have to use the equals-method: "A".equals(nesting).
Karsten
  • 195
  • 5
  • 1
    Unfortunately, your "A".equals(nesting) advice was useful, so I have to let the unnecessary and utterly useless- not to mention badly misaimed- sarcasm slide. I've been doing Java coding for 6 years now. I usually code mini-bots to press buttons for me repetitively or to parse for keywords, things like that. I rarely use it for anything more complex, although in my first Java class we were taught to use Java to generate a snowman and to pull up an Internet link. – Richey Baumann Aug 20 '23 at 03:34