Common coding errors in java

Understanding Common Errors In Java

Picture of Prahasith Veluvolu

When we were building our automated common error feedback feature on Mimir Classroom, we analyzed millions of stack traces that our students received when completing their coursework on our platform. In this post, you will find the errors we found to be most troubling to students in Java and some tips on how to approach them.

To start off, here is a quick refresher on how to read an error in the Java stack trace.

JavaStackTrace

1. ’;’ expected

This error means that you forgot a semicolon in your code. If you look at the full error statement, it will tell you where in your code you should check within a few lines. Remember that all statements in Java must end in a semicolon and elements of Java like loops and conditionals are not considered statements.

2. cannot find symbol

This error means that Java is unable to find an identifier that it is trying to work with. It is most often caused by the following:

  • You misspelled a variable name, class name, or a keyword somewhere. Remember that Java is case sensitive.
  • You are trying to access a variable or class which does not exist or can not be accessed.
  • You forgot to import the right class.

3. illegal start of expression

This error usually occurs when your code does not follow Java’s standard structure. Check the nesting of your variables and ensure that all your < >and ( ) are in the right place..

4. class, interface, or enum expected

public class Driver < public static void main (String[] args)< int variableOne = 1; >> System.out.println("Hello");

This error is usually caused by misplaced < >. All code in Java needs to be contained within a class, interface, or enum. Ensure that all of your code is within the < >of one of those. We often have seen this error when there is an extra > at the end of your code.

Читайте также:  Forum trade print ru showthread php

5. reached end of file while parsing

This error usually occurs when you are missing a > somewhere in your code. Ensure that for every < you have one >in the right place.

6. missing return statement

public class Calculator < public Calculator()< >public int sum(int one, int two) < int sum = one + two; >>

This error usually means one of two things:

  • Your method is expecting a return statement but is missing one. Ensure that if you declared a method that returns something other than void and that it returns the proper variable type.
  • Your return statements are inside conditionals who’s parameters may not be reached. In this case, you will need to add an additional return statement or modify your conditionals.

7. ‘else’ without ‘if’

import java.util.Scanner; public class Driver < public static void main (String[] args)< Scanner scan = new Scanner(System.in); int in = scan.nextInt(); else if( in >10) System.out.println("Your input is greater than 10"); else System.out.println("Your input is less than 5"); > >

This error means that Java is unable to find an if statement associated to your else statement. Else statements do not work unless they are associated with an if statement. Ensure that you have an if statement and that your else statement isn’t nested within your if statement.

8. ‘(‘ expected or ‘)’ expected

import java.util.Scanner; public class Driver < public static void main (String[] args)< Scanner scan = new Scanner(System.in); int in = scan.nextInt(); if in >10) System.out.println("Your input is greater than 10"); else System.out.println("Your input is less than 5"); > >

This error means that you forgot a left or right parenthesis in your code. If you look at the full error statement, it will tell you where in your code you should check. Remember that for every ( you need one ).

9. case, default, or ‘>’ expected

This error means that your switch statement isn’t properly structured. Ensure that your switch statement contains a variable like this: switch (variable). Also ensure that every case is followed by a colon (:) before defining your case.

10. ‘.class’ expected

public class Calculator < public Calculator()< >public int sum(int one, int two) < int s = one + two; return int s; >>

This error usually means that you are trying to declare or specify a variable type inside of return statement or inside of a method calls. For example: “return int 7;» should be «return 7;»

Читайте также:  Docker laravel nginx php mysql

11. invalid method declaration; return type required

public class Thing < int mode; public Thing()< mode = 0; >public setMode(int in) < mode = in; >>

Every Java method requires that you declare what you return even if it is nothing. «public getNothing()» and «public static getNumber()» are both incorrect ways to declare a method.

The correct way to declare these is the following: «public void getNothing()» and «public static int getNumber()»

12. unclosed character literal

This error occurs when you start a character with a single quote mark but don’t close with a single quote mark.

13. unclosed string literal

This error occurs when you start a string with a quotation mark but don’t close with a second quotation mark. This error can also occur when quotation marks inside strings are not escaped with a backslash.

14. incompatible types

This error occurs when you use the wrong variable type in an expression. A very common example of this is sending a method an integer when it is expecting a string. To solve this issue, check the types of your variables and how they are interacting with other types. There are also ways to convert variable types.

15. missing method body

public class Calculator < int mode; public Calculator()< >public void setMode(int in); < mode = in; >>

This error commonly occurs when you have a semicolon on your method declaration line. For example «public static void main(String args[]);». You should not have a semicolon there. Ensure that your methods have a body.

16. unreachable statement

public class Calculator < int mode; public Calculator()< >public void setMode(int in); < mode = in; >>

This error means that you have code that will never be executed. Usually, this is after a break or a return statement.

If you have any other errors that you find your students encountering often, reach out to us at hello@mimirhq.com and let us know!

Источник

Common coding errors in java

  • The basics of TOGAF certification and some ways to prepare TOGAF offers architects a chance to learn the principles behind implementing an enterprise-grade software architecture, including.
  • Haskell vs. PureScript: The difference is complexity Haskell and PureScript each provide their own unique development advantages, so how should developers choose between these two .
  • A quick intro to the MACH architecture strategy While not particularly prescriptive, alignment with a MACH architecture strategy can help software teams ensure application .
  • Postman API platform will use Akita to tame rogue endpoints Akita’s discovery and observability will feed undocumented APIs into Postman’s design and testing framework to bring them into .
  • How to make use of specification-based test techniques Specification-based techniques can play a role in efficient test coverage. Choosing the right techniques can ensure thorough .
  • GitHub Copilot Chat aims to replace Googling for devs GitHub’s public beta of Copilot Chat rolls out GPT-4 integration that embeds a chat assistant into Visual Studio, but concerns .
  • Navigate multi-cloud billing challenges Keeping track of cloud bills from multiple clouds or accounts can be complex. Learn how to identify multi-cloud billing .
  • 5 Google Cloud cost optimization best practices Cost is always a top priority for enterprises. For those considering Google Cloud, or current users, discover these optimization .
  • How to create and manage Amazon EBS snapshots via AWS CLI EBS snapshots are an essential part of any data backup and recovery strategy in EC2-based deployments. Become familiar with how .
  • BrightTALK @ Black Hat USA 2022 BrightTALK’s virtual experience at Black Hat 2022 included live-streamed conversations with experts and researchers about the .
  • The latest from Black Hat USA 2023 Use this guide to Black Hat USA 2023 to keep up on breaking news and trending topics and to read expert insights on one of the .
  • API keys: Weaknesses and security best practices API keys are not a replacement for API security. They only offer a first step in authentication — and they require additional .
  • AWS Control Tower aims to simplify multi-account management Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates .
  • Break down the Amazon EKS pricing model There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service .
  • Compare EKS vs. self-managed Kubernetes on AWS AWS users face a choice when deploying Kubernetes: run it themselves on EC2 or let Amazon do the heavy lifting with EKS. See .
Читайте также:  String максимальное количество символов java

Источник

Оцените статью