0

I want to make a program that keeps getting string input and only stops when "." is entered, any ideas how to make it?

I understand that I need to make a string array, but what's the length I'm gonna give it if I dont know how many strings the user will enter?

This is my first time to use this website, so excuse me for any mistakes.

Thank you.

Edit: Here's a code that is confusing me. I keep entering dots but the for loop never breaks. Also the length is currently 10, how can I make it unlimited until the input is a dot?

Scanner s=new Scanner(System.in);
    String[] x = new String[10];
    for(int i=0;i<10;i++)
    {   
        x[i]=s.next();
        if(x[i]==".")
            break;
    }
xenteros
  • 15,586
  • 12
  • 56
  • 91
Heshamy
  • 69
  • 8
  • Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Aug 28 '16 at 12:22
  • *"I understand that I need to make a string array, but what's the length I'm gonna give it if I dont know how many strings the user will enter?"* Well, it's not at all clear to me you need an array, but the good news is that if you do, JavaScript's standard arrays don't need to have a predefined length, they can grow. (This is because they [aren't really arrays at all](http://blog.niftysnippets.org/2011/01/myth-of-arrays.html) *[disclosure: that's a post on my anemic little blog]*.) – T.J. Crowder Aug 28 '16 at 12:24
  • I have posted a code below, please have a look at it. – Heshamy Aug 28 '16 at 14:21
  • Please update your question instead of dumping the code in the comment section. – Ram Aug 28 '16 at 15:32

1 Answers1

1
Scanner s=new Scanner(System.in);
ArrayList<String> inputs = new ArrayList<>();
while (true) {
    inputs.add(s.next());
    if(inputs.get(inputs.size().equals("."))
        break;
}

Remember to import ArrayList. Check the documentation for more information about ArrayList. In java == is an operator used for comparing references. your new String won't have the same reference as "." which will be created at compilation time.

equals() method is used for comparing if the objects equal. In case of String it compares char by char to see if they are all the same. If yes, it returns true. False otherwise.

Remember to always @Override the public boolean equals() in your class. You have to decide what it means that to object of a certain class are equal and implement it. it'll be useful if you read this topic. It's widely described there.

Community
  • 1
  • 1
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • Avoid answering obvious duplicates like this question. Flag as dupe instead. – Tom Aug 31 '16 at 11:05
  • 1
    @Tom flagged. I've found this topic tagged as `javascript` just a moment before with js answer attached. It would be a pity if it get locked with just a javeascript answer. – xenteros Aug 31 '16 at 11:06