0

I am new in this Java journey, at College they are asking me to

"Define five String variables in the main method called: shipmentNum, supplierName, revDate, revTime, employeeNum." And assign the following text:99, Costco, 12/15/2011, 10:25 AM, 33."

I have this so far, but is giving an error message: "the local variable shipmentNum is never read", I don't see why am I getting this error message.

package c1;

public class ShipmentApp {

public static void main(String[] args){
    String shipmentNum = "99";
    String supplierName = "Costco";
    String revDate = "12/15/2011";
    String revTime = "10:25 AM";
    String employeeNum = "33";


    System.out.println("99");
    System.out.println("Costco");
    System.out.println("12/15/2011");
    System.out.println("10:25 AM");
    System.out.println("33");
    }
}
8
  • 7
    I think the one you are seeing is warning not error. Commented Sep 13, 2013 at 15:30
  • 1
    It is a warning, you should be printing out like this anyway: System.out.println(shipmentNum); Commented Sep 13, 2013 at 15:31
  • It's just a warning and not an error message, but the real question is why after defining the variables you haven't used them. This would be what the warning message is trying to tell you. Commented Sep 13, 2013 at 15:31
  • first of all try printing the variables, also are u sure its an error and not a warning? Commented Sep 13, 2013 at 15:31
  • also you can print the variable inside SOP Commented Sep 13, 2013 at 15:31

7 Answers 7

1

What you are seeing is a compiler warning, not an error. This is basically Java trying to help you find flaws in your code by analyzing what you wrote and detecting common mistakes.

In this case, Java recognized that you assigned values to a bunch of variables, but after that never use those variables again. You probably want to write out the values of the variables, not the assigned values again.

public static void main(String[] args){
    String shipmentNum = "99";
    String supplierName = "Costco";
    String revDate = "12/15/2011";
    String revTime = "10:25 AM";
    String employeeNum = "33";

    System.out.println(shipmentNum );
    System.out.println(supplierName );
    System.out.println(revDate );
    System.out.println(revTime );
    System.out.println(employeeNum );

}
Sign up to request clarification or add additional context in comments.

Comments

1

Thats warning not error but try this.

public static void main(String[] args){
    String shipmentNum = "99";
    String supplierName = "Costco";
    String revDate = "12/15/2011";
    String revTime = "10:25 AM";
    String employeeNum = "33";


    System.out.println(shipmentNum);
    System.out.println(supplierName);
    System.out.println(revDate);
    System.out.println(revTime);
    System.out.println(employeeNum);

}

or just try:

package c1;

public class ShipmentApp{

    public static void main(String[] args){
         System.out.println("99");
        System.out.println("Costco");
        System.out.println("12/15/2011");
        System.out.println("10:25 AM");
        System.out.println("33");

    }
}

Comments

0

What you are receiving are warnings because you are not actually ever reading any of the variables you have declared. You could correct this by passing the variables to println instead of typing out the strings twice.

Being that these are only warnings, they should not affect the ability of the program to compile and/or execute. While you could conceivably ignore such warnings, it's usually wise to at least analyze what they are being caused by.

Comments

0

It's a warning that the java compiler tells you that you defined a variable but never used it.

The purpose of a variable, is that it stores information that will be used at some later point in your code. Java gives you a warning, because if you define a variable but never use it you've likely made a mistake. This is because variables that are never used are basically nonsense, and should never have been declared.

Try these print statements:

System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);

Comments

0

That's is not an Error,It is just a warning shown by the IDE or compiler. There is no issue in this code to compile and run.

You may want to do as follows

    String shipmentNum = "99";
    String supplierName = "Costco";
    String revDate = "12/15/2011";
    String revTime = "10:25 AM";
    String employeeNum = "33";


    System.out.println(shipmentNum);
    System.out.println(supplierName);
    System.out.println(revDate);
    System.out.println(revTime);
    System.out.println(employeeNum);

Comments

0

You are writing the text out, not the value of the variables. It isn't an error, just a warning. You should probably change your code to this:

package c1;

public class ShipmentApp{

    public static void main(String[] args){
        String shipmentNum = "99";
        String supplierName = "Costco";
        String revDate = "12/15/2011";
        String revTime = "10:25 AM";
        String employeeNum = "33";


        System.out.println(shipmentNum);
        System.out.println(supplierName);
        System.out.println(revDate);
        System.out.println(revTime);
        System.out.println(employeeNum);

    }
}

Comments

0

this message is just to notice you, you have some variables haven't been used. It is not a error. if you dont want to see this warning or you cannot stand it, you can add @supresswarnings annotation at the beginning of the class. or you just follow others' suggestion to use the variables you have created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.