Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CONVERTING C++ TO JAVA I ALMOST HAVE IT. I Need help. I\'m having a hard time wi

ID: 641724 • Letter: C

Question

CONVERTING C++ TO JAVA

I ALMOST HAVE IT. I Need help. I'm having a hard time with the scanner class and inputs. Please help!

CODE:

public class Palindrome
{
private static int Main()
{
byte ch; //Variable used to hold an input character.
byte ans; //Variable used in the dialog: Want to examine another string? (
boolean good;
int i;
Scanner sc = new Scanner(System.in);

do //Beginning of the do-while loop.
{
java.util.Stack<Byte> s = new java.util.Stack<Byte>();
java.util.LinkedList<Byte> q = new java.util.LinkedList<Byte>();
//Declaring s and q here ensures that the stack s and queue q
//are necessarily empty at the the beginning of each iteration.
System.out.print("Please enter a string of characters: ");
cin.get(ch);//NEED HELP HERE
while (ch != ' ') //Read the input string one character at a time.
{
s.push(ch);
q.offer(ch);
//A copy of ch goes at the top of the stack. At the same
// time, a copy of ch is added at the end of the queue.

} // end of while
good = true;
i = 1;
while (!s.empty())
{ // Repeatedly compare the top element of the stack
//and the front element of the queue
if (s.peek() == q.peek())
{
s.pop();
q.poll();
i++;
}
else
{
good = false;
break;
}
} // end of while
if (good)
{
System.out.print(" ");
System.out.print("It is a palindrome.");
}
else
{
System.out.print(" ");
System.out.print("It is not a palindrome, since");
System.out.print(" ");
System.out.print("the symbol at position ");
System.out.print(i);
System.out.print(" from the left is different from ");
System.out.print(" ");
System.out.print("the symbol at position ");
System.out.print(i);
System.out.print(" from the right.");
}
System.out.print(" ");
System.out.print(" ");
System.out.print("Want to examine another string? (y/n): ");
cin >> ans;//HOW DO I DO THIS? PLEASE HELP AND EXPLAIN SCANNER CLASS
cin.ignore(100, ' '); //Ignore the newline character.
while (ans != 'n' && ans != 'N' && ans != 'y' && ans != 'Y')
{ //Force the user to input n or N or y or Y.
System.out.print("Please enter n or N or y or Y: ");
cin >> ans;
cin.ignore(100, ' '); //Ignore the newline character.// NEED HELP HERE
} //end of while
} while (ans == 'y' || ans == 'Y'); // end of do-while.
System.out.print(" ");
System.out.print("Bye!");
System.out.print(" ");
System.out.print(" ");
return 0;// Is this right?
}
  
}

Explanation / Answer

//Java program that reads a string and prints if it is palindrome or not

//Palindrome.java

import java.util.Scanner;

public class Palindrome
{
  
   //It should be public static void main(String[] args)
   public static void main(String[] args)
   {
       char ch; //Variable used to hold an input character.
       char ans; //Variable used in the dialog: Want to examine another string? (
       boolean good;
      
       //set index to zero
       int index=0;
       //Declare a string to read input from the user
       String userInput;
       Scanner sc = new Scanner(System.in);

       do //Beginning of the do-while loop.
       {
           java.util.Stack<Character> s = new java.util.Stack<Character>();
           java.util.LinkedList<Character> q = new java.util.LinkedList<Character>();
           //Declaring s and q here ensures that the stack s and queue q
           //are necessarily empty at the the beginning of each iteration.
           System.out.print("Please enter a string of characters: ");
           //read input
           userInput=sc.next();


           //push elements into the stack and linked list
           while (index<userInput.length()) //Read the input string one character at a time.
           {

               ch=userInput.charAt(index);
               //cin.get(ch);//NEED HELP HERE
               s.push(ch);
               q.offer(ch);
               //A copy of ch goes at the top of the stack. At the same
               // time, a copy of ch is added at the end of the queue.
               index++;

           } // end of while
           good = true;
           int pos = 1;
           while (!s.empty())
           { // Repeatedly compare the top element of the stack
               //and the front element of the queue
               if (s.peek() == q.peek())
               {
                   s.pop();
                   q.poll();
                   pos++;
               }
               else
               {
                   good = false;
                   break;
               }
           } // end of while
           if (good)
           {
               System.out.print(" ");
               System.out.print("It is a palindrome.");
           }
           else
           {
               System.out.print(" ");
               System.out.print("It is not a palindrome, since");
               System.out.print(" ");
               System.out.print("the symbol at position ");
               System.out.print(pos);
               System.out.print(" from the left is different from ");
               System.out.print(" ");
               System.out.print("the symbol at position ");
               System.out.print(pos);
               System.out.print(" from the right.");
           }
           System.out.print(" ");
           System.out.print(" ");
           System.out.print("Want to examine another string? (y/n): ");
           ans=sc.next().charAt(0);

           //If user eneter y to continue
           //then reset all index to zero and good to true
           //and pos to 1
           if(ans=='y' || ans=='Y')
           {
               index=0;
               good=true;
               pos=1;
           }

       } while (ans == 'y' || ans == 'Y'); // end of do-while.

       System.out.print(" ");
       System.out.print("Bye!");
       System.out.print(" ");
       System.out.print(" ");

   }

}

------------------------------------------------------------------------------------------------------

Sample output:

Please enter a string of characters: lirirl

It is not a palindrome, since
the symbol at position 2 from the left is different from
the symbol at position 2 from the right.

Want to examine another string? (y/n): y
Please enter a string of characters: liril

It is a palindrome.

Want to examine another string? (y/n): y
Please enter a string of characters: madam

It is a palindrome.

Want to examine another string? (y/n): y
Please enter a string of characters: five

It is not a palindrome, since
the symbol at position 1 from the left is different from
the symbol at position 1 from the right.

Want to examine another string? (y/n): n

Bye!

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote