0

How can i possibly do this without knowing the size of the array beforehand? I'm totally confused. I've been trying to do this by splitting the array and what not but it's supposed to do this by using only loops. Here's different ways I've tried to do it but it doesn't stop taking user input even when it encounters a *.

1st version:

import java.util.*;
public class Number11 {

public static void main(String[] args) {


    String line="";
    Scanner input = new Scanner(System.in);

    System.out.println("You are asked to enter a character one by one and terminate the list with *");

    System.out.print("Please enter a character or end it with * : ");
    line = input.next();
    while(!(line.equals("*")))
    {
        System.out.print("Please enter a character or end it with * : ");
        line = input.next();
        if(line.equals("*"))
        {
        System.exit(0);
        int length = line.length();
        String [] sequence = new String[length-1];
        for(int i=0; i<length; i++)
        {
            sequence[i] = line;
        }
        break;
        }
    }

 }

}

2nd:

import java.util.*;
public class Number11 {

public static void main(String[] args) {

    String [] character = new String[20];

    Scanner input = new Scanner(System.in);

    for(int i=0; i < character.length; i++)
    {
        System.out.print("Enter a character or press * to stop: ");
        character[i] = input.next();

       while(!(character[i].equals("*")))
        {
            System.out.print("Enter another character or press * to stop: ");
            character[i] = input.next();
        }

    }
 }

}

Your help will be much appreciated. Thanks.

Manisha Singh Sanoo
  • 919
  • 1
  • 13
  • 42
  • Nope it can't be a duplicate because i can't find my answer in the other question. It's not what i'm searching for, like at all. – Manisha Singh Sanoo Jan 11 '15 at 09:23
  • Read the answer and compare it to your code. Note also that calling something `character` doesn't make it just a character; it's still a string. – chrylis -cautiouslyoptimistic- Jan 11 '15 at 09:25
  • `How can i possibly do this without knowing the size of the array beforehand` how this question become duplicate of `http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java` – Saif Jan 11 '15 at 09:28
  • Exactly @Saif. My question is based on arrays. I'm not trying to compare strings here. My concern is not with strings but using loop and arrays without knowing the size of the array. I've been through the other question, still can't find an answer to mine. – Manisha Singh Sanoo Jan 11 '15 at 09:32
  • The main issue in your code is that you're comparing Strings incorrectly, which would be a huge solution to your problem in general. The further answer to your question would be to not use an array; use a data structure that can dynamically grow like a `List`, or use a `StringBuilder`. – Makoto Jan 11 '15 at 09:32
  • 1
    I'm not seeing it as a pure duplicate either, so I'll go ahead and cast a reopen vote. I *strongly* encourage you to fix your code *before* I cast the vote though so that you're using `.equals` instead of `==`, since the other way isn't going to work. Ping me when you've done so (hopefully within the next 20 minutes). – Makoto Jan 11 '15 at 09:34
  • Just forget the 1st version guys the main problem is with the 2nd one as i'm trying to do this as simply as possible. I'll edit it right away. – Manisha Singh Sanoo Jan 11 '15 at 09:35
  • It's still using `==` and not `.equals`, though. That's the main reason it was closed as a dupe. – Makoto Jan 11 '15 at 09:42
  • @MarounMaroun I've fixed my mistakes concerning the == but my question was not concerning strings but rather about loops and arrays. I'm searching for a simple way to do this. – Manisha Singh Sanoo Jan 11 '15 at 09:57

2 Answers2

0

Use Collection frameworks. Study them a little here: An example using ArrayList given below

public static void main(String[] args) {

    ArrayList<String>  character = new ArrayList<String>();

     Scanner input = new Scanner(System.in);
     String temp;
     while(true)
     {
         System.out.print("Enter a character or press * to stop: ");
         temp=input.next();
         character.add(temp);

         if(temp.equals("*"))
         {
            break;
         }

     }
  }

And also you don't need two loops if you use them perfectly .

Saif
  • 6,804
  • 8
  • 40
  • 61
  • Hey thanks for your help. I've tried using ArrayList but in class we haven't yet studied it so i figured out it could be done simply by using loops. – Manisha Singh Sanoo Jan 11 '15 at 09:55
0

You can achieve this using loop like this:

public static void main(String[] args) {

    String line="";
    char ch = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("You are asked to enter a character one by one and terminate the list with *");
    System.out.print("Please enter a character and Press Enter key or end it with * : ");
    while(ch!='*')
    {
        ch=input.next().charAt(0);
        if(ch=='*')
        {
            System.out.print(line);
            break;
        }
        else
            line+=ch;
    }

}
Umair
  • 81
  • 1
  • 1
  • 8