mhridoy's blog

By mhridoy, history, 5 years ago, In English

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;
        }

    }
}


Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it

By mhridoy, history, 5 years ago, In English

Remove the minimum number of edges from the following directed graph so that there exists a topological order. Draw the new graph and find the topological order using a DFS.

No. of Nodes: 9 No. of Edges: 14

Edge List: 1-4, 1-5, 2-5, 3-6, 4-9, 5-1, 5-7, 6-5, 7-4, 8-5, 9-3, 9-6, 9-7, 9-8

How can I Draw the graph?

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it