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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:05:37+00:00 2026-05-18T09:05:37+00:00

I’m pretty new to Java and this has me really confused. I have two

  • 0

I’m pretty new to Java and this has me really confused.

I have two classes. One (controlNumber) has two int fields used for numbers, one is the current number and one is the max number (once the current number reaches this it is reset to 1). The constructor initializes the current number to 1 and takes the max as a parameter (and checks that the max is positive and less than 100).
Of methods I have one that returns the current number, one that returns the max number, one that sets the max as the current, one that adds 1 to the current number (working its was towards the max) and lastly one that returns a String with the current number where it puts “0” in front if the current number is only one digit (so 5 becomes 05).
The toString looks like this:

public String toString()
    {
        if (currentNumber > 9) {
            return Integer.toString(currentNumber);
        }
        else {
            return ("0" + Integer.toString(currentNumber));
        }
    }

This class works just fine.
The second class is where I start running into problems. It has two fields which are of the class above. The constructor initializes them with two different max values. All this seems to be working.
Now I need a toString method which’ll return a String with the current value of both fields, which a “/” between and “0” in front if it’s one digit (so if day is 3 and month is 11 it’ll return “03/11”). My first thought was just to use something like what I used above with Integer.toString, but it turns out that you can’t do that (even though the fields in the controlNumber class are ints). So then I thought of using my method from above (which is in a different class) as it does most of what I want, but I don’t know how.

I hope I’ve made sense and that someone can help me figure out how to solve my problem.

  • 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-18T09:05:37+00:00Added an answer on May 18, 2026 at 9:05 am

    If you have two fields of the type above, called a and b

    public String toString() {
        return a + "/" + b;
    }
    

    You don’t need to call toString() explicitly as that is the behaviour anyway. The only difference is that if a or b is null, this will print “null” whereas calling toString() will throw a NullPointerException.

    BTW You can shorten your method to

    public String toString() {
        return (currentNumber > 9 ? "" : "0") + currentNumber;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.