2

I wanted to create a simple program for user to insert 3 strings to a private string array in a class and then print it back by creating a new object using object reference but I think I am facing problem in the setter/getter.(Pretty new to class and setter/getter) Here is what I have so far:

import java.util.Scanner;
public class Stringtest {
public static void main(String[] args)
{   Scanner input=new Scanner(System.in);


   Stringer Strung=new Stringer();

    System.out.println("Strings:"+Strung.print());


}
}
class Stringer
{   Scanner input=new Scanner(System.in);
    private String[] aa=new String[3];
    aa[0]="zero";
    aa[1]="one";
    aa[2]="two";

    Stringer()
    {}

{  System.out.println("Please enter 3 strings:");

    for(int i=0;i<4;i++)
    {
        aa[i]=input.next();
    }
}

public  void setaa(String[] a)
{
    aa=a;
}

public String[] getaa()
{
    return aa;
}

public void print(String[] a)
{
    for(int b=0;b<4;b++)
    {
        System.out.printf("%s",a[b]);
    }
}

}
3
  • What is your expected output? What is your observed outout. Also, you use an initializer block. I would suggest moving the logic from the initializer block to the constructor. Furhtermore, in the initializer and the print()-method, the for loops from 0 to 3 (both inclusive), but the array only has a length of 3 (indices 0 to 2), so this will cause an ArrayIndexOutOfBoundsException. --- While you have written a getter and a setter for aa (which I suggest to be renamed for clarity), but you do not use them. Commented Oct 5, 2019 at 9:35
  • and a hint: if you want to loop over all elements of an array, normally for (int i = 0; i < aa.length; i++) is used - so if the size of he array is changed, the code still works Commented Oct 5, 2019 at 9:53
  • Ah,sorry I think I forgot to initialize the string array to a few strings.I wanted to have the setter and getter so user input can replace the initialized private string array by creating a new object class Commented Oct 5, 2019 at 11:25

1 Answer 1

1
  1. Due to populating the array while creating a class instance, you don't require any setters. The only getter requires.

  2. Divide the logic from the runner.

  3. Always use array.length() while looping or use a simple for loop otherwise you'll be getting an indexOfBoudException error.

  4. Didn't get why you are using printf() while printing results.

    My solution:

    import java.util.Scanner;
    
    public class App {
        public static void main(String[] args) {
            App.run();
        }
    
        private static void run() {
            Stringer stringer = new Stringer();
            stringer.print(stringer.getStrings());
        }
    }
    
    class Stringer {
    
        private String[] strings = new String[3];
    
        Stringer() {
            System.out.println("Please enter 3 strings:");
            for (int i = 0; i < 4; i++) {
                Scanner scanner = new Scanner(System.in);
                strings[i] = scanner.next();
            }
        }
    
        String[] getStrings() {
            return strings;
        }
    
        void print(String[] strings) {
            System.out.println("Strings are:");
            for (String string : strings) {
                System.out.println(string);
            }
        }
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

I forgot to initialize the array but I wanted to use setter/getter for when creating a new object class I can reference to the class to access and modify the private string array
According to your code and the purpose you’ve declared setter is unnecessary.There’s the code convention: don’t have unused classes / methods / variables ‘just in case’.

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.