0

I am trying to use IcecreamArray[] in the method searchByCompany() to print out ice cream information by its company name, but it is giving me an error that the array does not exist.

Error: Create local variable IcecreamArray. Also, how can I make my code more efficient?

public class MainIcecream {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Icecream[] IcecreamArray = new Icecream[5];
        for (int i = 0; i < 5; i++) {
            IcecreamArray[i] = new Icecream();
            System.out.println("Enter Icecream type " + i + " : ");
            IcecreamArray[i].setIcecreamType(sc.next());
            System.out.println("Enter Icecream Company " + i + " : ");
            IcecreamArray[i].setIcecreamCompany(sc.next());
            System.out.println("Enter Icecream Price " + i + " : ");
            IcecreamArray[i].setIcecreamPrice(sc.nextDouble());
        }

    public static void searchByCompany(String s){
        for (int i = 0; i < 5; i++) {
            if(s.equals(IcecreamArray[i].getIcecreamCompany())){
                System.out.println(IcecreamArray[i].getIcecreamType());
                System.out.println(IcecreamArray[i].getIcecreamCompany());
                System.out.println(IcecreamArray[i].getIcecreamPrice());
            } 
        }
    }

    public static class Icecream {

        private String icecreamType;
        private String icecreamCompany;
        private double icecreamPrice;

        public String toString(){
            String str , t , c;
            double p;
            t = icecreamType;
            c = icecreamCompany;
            p = icecreamPrice;
            str = "Icecream type: " + t + "\nManufacturer: " + c + "\nPrice: " + p;
            return str;
        }
        public void setIcecreamType(String t) {
            icecreamType = t;   
        }
        public void setIcecreamCompany(String c){
            icecreamCompany = c;
        }
        public void setIcecreamPrice(double p){
            icecreamPrice = p;
        }
        String getIcecreamType(){
            return icecreamType;
        }
        String getIcecreamCompany(){
            return icecreamCompany;
        }
        double getIcecreamPrice(){
            return icecreamPrice;
        }
    }
}

4 Answers 4

1

You have declared and initialised IcecreamArray inside of the main method so outside of main IcecreamArray doesn't exist.

You could pass IcecreamArray as an argument to searchByCompany or else declare it outside main as a static variable

I took a closer look at your code and you have tried to declare searchByCompany inside of main which is not valid. You cannot declare a method inside another method. Below I have included an example of what you could do

public class MainIcecream {

    private static Icecream[] IcecreamArray = new Icecream[5];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < 5; i++) {
            IcecreamArray[i] = new Icecream();
            System.out.println("Enter Icecream type " + i + " : ");
            IcecreamArray[i].setIcecreamType(sc.next());
            System.out.println("Enter Icecream Company " + i + " : ");
            IcecreamArray[i].setIcecreamCompany(sc.next());
            System.out.println("Enter Icecream Price " + i + " : ");
            IcecreamArray[i].setIcecreamPrice(sc.nextDouble());
        }

        System.out.println("Enter company name to search");
        searchByCompany(sc.next());
    }

    private static void searchByCompany(String s) {
        for (int i = 0; i < 5; i++) {
            if (s.equals(IcecreamArray[i].getIcecreamCompany())) {
                System.out.println(IcecreamArray[i].getIcecreamType());
                System.out.println(IcecreamArray[i].getIcecreamCompany());
                System.out.println(IcecreamArray[i].getIcecreamPrice());
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you guide me how can I do that? I am new to OOP
Thank you. I did not create a method inside a method. I did a mistake in the syntax.
0

Your program has some compiler errors. Apart from that, IcecreamArray is initialized inside the main method and it's scope lies inside that method. It's not visible to other methods because it doesn't exist in their context.

Here are your options,

  1. Pass the array as an argument to the searchByCompany method, so searchByCompany(String s, Icecream[] array)

  2. Make the array a field of the class so it can be accessed by other methods (in this case make it a static field), so public static Icecream[] IcecreamArray = new Icecream[5];

Comments

0

I fixed the code by passing IcecreamArray as an argument to searchByCompany.

Here's the final code:

public class MainIcecream {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Icecream[] IcecreamArray = new Icecream[5];
        for (int i = 0; i < 5; i++) {
            IcecreamArray[i] = new Icecream();
            System.out.println("Enter Icecream type " + i + " : ");
            IcecreamArray[i].setIcecreamType(sc.next());
            System.out.println("Enter Icecream Company " + i + " : ");
            IcecreamArray[i].setIcecreamCompany(sc.next());
            System.out.println("Enter Icecream Price " + i + " : ");
            IcecreamArray[i].setIcecreamPrice(sc.nextDouble());
        }
}
    public static void searchByCompany(Icecream IcecreamArray[]) {
        System.out.println("Enter manufacturer to search for icecream information: ");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        for (int i = 0; i < 5; i++) {
            if (s.equals(IcecreamArray[i].getIcecreamCompany())) {
                System.out.println("Icecream Type: " + IcecreamArray[i].getIcecreamType());
                System.out.println("Icecream Company: " + IcecreamArray[i].getIcecreamCompany());
                System.out.println("Icecream Price: $" + IcecreamArray[i].getIcecreamPrice());
            }
        }
    }
    public static class Icecream {

        private String icecreamType;
        private String icecreamCompany;
        private double icecreamPrice;

        public String toString(){
            String str , t , c;
            double p;
            t = icecreamType;
            c = icecreamCompany;
            p = icecreamPrice;
            str = "Icecream type: " + t + "\nManufacturer: " + c + "\nPrice: " + p;
            return str;
        }
        public void setIcecreamType(String t) {
            icecreamType = t;   
        }
        public void setIcecreamCompany(String c){
            icecreamCompany = c;
        }
        public void setIcecreamPrice(double p){
            icecreamPrice = p;
        }
        String getIcecreamType(){
            return icecreamType;
        }
        String getIcecreamCompany(){
            return icecreamCompany;
        }
        double getIcecreamPrice(){
            return icecreamPrice;
        }
    }
}

Comments

0

try something like this. hope it will resolve your issue:----

import java.util.Scanner;

public class MainIcecream {

    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Icecream[] IcecreamArray = new Icecream[5];
        for (int i = 0; i < IcecreamArray.length; i++) {
            System.out.println("Enter Iceiceicecream Price " + i + " : ");
            double icecream_price = sc.nextDouble();
            System.out.println("Enter Icecream type " + i + " : ");
            String icecream_type = sc.next();

            System.out.println("Enter Icecream Company " + i + " : ");
            String icecream_company = sc.next();
            IcecreamArray[i] = new Icecream(icecream_type, icecream_company, icecream_price);
            
        }
        sc.nextLine();
        System.out.println("Enter Icecream Company  to retrieve all ");
        String icecreamCompany = sc.nextLine();
        searchByCompany(icecreamCompany, IcecreamArray);
        sc.close();
    }

    public static void searchByCompany(String s, Icecream[] icecreamArray) {
        boolean flag = false;
        for (int i = 0; i < icecreamArray.length; i++) {

            if (s.equals(icecreamArray[i].getIcecreamCompany())) {
                flag = true;
                System.out.println(icecreamArray[i].getIcecreamType());
                System.out.println(icecreamArray[i].getIcecreamCompany());
                System.out.println(icecreamArray[i].getIcecreamPrice());
            }
        }
        if (!flag) {
            System.out.println("record not found");
        }

    }

    public static class Icecream {

        private String icecreamType;
        private String icecreamCompany;
        private double icecreamPrice;

        public Icecream(String icecream_type, String icecream_company, double icecream_price) {
            this.icecreamType = icecream_type;
            this.icecreamCompany = icecream_company;
            this.icecreamPrice = icecream_price;
        }

        public String toString() {
            String str, t, c;
            double p;
            t = icecreamType;
            c = icecreamCompany;
            p = icecreamPrice;
            str = "Icecream type: " + t + "\nManufacturer: " + c + "\nPrice: " + p;
            return str;
        }

        public void setIcecreamType(String t) {
            icecreamType = t;
        }

        public void setIcecreamCompany(String c) {
            icecreamCompany = c;
        }

        public void setIcecreamPrice(double p) {
            icecreamPrice = p;
        }

        String getIcecreamType() {
            return icecreamType;
        }

        String getIcecreamCompany() {
            return icecreamCompany;
        }

        double getIcecreamPrice() {
            return icecreamPrice;
        }
    }
}

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.