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,006
15
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

1. What are common errors displayed in Java code?

Some common errors displayed in Java code include syntax errors, logic errors, and runtime errors.

2. How can I identify and fix syntax errors in my Java code?

Syntax errors are caused by incorrect use of Java syntax and can be identified by the red underlines in an IDE. To fix these errors, carefully review the error message and check for missing semicolons, parentheses, or curly braces.

3. What are logic errors and how do I troubleshoot them in my Java code?

Logic errors occur when there is a mistake in the program's logic, resulting in incorrect output. These can be challenging to identify, but debugging tools and careful code review can help troubleshoot and fix them.

4. What is a runtime error and how do I handle it in my Java code?

A runtime error occurs during the execution of a program, typically caused by unexpected input or invalid operations. These can be handled using try-catch blocks and exception handling techniques.

5. How can I prevent errors in my Java code?

To prevent errors, it is important to follow best practices such as writing clean and organized code, testing and debugging regularly, and using proper error handling techniques. It is also helpful to have a good understanding of Java syntax and programming concepts.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top