Ask coding questions

← Back to all posts
Need help
NanaOhene_Nyark (1)

This program is meant to calculate the average of 3 numbers but i keep receiving errors that i don't know how to fix. the Language i used is java, if you could show me what I did wrong I would greatly appreciate it.

Answered by Edgod (58) [earned 5 cycles]
View Answer
Commentshotnewtop
Edgod (58)
  1. You need to import a library that enables input in your program to make the Scanner class work. You first line should be:
    import java.util.*;

  2. In repl.it, you can name your project, but unfortunately, you cannot rename the file you are writing in. The file you are using in repl.it will always be Main.java. Your public class header should be:
    public class Main
    (The .java extension should not be used in that line.)

  3. I recommend using variables to print your sum and average to make your code easier to read.

  4. a+b+c/3 is wrong. Java follows the rule of pemdas. You are dividing c by 3 before adding a and b. It should be:
    (a+b+c)/3

  5. I've noticed some unnecessary curly brackets in your code. Please get rid of those.

It seems that you are new to coding. If you need any help, feel free to ask me.

NanaOhene_Nyark (1)

@Edgod Hello, ive made some of the changes you suggested but I didint quite understand numbers 5,3 and 1 from your original post. And yes I am very new at coding XD and am taking my 1st programming class ever in high school, it is difficult but I find it enjoyable.

Edgod (58)

@NanaOhene_Nyark I have fixed the errors in your program, and did what I think you wanted to do. You can check it using this link (if you want):
https://repl.it/@Edgod/IcyImaginativeCleantech

Let me be more clear on what you didn't understand.
On your first line

import java.util.*;

you have imported a package that contains data in order to make this line work:

Scanner keyboard = new Scanner(System.in);

There are packages in java that enable you to do certain tasks.

On number 3, when I recommended that you should use variables for sum and average, I meant:

int sum = a+b+c;
int average = sum/3;

Using variables can make your code easier to read, but it's up to you on how you want to write your code. By doing this, you can then use variables sum and average in your print statements.

What I've noticed is that in your code, is that you have some curly brackets { } that aren't needed. Over here:

public class main  { Lab2Averagejava
{

and

      public static void main(String[] args) {
  
   {

Ignore the other errors in the first one, both lines only need one { after it, and one } at the end to close it.

By the way, Java is case sensitive, which means Main and main are two different words. You might be wondering why capitalization is important, but you will learn the reason later.

Just wondering, are you using any websites or documents to learn Java?

NanaOhene_Nyark (1)

@Edgod thanks for the help. I am kind of using an app on my phone to learn java from time to time but I do most of my learning in my high school computer science class

PaoloAmoroso (71)

My Java is a bit rusty but it looks like you're using variable sc without defining it. Maybe you meant to use keyboard instead instead of sc?

NanaOhene_Nyark (1)

@PaoloAmoroso Thanks for the reply, I made the changes you suggested and came up with only one error, unlike before where i had 17. This new error still persists even after I change the header to what is "expected". Do you have any Recommendations?

Bochi4Echt (0)

I really do not know what is happening with my computer cause I always getting notifications like
Failed to connect, retrying
but my net is full can you tell me what to do next

HappyFakeboulde (213)

Line 1 -- you can't have . in the class name.