public class StringEntry {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// initialize and ArrayList
ArrayList<String> stringList = new ArrayList<String>();
while (!reader.readLine().equals("end")); // <--------------------THIS IS WHERE THE ERROR IS OCCURING
{
stringList.add(reader.readLine());
}
for (int i = 0; i < stringList.size(); i++)
{
System.out.println(stringList.get(i));
}
//write your code here
}
}
This code sort of works. In IntelliJ it allows the code to execute but when printing out the string it skips every other word and you need to enter end twice to end the program.
I also got this error when I try it on the website
java.lang.NullPointerException at com.codegym.task.task07.task0722.Solution.main(Solution.java:20)
package com.codegym.task.task07.task0722;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
The end
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// initialize and ArrayList
ArrayList<String> stringList = new ArrayList<String>();
String end = "end";
while (!reader.readLine().equals(end))
{
stringList.add(reader.readLine());
}
for (int i = 0; i < stringList.size(); i++)
{
System.out.println(stringList.get(i));
}
//write your code here
}
}