0

This is my first post so if there are formatting issues, I'm sorry. So long story short, every time I compile this code in DrJava the interactions pane becomes unresponsive, but the program itself is fine.

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList; 
import java.util.Arrays;
import java.util.*;
class Main { 

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter words, enter STOP to stop the loop.");
    ArrayList <String>words = new ArrayList
      <String>();
    boolean end = false;
    while(end == false){
     String stop = "STOP";
     String word = "";
     for(int i = 0; Objects.equals(stop, word); i++){
       word = scan.nextLine();
       if(Objects.equals(stop, word) && i > 2){
         System.out.print(words.size());
         words.remove(0);
         words.remove(i);
         System.out.println(words);
         end = true;
       } else if(Objects.equals(stop, word) && i < 2){
         System.out.print(words.size());
         System.out.println(words);
         end = true;
       } else {
         words.add(i, word);
       }
       System.out.println();
    }
   }
  }
}

The objective of this code is: Write a Java program that allows a user to input words at the command line. Your program should stop accepting words when the user enters "STOP". Store the words in an ArrayList. The word STOP should not be stored in the list.

Next, print the size of the list, followed by the contents of the list.

Then, remove the first and last words stored in the list, but only if the list has a length greater than two. Finally, reprint the contents of the list.

This is a class assignment, so I am not trying to get the answer to the coding problem itself, but rather just the reason the interactions pane isn't responding. Thanks in advance!

  • May you please attach the output you get from the terminal? – MZ97 Feb 18 '20 at 14:02
  • So the interactions pane would be where you input information and information is outputed. You can see the interactions pane open on the bottom of each of these images [working](https://imgur.com/a/vi1QvO6) [not working](https://imgur.com/a/cmZuUKj) In this image, the cursor is not shown but it is showing up as a loading circle, it never stops loading – weirdguyben Feb 18 '20 at 14:04
  • @MZ97 That's the problem, no output is even given. – weirdguyben Feb 18 '20 at 14:09
  • Can you please try to remove this for-loop: for(int i = 0; Objects.equals(stop, word); i++) I dont see it neccesary – MZ97 Feb 18 '20 at 14:10
  • Why do you remove items from the words list? – NomadMaker Feb 18 '20 at 14:11
  • @NomadMaker That is just part of the assignment. If the length is greater than 2, you take out the first and last words in the array list – weirdguyben Feb 19 '20 at 14:21
  • @MZ97 I took out the for loop and the interactions pane now works, thanks! – weirdguyben Feb 19 '20 at 14:26

1 Answers1

1

Removing the for-loop: for(int i = 0; Objects.equals(stop, word); i++), as @MZ97 suggested, caused the interaction pane work as normal.