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

  • Home
  • SEARCH
  • 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 6752539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:03:24+00:00 2026-05-26T13:03:24+00:00

This is our code: import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;

  • 0

This is our code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class reverse {

public static void main(String[] args) throws FileNotFoundException {
    File fil = new File("textFile.txt");
    Scanner scan = new Scanner(fil);

    while (scan.hasNext()) {
        String in = scan.nextLine();
        in = new StringBuffer(in).reverse().toString();
        Pattern replace = Pattern.compile("\\W+");
        Matcher matcher = replace.matcher(in);
        System.out.println(matcher.replaceAll("\t"));
        }



    }

}

In our textFile.txt we have letters, numbers and words in different lines. We want print them out in reverse order. So if one line is: Yes, 4 Chocolate milk. We want to print out: milk Chocolate 4 Yes.

Our code prints out words in reverse, so yes becomes sey. We don’t want it to be like this, but don’t know how to change it. We’ve been thinking about creating a stack, but we don’t how it’ll work together with out Pattern and Matcher stuff.

  • 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-05-26T13:03:25+00:00Added an answer on May 26, 2026 at 1:03 pm

    You shouldn’t reverse the line you read but split the line into a collection and reverse that.
    What you do is reversing the characters, so you get this:

    Yes, 4 Chocolate milk -> klim etalocohC 4 , seY
    

    If you split the line into ["Yes","4","Chocolate milk"] you can reverse that array/list.

    The problem is: I guess you want to keep Chocolate milk in that order, so you’d need to define what words belong together. If your input is always <words>, <number> <words> you could split on , first, to separate the first <words> and then on the first whitespace of the remainder in order to split the number and the second <words>.

    Update: try this:

    String input = "Yes sir, 4 Chocolate milk";
    
    //Pattern is: 
    //- one or more words separated by whitespace as group 1: ((?:\\w+\\s*)+)
    //- a comma 
    //- optional whitespace: \\s*
    //- an integer number as group 2: (\\d+)
    //- optional whitespace \\s*
    //- an arbitrary rest (which might contain anything) as group 3: (.*)
    Pattern p = Pattern.compile( "((?:\\w+\\s*)+),\\s*(\\d+)\\s*(.*)" );
    Matcher m = p.matcher( input );
    
    List<String> list = new ArrayList<String>();
    while(m.find()) {
      list.add( m.group( 1 ) );
      list.add( m.group( 2 ) );
      list.add( m.group( 3 ) );
    }
    
    Collections.reverse( list );
    for( String s : list) {
      System.out.println(s);
    }
    

    The output should be

    Chocolate milk
    4
    Yes sir
    

    Note that this depends on the layout of your input and is just meant to provide a start. You’d also need to integrate that with your other pattern matching logic.

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

Sidebar

Related Questions

Consider this code: import java.util.regex.*; public class Pattern3 { /** * @param args the
Below is the partial code. import java.io.*; import java.util.*; public class inputting { public
In our code we have quite a few cases of this pattern: class outerClass
I'm writing a C code for a class. This class requires that our code
In our code we used to have something like this: *(controller->bigstruct) = ( struct
We have this anonymous function in our code, which is part of the jQuery's
Maybe this applied to other Delphi's (I've only used 7). We've got our code
i'm wondering about this possibility. Is it possible to make our code written in
There's some code in our project that looks a bit like this: Private Sub
I saw this code snippet during our lab and it actually compiles in MSVC2008

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.