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 7831373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:41:59+00:00 2026-06-02T11:41:59+00:00

I’m trying to learn Java and basically my approach has been to take the

  • 0

I’m trying to learn Java and basically my approach has been to take the procedural style I learned with python, and apply it to Java. So I never use classes and just put everything in a single class with many methods(which I just use as python functions). I think I’ve hit a problem, and need to bite the bullet and use classes, but I’m having trouble wrapping my head around how to do it.

To simplify my problem(ignore the poor design- it’s just to illustrate the point), I have a program that takes a list and within a for loop does some math on each item(in this case adds 1 to the value of the list). I only want it to do work on 2 items on the list and then stop(in this example it’s the first 2 items but in my real program it could be anywhere in the list). Here’s the working code that is similar to how I’m already doing it:

No Classes:

public class LearningClasses {
    public static void main(String[] args) {
        int[] list = new int[]{1,2,3,4,5,6,7,8,9,10};
        int[] data_list = new int[list.length];

        for (int current_location = 0; current_location<list.length;current_location++) {
            for (int i =0; i<100; i++){
                if (check_size(data_list) == false ) {
                    break;
                }
                data_list[current_location] = (list[current_location]+1);
            }
        }

        //its done now lets print the results
        for (Integer item : data_list) {
            System.out.println(item);
        }
    }

    private static boolean check_size(int[] data_list) {
        // TODO Auto-generated method stub
        int count = 0;
        for (int item : data_list) {
            if (item != 0) {
                count++;
                if (count>=2) {
                    break;
                }
            }
        }
        if (count>=2) {
            return false;
        } else {
            return true;
        }
    }
}

The problem with this code is although it works it’s inefficient because it calculates the count on every iteration of the second for loop. In my program I cannot put anything above the first for loop but I can put anything below it, so I thought instead of doing the count every time maybe I could use a class to somehow maintain state and just increment the number as oppose to recalculating every time.

With classes:

public class LearningClassesCounter {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] list = new int[]{1,2,3,4,5,6,7,8,9,10};
        int[] data_list = new int[list.length];
        for (int current_location = 0; current_location<list.length;current_location++) {
            //can only put commands in here. Nothing above.
            Counter checker = new Counter(data_list);
            System.out.println(checker.check_data());
            for (int i =0; i<100; i++){
                data_list[current_location] = (list[current_location]+1);
            }
        }

        //its done now lets print the results
        for (Integer item : data_list) {
            System.out.println(item);
        }
    }
}


class Counter {
    private int count;             // current value
    private boolean continue_or_not;
    private int[] data_list;

    // create a new counter with the given parameters
    public Counter(int[] data_list) {
        data_list = this.data_list;
        count = 0;
        continue_or_not = true;
    } 

    public boolean check_data() {
        // TODO Auto-generated method stub
        int count = 0;
        for (int item : data_list) {
            if (item != 0) {
                count++;
                if (count>=3) {
                    break;
                }
            }
        }

        if (count>=3) {
            return false;
        } else {
            return true;
        }
    }

    // increment the counter by 1
    public void increment() {
        count++;
    } 

    // return the current count
    public int value() {
        return count;
    } 
}

This doesn’t work because it thinks the data_list is a null pointer(I know I’m declaring it null, but if I make it private int[] data_list = data_list it doesn’t compile either). My ultimate goal is to have some kind of controls, in this case its limiting it to 2 items but I want to also add other limits like total value of al items cannot exceed X or cannot be lower than X and want to save CPU power by not having to do full calculations every time. So I think I need to be able to increment the values and then need to check that those increments haven’t exceeded thresholds.

Can anyone help me understand what I’m doing wrong? Am I only wrong with syntax; or am I designing this wrong?

  • 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-06-02T11:42:05+00:00Added an answer on June 2, 2026 at 11:42 am

    //can only put commands in here. Nothing above.
    Counter checker = new Counter(data_list);
    System.out.println(checker.check_data());

    When you are calling checker.check_data(), its trying to parse through the data_list, but its empty. So, it throws a NullPointerException. The data_list is empty because inside your constructor, you may need to initialize like this this.data_list = data_list instead of data_list = this.data_list (here this.data_list has no reference so NULL)

    If you avoid that call, the output will be 2,3,4,5,6,7,8,9,10,11.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters

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.