Question:
what is wrong with my java code?
Kuddels
2010-10-17 13:44:19 UTC
this is my first programming class, and we are being introduced to programming and java. I'm writing my code, and I keep getting a syntax error, and i can't figure out what's wrong with it. I tend to check frequently for mistakes, and already i have one. i know i haven't completed the code, but i would like to be able to be error free as much as i can. any advice would be greatly appreciated.

if possible could u tell me what is wrong with my code? and how i can fix it?

the code is as follows:


//This program calculates Fed income tax, State tax, Social security tax, Medicare/Madicaid tax, pension plan, health insurance,

import java.util.*;

public class Netpay
{

public static void main(String [] args)

{
Scanner input=new Scanner(System.in);

String name;
System.out.println("The name of employee");

name=input.nextLine();

grosspay=input.nextDouble();
}
}

the error is:
----jGRASP exec: javac -g Netpay.java

Netpay.java:25: cannot find symbol
symbol : variable grosspay
location: class Netpay
grosspay=input.nextDouble();
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Four answers:
Joseph
2010-10-17 14:26:20 UTC
there are a few things wrong here.

1. the variable "grosspay" needs to be "declared" aka created first Ex. double grosspay;

2. you need to give the user a chance to enter the gross pay. in this code you only ask for the name

between name=input.nextLine(); and grosspay=input.nextDouble(); you should write something like

System.out.print("Enter the gross pay: ");

so the program stops and gives the user a chance to enter it

WAIT!!!

Don't forget this one last part. it might be a little over your head since you are new, but, it is important to always get String input before primitive data types such as double. you will get some weird results otherwise. ask your teacher for a better explination.

P.S. you dont need to change it here cuz its already right
modulo_function
2010-10-17 13:48:39 UTC
You're using grosspay before you've defined it



Use:



double grosspay = input.nextDouble();



Note that you correctly defined name before you involked nextLine() to get its value. You need to do the same for grosspay.
Irakli
2010-10-17 16:28:51 UTC
You don't have grosspay declared

so, you'd have to do:

double grosspay = input.nextDouble();



or you could do:

double grosspay;

grosspay = input.nextDouble(); (just like you did for the String name)
sabatini
2016-11-05 09:46:11 UTC
i don't comprehend what you propose by skill of "marked as unread". inspite of the undeniable fact that, one subject you are going to have is which you're evaluating 2 Strings with the == operator. whilst utilized to merchandise varieties, like String, the == operator compares the references — in actuality this is checking to be certain if the two variables communicate over with a similar merchandise. It would not verify the values of those products. What you want to do is come across the String classification's equals() approach to evaluate them, like so: if (person.equals("Bob")) which could get you the end result you want.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...