2

This is what I have so far. What this program basically does is take user input(their social security numbers), verifies that their input is in the proper format (XXX-XX-XXXX), adds the verified input to the arrays list. Now the final step is to create a toString() method that will return a human readable String representation of all social security numbers added to the array. This method must use a for loop, and must print 1 social security number entry per line. For example:

"The Social Security Numbers you entered are:
345-43-5674
456-62-8435
910-70-0053"

package SSNServerStorage;

public class SSNArray
{
    private String[] SSNNumber;
    private int arrayCount;

    public SSNArray(){
        SSNNumber = new String[300];
        arrayCount = 0;
    }

    public SSNArray(int arraySize){
        SSNNumber = new String[arraySize];
        arrayCount = 0;
    }

    public String[] getSSNNumber(){
        return SSNNumber;
    }

    public int getArrayCount(){
        return arrayCount;
    }

    public boolean validateSSNNumber(String SSNFormat){
        return SSNFormat.matches("\\d{3}-\\d{2}-\\d{4}");
    }

    public String addSSN(String SSNFormat){
        if (validateSSNNumber(SSNFormat)){
            return SSNNumber[arrayCount++] = SSNFormat;
        }else{
            return null;
        }
    }

    public String toString(){
        System.out.println("The Social Security Numbers you entered are:");

        for()
        {
            //
            //
            //
            //
        }
    }
}
5
  • Put your for loop inside the test class Commented Sep 25, 2015 at 17:05
  • 1
    Try to use StringBuilder Commented Sep 25, 2015 at 17:06
  • 1
    Have you tried anything or is this just a request to write code for you? You should not print the output in the toString method. That method should be used to create and return the String representation of an object, not print it. Commented Sep 25, 2015 at 17:08
  • possible duplicate of Java output formatting for Strings Commented Sep 25, 2015 at 17:09
  • @fabian I think the question is just misusing the word "print", but a clarification from OP might be good. Commented Sep 25, 2015 at 17:10

4 Answers 4

4

Your toString method doesn't have correct signature, it should return String. This should do what you need:

@Override
public String toString(){
    StringBuilder sb = new StringBuilder("The Social Security Numbers you entered are:\n");

    for (int i = 0; i < arrayCount; i++) {
        sb.append(SSNNumber[i]).append("\n");
    }

    return sb.toString();
}

Also remember to use the @Override annotation.

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

1 Comment

@Andreas you're right, thanks for heads up - didn't notice that upon first reading. Answer corrected.
2

You can declare a String to concatenate all the SSNumbers.

@Override
public String toString(){
    String str = "The Social Security Numbers you entered are:\n";
    for(int x=0; x< arrayCount; x++)
        str += SSNumber[x] + "\n";
    return str;
}

Since it is a toString() method, you will want to return the string representation of the class instead of doing println().

5 Comments

Never use String += String in a loop. Use StringBuilder.
@Andreas This looks like a school assignment, according to my knowledge in universities and colleges, they don't expect you to use StringBuilder. String is suffice for them.
@Andreas Thanks dude, changed.
You may have a point on "school assignment", it's just very bad form, and hits a nerve. ;-)
You got my up-vote. Add \n after :, and it's more common to use i as index variable.
0

Just concatenate :

@Override
public String toString(){
String s = "The Social Security Numbers you entered are:\n";
    for(int i =0; i < SSNNumber.length; i ++)
        s +=SSNNumber[i]+"\n";
    return s;
}

3 Comments

Never use String += String in a loop. Use StringBuilder.
Also it looks like not every element in the array needs to contain a String.
Loop should be limited to arrayCount.
0
StringBuilder strbul = new StringBuilder()
    for(int i=0;i<SSNNumber.length;i++)
            {
                strbul.append(SSNNumber[i]).append("\n");
            }
return strbul.toString();

1 Comment

Loop should be limited to arrayCount.

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.