Search code examples
javaprogram-entry-point

Public class Main not working, is this code even possible or?


I was doing some practicing with Java and I decided to try out some things so I asked my professor to give me some ideas to practice on and came up with this. When I went to compile it in Eclipse it wouldn't work, it has something to do with the public class Main but I have no clue what. Please tell me what I am doing wrong or if this would work at all.

This is the error I get

 public class Forecast 
{
    public int temperature;
    public int pressure;

}

public class Main
{
    public static void changeTheString(String weather)
    {
        weather = "sunny";
    }

    public static void changeTheArray(String[] rainyDays)
    {
        rainyDays[1] = "Sunday";
    }
    public static void changeTheObject(Forecast forecast)
    {
        forecast.temperature = 35;
    }
    public static void main (String[] args)
    {
        String weather = "rainy";
        changeTheString(weather);
        String[] rainyDays;
        System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);

        String[] rainyDays = new String[] {"Monday", "Friday"};
        changeTheArray(rainyDays);
        System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);

        Forecast forecast = new Forecast();
        forecast.pressure = 700;
        forecast.temperature = 20;
        changeTheObject(forecast);
        System.out.println("The temperature is " + forecast.temperature + " Celsius");

    }

}

Solution

  • As aziz_aon pointed out you also need to either declare the class main in a separate file or make the class Forecast an internal class. I'd suggest to move the Forecast class inside of the Main class To do that you have to move it inside Main like you'd do it with a function. (Sorry for formatting I'm on mobile) The problem is you declare

    String[] rainyDays;
    

    then you try to assigen a new value like

    String[] rainyDays = new String[] {"Monday", "Friday"};
    

    but you already defined rainyDays as String[] so you can't do that again
    change it to

    rainyDays = new String[] {"Monday", "Friday"};
    

    so your class looks like this:

     public class Forecast 
     {
         public int temperature;
         public int pressure;
    
    }
    
    public class Main
    {
        public static void changeTheString(String weather)
        {
            weather = "sunny";
        }
    
        public static void changeTheArray(String[] rainyDays)
        {
        rainyDays[1] = "Sunday";
        }
        public static void changeTheObject(Forecast forecast)
        {
            forecast.temperature = 35;
        }
        public static void main (String[] args)
        {
        String weather = "rainy";
        changeTheString(weather);
        String[] rainyDays;
        System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
    
        rainyDays = new String[] {"Monday", "Friday"};
        changeTheArray(rainyDays);
        System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
    
        Forecast forecast = new Forecast();
        forecast.pressure = 700;
        forecast.temperature = 20;
        changeTheObject(forecast);
        System.out.println("The temperature is " + forecast.temperature + " Celsius");
    
    }
    
    }