This is a code of checking palindrome. I want to input End of File & also using input string as a full sentence. But When I run this programme it shows java.lang.NullPointerException. I don't know why. How can avoid this error? Thanks In Advance.
My Code:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Twinkle Twinkle Little Star
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
MotherBear solver = new MotherBear();
solver.solve(1, in, out);
out.close();
}
static class MotherBear {
public void solve(int testNumber, InputReader in, PrintWriter out) {
String line = null;
while (in.hasNext()) {
line = in.nextLine().toLowerCase();
// out.println(line);
String temp[] = line.split(" ");
String temporary = "";
for (int i = 0; i < temp.length; i++) {
temporary += temp[i];
}
out.println(temporary);
if (palindrome(temporary))
out.println("You won’t be eaten!");
else
out.println("Uh oh..");
}
}
private boolean palindrome(String text) {
String temp = "";
for (int i = text.length() - 1; i >= 0; i--) {
temp += text.charAt(i);
}
if (temp.equals(text))
return true;
else
return false;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return false;
}
tokenizer = new StringTokenizer(line);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return true;
}
}
}