Home > Blockchain >  Why am i getting a error like this in java
Why am i getting a error like this in java

Time:03-12


mport java.util.*;
import java.util.Scanner;

public class intro {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        ArrayList<String> produceList = new ArrayList();

        ArrayList<String> drinksList = new ArrayList();


       while (true) {
        System.out.println("Enter 1 to add to drinks list or enter 2 to add to produce list");
        int ch_list = scanner.nextInt();

        if (ch_list == 1){
            
            System.out.println("Welcome to the drinks list");
            str add_drinks = System.out.println("What would you like to add to the drinks list?");

            System.out.println();



        }

        else if (ch_list == 2) {

            System.out.println("Welcome to the produce List");
        }

       }
        }


    }
        

I am getting an red line under the system.out.println thatr has a variable assigned to it. Is it because i have not full declared the variable. I really need some help!!!

CodePudding user response:

System.out.println() cannot be stored in a variable as it returns void. Instead, try storing the string you want to print in the variable and calling System.out.println(add_drinks).

CodePudding user response:

Remove the str add, It is because you try to assign a unassigned without any datatype variable, the compiler is confused by "What do you mean here I don't get it"


       while (true) {
        System.out.println("Enter 1 to add to drinks list or enter 2 to add to produce list");
        int ch_list = scanner.nextInt();

        if (ch_list == 1){
            
            System.out.println("Welcome to the drinks list");
           // str add_drinks = System.out.println("What would you like to add to the drinks list?");   // Delete this line instead write like the line below
         System.out.println("What would you like to add to the drinks list?");

            System.out.println();



        }

        else if (ch_list == 2) {

            System.out.println("Welcome to the produce List");
        }

  •  Tags:  
  • java
  • Related