Please help explain the reason for the errors displayed for my Java code

In summary: We can make use of an array with the number of days in the month * and another array with the names of the months and use these two * loops to go through these numbers of days and these names of months*/ int mdays2 []={31, 37, 31,30, 31}; String months2 []={Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct
  • #1
grzz
1,021
21
TL;DR Summary
I cannot see why there are errors displayed in lines 22 and 23.
Java:
//program showing an advantage of using arrays
public class Arr3
{
    public static void main(String []args)
    {
      
        System.out.println("There are 31 days in the month of Jan");
        System.out.println("There are 27 days in the month of Feb");
        System.out.println("There are 31 days in the month of Mar");
        System.out.println("There are 30 days in the month of Apr");
        System.out.println("There are 31 days in the month of May");
     
        /*We can make use of an array with the number of days in the month
         * and another array with the names of the months and use these two
         * loops to go through these numbers of days and these names of
           months*/
         
        int mdays []={31, 37, 31,30, 31};
        String months []={Jan, Feb, Mar, Apr, May};
        for(i = 0; i < mdays.length; i++)
        {
         System.out.println("There are " + mdays[i] + " days in the month
         of " + months[i]);  
        }
       
       

    }
}
I am a beginner in learning Java and hence I need some help because I cannot see the mistakes that I must have made in the above code.
Please help me.
Thanks.
 
Last edited:
Technology news on Phys.org
  • #2
grzz said:
TL;DR Summary: I cannot see why there are errors displayed in lines 22 and 23.
Read the error messages carefully. Do you know what a "string literal" is? What do you think it means when it says there is an "unclosed string literal"?

There are quite a few more errors in your code; before you go on I suggest you revise both Strings and Variable Declaration in Java.
 
  • Like
Likes Wrichik Basu
  • #3
What IDE are you using, @grzz? BlueJ? I suggest you switch to a better IDE like NetBeans (at least) or IntelliJ IDEA. Good IDEs will point out the errors even before you compile your code. BlueJ shouldn't even be considered an IDE; it's just a colourful Notepad with some buttons to compile/run code, which eliminates the use of a terminal.
 
  • #4
pbuk said:
Read the error messages carefully. Do you know what a "string literal" is? What do you think it means when it says there is an "unclosed string literal"?

There are quite a few more errors in your code; before you go on I suggest you revise both Strings and Variable Declaration in Java.
Java:
//program showing an advantage of using arrays
public class Arr3
{
    public static void main(String []args)
    {
      
        System.out.println("There are 31 days in the month of Jan");
        System.out.println("There are 27 days in the month of Feb");
        System.out.println("There are 31 days in the month of Mar");
        System.out.println("There are 30 days in the month of Apr");
        System.out.println("There are 31 days in the month of May");
      
        /*We can make use of an array with the number of days in the month
         * and another array with the names of the months and use these two
         * loops to go through these numbers of days and these names of
           months*/
        int i;
        int mdays []={31, 37, 31,30, 31};
        String months []={"Jan", "Feb", "Mar", "Apr", "May"};
        for(i = 0; i < mdays.length; i++)
        {
         System.out.println("There are " + mdays[i] + " days in the month"+
         "of " + months[i]);   
        }
        
        

    }
}
I corrected my mistakes.
It was very foolish of me not to put the strings of the months in double
quotation marks but I did not know that when starting a new line with strings
one has to put again the quotation marks.

Thank you very much for the help given.
 
  • #5
i corrected the java code.
Thanks pbuk for the help.
It was foolish of me not to put the strings for the months in double quotation marks but I did not know that one has to put these marks even when one goes to a new line.
 
Last edited:
  • #6
Wrichik Basu said:
What IDE are you using, @grzz? BlueJ? I suggest you switch to a better IDE like NetBeans (at least) or IntelliJ IDEA. Good IDEs will point out the errors even before you compile your code. BlueJ shouldn't even be considered an IDE; it's just a colourful Notepad with some buttons to compile/run code, which eliminates the use of a terminal.
I started with Eclipse but I found it too complicated for a beginner and so I switched to Bluej.
 
  • #7
grzz said:
I started with Eclipse but I found it too complicated for a beginner and so I switched to Bluej.
Eclipse is ?:). You can try NetBeans or IntelliJ IDEA. A better IDE will help you code better by pointing out your errors even before you compile the code, suggest improvements to the code, and help you view documentation with ease (which is very much necessary and not taught in most schools/courses).
 
  • Informative
Likes berkeman
  • #8
There is also VS Code Editor with Java plugin support.

https://code.visualstudio.com/

https://code.visualstudio.com/Search?q=java+plugins

I've used all of these IDEs, Eclipse and IBM's extensions to it, Netbeans, and IntelliJ.

Eclipse was great at first but then you'd become enamored with a plugin's features and then the plugin became subscription based or paid for newer features. IBM's extensions would make the IDE slow to start and sometimes slow to use as plugins were loaded. IBM had added Rational plugins to the base of 50 plugins to up to 5000 Rational code analysis plugins (that's a guess) which really slowed things down causing us to seldom turn off our machines.

https://www.ibm.com/products/rad-for-websphere-software

Netbeans is Netbeans, similar to Eclipse but with the plugins you need. It works well and has a custom forms builder aka "Matisse" saving you from hand-coding a GUI for your app but always seemed to lag behind the latest Java features and was somewhat hard to find features you needed with many bells and whistles. And then they moved it to Apache which for a time made it even slower to update. It has a specific directory format for projects preferring either Ant or Maven build structures.

https://netbeans.apache.org/

IntelliJ has a free version and a paid version. I've used the free version and liked it a lot. It was fast and had a clean interface. However, my company had objections to paying for a version and more recently to it being built by a company with Russian ties.

https://news.ycombinator.com/item?id=30453689

VS Code seems to be the "go-to" editor of choice for programmers. It seems to be under active development with good support. It has a wide range of free plugins. It loads fast and supports many popular languages and does not tie you down to a specific project directory format.

More on IDE's:

https://www.techrepublic.com/article/best-ide-software/

https://www.slant.co/topics/10021/~ides
 
  • #9
Aside from the errors already noted, you have a couple of other errors:
Line 8: System.out.println("There are 27 days in the month of Feb");
LIne 18: int mdays []={31, 37, 31,30, 31};

February has more than 27 days, and no month has 37 days. These errors won't affect the running of your code, but others may suspect you don't have a clear understanding of calendars.
 
  • Like
Likes Wrichik Basu and berkeman

FAQ: Please help explain the reason for the errors displayed for my Java code

Why am I getting a "Syntax Error" in my Java code?

Syntax errors occur when the code does not follow the correct syntax rules of Java. This could be due to missing semicolons, parentheses, or curly braces, or using incorrect keywords.

What does a "NullPointerException" mean in Java?

A NullPointerException occurs when you try to access or manipulate an object that is null, meaning it has not been initialized. This can happen when you forget to instantiate an object or when an object is set to null.

How can I fix a "ClassCastException" in my Java code?

A ClassCastException occurs when you try to cast an object to a type that it is not compatible with. To fix this error, make sure to check the type of the object before casting it to avoid runtime exceptions.

Why am I getting an "IndexOutOfBoundsException" in my Java code?

An IndexOutOfBoundsException occurs when you try to access an index that is outside the bounds of an array or collection. To fix this error, make sure to check the size of the array or collection before accessing an index.

What is causing a "NoSuchMethodError" in my Java code?

A NoSuchMethodError occurs when a method that is expected to be available at runtime is not found. This can happen if the method signature has changed or if the method is not available in the specified class or package.

Similar threads

Replies
2
Views
2K
Replies
13
Views
4K
Replies
1
Views
6K
Replies
2
Views
12K
Replies
4
Views
1K
Replies
2
Views
2K
Replies
2
Views
1K
Back
Top