1

I am trying to write a program that reads words from the user until they enter the word “quit”, at which point it prints out all the words they entered on one line separated by commas.

I previously got the last user input (quit) to be only outputted, but now I am lost. Help. There is some commented out code. Thanks. I am new to Java but know C/C++ and UE4.

public static void main(String[] args) {
    java.util.Scanner scanner = new java.util.Scanner(System.in);
    String[] array = new String[1000];

    for (int i=1; i>0; i++) {
        System.out.print("Enter string: ");
        array[i] = scanner.nextLine();

        if (array.equals("quit")) {
            System.out.println(array);
            break;
        }
     }
}
5
  • 2
    you need array[i].equals("quit") and loop is faulty Commented May 26, 2017 at 16:53
  • if you want the loop to run until the user enters quit, it's better to use a while loop rather than making an infinite for loop. Commented May 26, 2017 at 16:56
  • You might want to use System.out.println(Arrays.toString(array)) instead. Commented May 26, 2017 at 16:58
  • Also, if you want an infinite for loop, it's un-necessary to increment i when it's not needed. Simply, omit the third part like so for(int i = 1; i>0;) {...} Commented May 26, 2017 at 17:02
  • @Aominè Just as a note, this loop isn't infinite. It ends when i overflows. Commented May 27, 2017 at 10:48

4 Answers 4

4
  1. I would advice to use collection ArrayList instead of regular array. So you don't need to worry about size.
  2. Make an infinite loop
  3. Constants always should be on the left side of comparison. In this particular case line is not nullable, but this is good practice at all.

Eventually the code will look like this:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    List<String> buffer = new ArrayList<>();
    while (true) {
        System.out.print("Enter string: ");
        String line = scanner.nextLine();
        if ("quit".equals(line)) {
            break;
        }
        buffer.add(line);
    }
    System.out.println(buffer);
}

Hope it helps!

P.S. This will not print "quit" at the end, if you want to get it printed move buffer.add(line); before if statement.

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

1 Comment

a do while loop might be slightly nicer. He also asked for the strings to be printed separated by commas, otherwise great answer
1
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    Scanner keyboard= new Scanner(System.in);
    String word=null;
    ArrayList<String> words= new ArrayList<String>();
    do{
        System.out.print("Type your word: ");
        word=keyboard.next();
        if(word.equalsIgnoreCase("Quit")) break;
        words.add(word);
    }while(true);


    System.out.println("You typed: ");
    for(String w: words){
        System.out.print(w+",");
    }

}

}

You dont know the number of loops so you need "while" not "for". if user type quit it stops. If not, you add this word into the ArrayList. In the end, you traverse this ArrayList and print all the words with a comma.

1 Comment

This will add a comma to the end of the list. You might want to try the StringJoiner class of java8
0

You forget to show an index of the array and also you need to change the withdrawing:

  if (array[i].equals("quit")) {
      System.out.println(Arrays.toString(array))
      break;
  }

1 Comment

You might want to use System.out.println(Arrays.toString(array)) instead.
0

I would recommend that you use a class named TextIO which can be found here. This class has the methods that will make your problem easier. First of all, an introduction:

TextIO is a class that works on control pointer which is moved ahead immediately after TextIO reads some text. E.g. if the user enters the string:

Hello World

then the default pointer position will be start of the text. If you run the statement:

String str = TextIO.getWord();

str will contain the string Hello and now the pointer position will be right in front of Hello. Now again if you call some other methods, TextIO will return the output starting from the current pointer position.


Coming to your problem now, TextIO contains a method peek() which returns the character that is just ahead the pointer position without changing the pointer position. This is useful if you want to pre-determine the next character in your text(the desired character being a comma in your case). This is how you would do the coding:

ArrayList<String> array = new ArrayList<String>();
while(true){
    TextIO.skipWhiteSpace();//Skips any white space(blacks, carriage returns etc. ) that may be preceding your next word.
    String str = TextIO.getWord();
    if (str.equalsIgnoreCase("quit")){
        break;}
    if(TextIO.peek() != ','){
        ...Show error message...
        break;
    }
    TextIO.getChar();//Skip the comma that is the next character
    array.add(str);
}

I hope that helps!

Comments

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.