Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7994923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:15:29+00:00 2026-06-04T14:15:29+00:00

I am trying to create a program which will read a file and check

  • 0

I am trying to create a program which will read a file and check whether the text is a palindrome or not. The code compiles, but doesnt really work.

The problem is I dont know how to break a complete token into characters or assign it to a string in order to use string’s length to push(enqueue) each letter or digit into the stack(queue). Can anyone suggest a solution for this?

public static void main(String [] args) throws IOException{
    StackReferenceBased stack = new StackReferenceBased();
    QueueReferenceBased queue = new QueueReferenceBased();
    Scanner s = null;
    String fileName=args[0]+".txt";
    int symbols = 0;
    int lettersAndDigits =0;
    int matches = 0;

    try{
      s = new Scanner(new File(fileName));
      while(s.hasNext()){
        String current = s.next();
        for(int i=0;i<current.length();i++){
          char temp = s.next().charAt(i);
          if(Character.isLetterOrDigit(temp)){
            stack.push(temp);
            queue.enqueue(temp);
            lettersAndDigits++;

          }
          else {
            symbols++;

          }
        }
      }
      System.out.println("There are: " + " "+ symbols + " " +"symbols and " + " "+lettersAndDigits + " "+ "digits/letters");


    }
    catch (FileNotFoundException e) {
      System.out.println("Could not open the file:" + args[0]);
    } //catch (Exception e) {
      //System.out.println("ERROR copying file");
      finally {
      if(s != null){
        s.close();
      }
    }
    while (!stack.isEmpty()){
      if(!stack.pop().equals(queue.dequeue())){
          System.out.println("not pali");
          break;
        }
      else {
        ++matches;
      }
    }

    if(matches==lettersAndDigits){
      System.out.print("pali");
    }  
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:15:31+00:00Added an answer on June 4, 2026 at 2:15 pm

    Instead of

    char temp = s.next().charAt(i); 
    

    you need

    char temp = current.charAt(i); 
    

    By calling s.next() you read the next token from the file and try to access the ith element of that token based on the first string’s (current) length, which will lead to exceptions if the tokens read are shorter than the first stoken

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a program for Android which will read in text, and
I'm trying to create a program that takes a text file of c++ code
I am trying to create a program that will do some simple calculations, but
I am trying to create a program which will show me the status of
I'm trying to create a word sorting program that will read the words in
I'm trying to create a program which will visualize different sorting algorithms by drawing
Hey all I was trying to create a java program which will remain logged
I am trying to create a soundboard program which will play sounds using the
I am trying to create a program similar to Folder Lock which prevents users
I have been trying to create a simple program with Python which uses OpenCV

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.