0

Im stuck on this, I need a code that use 2 nested loops for this assignment (there are other solutions, but I need to demonstrate my understanding of nested loops). But I just dont get it. The outer loop repeats the entire algorithm and the inner loop iterates half-way (or less) through the string. I am not sure on what I need to put inside the for loops. This is what I have so far. Any Assistance would be pleasured.

import java.util.Scanner;
public class pali 
{
public static void main(String[] args) 
{
    String line;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a String to check if it's a Palindrome");
    line = input.nextLine();

    String x = 0;
    String y = input.length-1;

    for (String i = 0; i < line.length-1; i ++){
        for (String j = 0; j < line.length-1; j ++){

            if (input.charAt(x) == input.charAt(y)) 
            {
                x++;
                y--;
            }      
        }
     }
 }

Example Output:

Enter a string: 1331

1331 is a palindrome.

Enter a string: racecar

racecar is a palindrome.

Enter a string: blue

blue is NOT a palindrome.

Enter a string:

Empty line read - Goodbye!
azro
  • 53,056
  • 7
  • 34
  • 70
  • Palindrome check is not the better problem to show you understand nested loop, so much easier with simple loo^p – azro Dec 22 '18 at 20:28
  • Thank you, Do you know anyway to do it without a single loop? I kinda need help. – Geronimo Stiliton Dec 22 '18 at 20:31
  • There is dozens on StackOverflow, just search. https://stackoverflow.com/questions/4138827/check-string-for-palindrome – azro Dec 22 '18 at 20:32
  • Possible duplicate of [Check string for palindrome](https://stackoverflow.com/questions/4138827/check-string-for-palindrome) – S-Man Dec 23 '18 at 00:05

3 Answers3

1

Your algorithm is flawed, your nested loop should be to prompt for input - not to check if the input is a palindrome (that requires one loop itself). Also, x and y appear to be used as int(s) - but you've declared them as String (and you don't actually need them). First, a palindrome check should compare characters offset from the index at the beginning and end of an input up to half way (since the offsets then cross). Next, an infinite loop is easy to read, and easy to terminate given empty input. Something like,

Scanner input = new Scanner(System.in);
while (true) {
    System.out.print("Enter a string: ");
    System.out.flush();
    String line = input.nextLine();
    if (line.isEmpty()) {
        break;
    }
    boolean isPalindrome = true;
    for (int i = 0; i * 2 < line.length(); i++) {
        if (line.charAt(i) != line.charAt(line.length() - i - 1)) {
            isPalindrome = false;
            break;
        }
    }
    if (isPalindrome) {
        System.out.printf("%s is a palindrome.%n", line);
    } else {
        System.out.printf("%s is NOT a palindrome.%n", line);
    }
}
System.out.println("Empty line read - Goodbye!");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Your code had lot of errors. I have corrected them and used a while loop to check if its a palindrome or not. Please refer below code,

import java.util.Scanner;

public class Post {
    public static void main(String[] args) {
        String line;
        boolean isPalindrome = true;
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("Enter a String to check if it's a Palindrome");
            line = input.nextLine();



            int x = 0;
            int y = line.length() - 1;

            while (y > x) {
                if (line.charAt(x++) != line.charAt(y--)) {
                    isPalindrome = false;
                    break;
                }
            }
            if (isPalindrome) {
                System.out.println(line + " is a palindrome");
            } else {
                System.out.println(line + "is NOT a palindrome");
            }
            System.out.println();
        }
    }
}
Sandeepa
  • 3,457
  • 5
  • 25
  • 41
0
        import java.util.Scanner;
public class pali 
{
public static void main(String[] args) 
{
    String line;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a String to check if it's a Palindrome");
    line = input.nextLine();

    String reversedText ="";
    for(int i=line.length()-1/* takes index into account */;i>=0;i++) {
        reversedText+=line.split("")[i]; //adds the character to reversedText
    }

    if(reversedText ==line){
        //is a palidrome
    }

 }
Danny Piper
  • 73
  • 1
  • 1
  • 6