Loops and stacks, palindrome homework assignment

I am working on a task for my programming class, but I ran into some difficulties and I'm not sure where else to look. Basically the question asks that we are writing a program that checks for palindromes.

  • The user enters text (non-alphanumeric characters are not allowed).
  • The string is pushed one character at a time on the stack
  • Characters are pulled along one of the stack, thereby changing the line
  • If the original matches the opposite, we have a palindrome

I'm having problems with my loops and I don’t know where to go from here, does anyone have any tips or pointers? What am I doing wrong?

Here is what I still have.

import java.util.Stack; import java.util.regex.*; import javax.swing.*; public class Question1 { static Stack PDrome = new Stack(); public static String Reverse (String input) { String reverse; if (input.length() <= 1) { return input; } //pushing onto the stack for (int i=0; i<input.length();i++) { PDrome.push(input.charAt(i)); } //popping from the stack into the string for (int i=0; i<input.length(); i++) { PDrome.pop()=reverse.charAt(i); } return reverse; } //Illegal char check method public static boolean checker (String input) { Pattern p = Pattern.compile("[^a-z0-9]", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(input); boolean b = m.find(); if (b) { System.out.println("There is a special character in your string"); System.exit(0); } return b; } //Main public static void main (String [] args) { //input String input = JOptionPane.showInputDialog("Enter text to check if it a palndrome"); //error case if (input==null); { System.out.println("Nothing Entered"); System.exit(0); } //checking for illegal chars checker(input); } } 
+4
source share
4 answers

This part:

 String reverse; ... //popping from the stack into the string for (int i=0; i<input.length(); i++) { PDrome.pop()=reverse.charAt(i); } 

should look like this:

 String reverse = ""; ... //popping from the stack into the string for (int i=0; i<input.length(); i++) { // appends the popped character to reverse reverse += PDrome.pop(); } 

note that when adding a large number of rows, this is not the best way to do this, since the Java row is immutable, and to add the row multiple times you will need to create a new row each time. This problem is small enough that it really doesn't matter, but when the problem is big, you'll want to use StringBuffer / StringBuilder.

+3
source

PDrome.pop()=reverse.charAt(i); wrong.

  • reverse is null -> NullPointerException
  • Do you assign a value to a function? (Pop ())

You have to create the back of pop'ping from the stack.

So you should start with an empty line: reverse = ""; and add the characters taken from the stack:

 while (!PDrome.isEmpty()) reverse += PDrome.pop(); 

Naming more

Please use capital letters to start fields and method names:

 "someIntegerVariable" "methodForCalculation" 

and only uppercase letters to start class names and interfaces:

 Stack ArrayList MyClass 

:)

(from Java conventions)

+2
source

What are you really doing here?

 PDrome.pop()=reverse.charAt(i); 

You should use PDrome.pop() to extract one char at a time and add it to reverse .

+2
source

This is a much cleaner way to write this in my opinion. This is a recursive approach.

 bool isPalindrome(String s) { if(s.length() <= 1) return true; return s[0] == s[s.length() - 1] && isPalindrome(s.substr(1, s.length() - 2); } 

This is much less than you can see.

+1
source

Source: https://habr.com/ru/post/1392956/


All Articles