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 7081431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:55:13+00:00 2026-05-28T06:55:13+00:00

I want to write a method, which takes a String and swaps each pair

  • 0

I want to write a method, which takes a String and swaps each pair of characters in it and then concatenates them into a new String. Please let me know how to fix this code (or write a new better one):

    static String s;

    public static void proc(String w) {     
        ArrayList k = new ArrayList();
        ArrayList m = new ArrayList();
        System.out.println(w.length()); 
        int j = 0;
        //test arraylist to check if string is written into arraylist
        for (int i = 0; i < w.length(); i++){
            k.add(w.charAt(i));         
        }
        String p = k.get(2).toString();
        System.out.println(p);  

//here starts the logic of my app
        for (int n = 0; n < w.length(); n++){
            String v = k.get(n).toString();
            if (n == 0){
                m.add(1, v);
            }
            else if (n == 1){
                m.add(0, v);
            }
            else if ((n % 2) == 0){
                m.add(n+1, v);
            }
            else {
                m.add(n, v);
            }           
        }
    }

    public static void main(String[] args){
        s = "tests";
        proc(s);        
    }

Hi this is not a homework, but am doing exercises from a book. Anyway using code provided by Jon managed to work on my own – it may be not as much elegant but is doing the job using dynamic sizing as well:

public static void proc(String w) {     

        ArrayList k = new ArrayList();
        ArrayList g = new ArrayList();
        String h = "";

        for (int i = 0; i < w.length(); i++){   
            char temp = w.charAt(i);
            k.add(i, temp);
        }
        for (int i = 0; i < w.length(); i++){
            if (i == 0){
                h = k.get(1).toString();
                g.add(h);
            }
            else if (i == 1){
                h = k.get(0).toString();
                g.add(h);
            }
            else if ((i % 2) == 0){

                h = k.get(i+1).toString();

                g.add(h);
            }
            else if ((i % 2) == 1){
                h = k.get(i-1).toString();
                g.add(h);
            }
        }
        System.out.println(g.toString());
    }
    public static void main(String[] args){
        s = "test";
        proc(s);
    }
  • 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-28T06:55:14+00:00Added an answer on May 28, 2026 at 6:55 am

    I haven’t tried to go through exactly how your code is trying to work, but it looks unnecessarily complicated to me. Given that you don’t need dynamic sizing, you can do this more easily with an array:

    public static String swapPairs(String input) {
        char[] chars = input.toCharArray();
        for (int i = 0; i < chars.length - 1; i += 2) {
            char tmp = chars[i];
            chars[i] = chars[i + 1];
            chars[i + 1] = tmp;
        }
        return new String(chars);
    }
    

    Note that while this will work for “simple” characters (where each element of the array is independent of the rest), it doesn’t try to take any form of “composite” characters into consideration, such as characters formed from two UTF-16 code units (surrogate pairs) or combined characters such as “e + acute accent”. Doing this sort of contextually-aware swapping would take a lot more effort.

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

Sidebar

Related Questions

?string.Format(CultureInfo.GetCultureInfo(en-US), {0:d}, now) 4/12/2010 ?string.Format(CultureInfo.GetCultureInfo(fr-FR), {0:d}, now) 12/04/2010 I want to write a method:
I am creating a jasper report.In that I want to write one method which
Hey guys. I have a method that gets called each second which I want
I'm writing a web service which, for each method call, must write a log
I want to write a method which can take an arbitrary array of a
Give a base class Base , I want to write a method Test, like
I want to re-write a method that has way too many nested if statements.
I want to write a C# method that can accept any number. Something like:
OK, I know what you're thinking, "why write a method you do not want
I basically want to write a customer logger/tracer which also logs the class and

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.