Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:51:46+00:00 2026-05-26T20:51:46+00:00

ok so I’m kinda new to java and I’m trying to make a class

  • 0

ok so I’m kinda new to java and I’m trying to make a class which will be able to ask the user to input a 12 digit long upc code, check to make sure its a valid code, and then display if it is or not. I’ve got quite a few errors with the current program that I have and I can’t seem to figure it out. This is the code that I have so far:

public class Upc {
    private long upc;

    public Upc(long upcs) {
        upc = upcs;
    }

    public long getUpc() {

        int m = (n2 + n4 + n6 + n8 + n10);
        long n = (n1 + n3 + n5 + n7 + n9 + n11);
        long r = (10 - (m + 3 * n) % 10);

        long n12 = (int) (upc % 10);
        upc /= 10;
        long n11 = (int) (upc % 10);
        upc /= 10;
        long n10 = (int) (upc % 10);
        upc /= 10;
        long n9 = (int) (upc % 10);
        upc /= 10;
        long n8 = (int) (upc % 10);
        upc /= 10;
        long n7 = (int) (upc % 10);
        upc /= 10;
        long n6 = (int) (upc % 10);
        upc /= 10;
        long n5 = (int) (upc % 10);
        upc /= 10;
        long n4 = (int) (upc % 10);
        upc /= 10;
        long n3 = (int) (upc % 10);
        upc /= 10;
        long n2 = (int) (upc % 10);
        upc /= 10;
        long n1 = (int) (upc % 10);

        if (r == n12) {
            return (upc + " is a feasible UPC code");
        } else {
            return (upc + " is an invalid UPC code");
        }
    }
}

and my errors are as follows:

13 errors found:
File: C:\Users\Andrew\Downloads\Upc.java  [line: 10]
Error: n2 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 10]
Error: n4 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 10]
Error: n6 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 10]
Error: n8 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 10]
Error: n10 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 11]
Error: n1 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 11]
Error: n3 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 11]
Error: n5 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 11]
Error: n7 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 11]
Error: n9 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 11]
Error: n11 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java  [line: 39]
Error: Type mismatch: cannot convert from java.lang.String to long
File: C:\Users\Andrew\Downloads\Upc.java  [line: 42]
Error: Type mismatch: cannot convert from java.lang.String to long

I feel like fixing one or two things will eliminate alot of this, can anyone help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:51:47+00:00Added an answer on May 26, 2026 at 8:51 pm

    this variable define after

    long n12 = (int) (upc%10);
    upc /= 10;
    long n11 = (int) (upc%10);
    upc /= 10;
    long n10 = (int) (upc%10);
    upc /= 10;
    long n9 = (int) (upc%10);
    upc /= 10;
    long n8 = (int) (upc%10);
    upc /= 10;
    long n7 = (int) (upc%10);
    upc /= 10;
    long n6 = (int) (upc%10);
    upc /= 10;
    long n5 = (int) (upc%10);
    upc /= 10;
    long n4 = (int) (upc%10);
    upc /= 10;
    long n3 = (int) (upc%10);
    upc /= 10;
    long n2 = (int) (upc%10);
    upc /= 10;
    long n1 = (int) (upc%10);
    
    
    
    int m = (n2 + n4 + n6 + n8 + n10);
    long n = (n1 + n3 + n5 + n7 + n9 + n11);
    long r = (10-(m+3*n)%10);
    

    you’r using that variable which was not been defined and after using you define that variables

    Edit : for if

    As you define you method with the return value as long time and you return the String value, see the return value

    return (upc + " is a feasible UPC code"); 
    

    you need to change the return type either in method or as return time like if you want to return this then you method signature looks like this

    public long getUpc(){
      // and return will work
      return (upc + " is a feasible UPC code"); 
    }
    

    but if you want to only numeric value then do not change it’s method signature just return the upc like this

    return upc;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.