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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:47:21+00:00 2026-05-22T19:47:21+00:00

I am a beginner in Java and am doing an exercise from a book.

  • 0

I am a beginner in Java and am doing an exercise from a book. The task is to write a static method which takes as an argument a string array and returns a jumbled up version of the string in random order. To create a random number you must use the following:

import java.util.Random;
Random r = new Random();
int rand = r.nextInt();

My answer is as follows:

private static String[] jumble(String[] arr){
    String [] jumbled = new String[arr.length];
    int [] chosen = new int [arr.length];
    Random r = new Random();
    int rand = r.nextInt(arr.length);
    chosen[0] = rand;
    jumbled[0] = arr[rand];
    for(int i = 1; i < arr.length; i++){
        while(checkIfChosen(chosen, rand, i)){
            rand = r.nextInt(arr.length);
        }
        chosen[i] = rand;
        jumbled[i] = arr[rand];
    }
    print(jumbled);
    return jumbled;
}


private static void print(String[]arr){
    for(int i = 0; i < arr.length; i++){
        System.out.println(arr[i]);
    }
}

private static boolean checkIfChosen(int[] arr, int a, int ind){
    for(int i = 0; i < ind; i++){
        if(arr[i]==a){
            return true;
        }
    }
    return false;
}

This does work but it seems so bl**dy long winded for something so simple. Can anyone improve on this? Any easier ways to implement such a task adhering to the constraints mentioned in the question?

EDIT: With Fisher Yates Shuffle:

public static void main(String[] args) {
    String [] original = {"Hello", "How", "Are", "You"};
    jumble(original);
}


private static String[] jumble(String[] arr){
    Random r = new Random();
    for(int i = arr.length-1; i > 0; i--){
        int rand = r.nextInt(i);
        String temp = arr[i];
        arr[i] = arr[rand];
        arr[rand] = temp;
    }
    print(arr);
    return arr;
}


private static void print(String[]arr){
    for(int i = 0; i < arr.length; i++){
        System.out.println(arr[i]);
    }
}

A great piece of code and much more efficient than my answer. Thanks.

  • 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-22T19:47:22+00:00Added an answer on May 22, 2026 at 7:47 pm

    You probably just want to use the Fisher-Yates shuffle for this. It only takes one pass to do it and should produce a “fair” shuffle (assuming your random number generator has enough bits of entropy).

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

Sidebar

Related Questions

I am beginner to java and was trying out this code puzzle from the
I am a complete JSP beginner. I am trying to use a java.util.List in
I'm a beginner java developer and i'm not very confident with java world tools.
hello i'm a beginner java developer and this is one question in a list
I'm Java beginner, but I thought that when using try-catch-finally I don't have to
I'm a beginner in Java programming language, and I have a problem that I
Ok, I am beginner in JAVA. I have just started. I downloaded Java SE
String fileToBeRead = C:/Documents and Settings/Developer/Desktop/Anand exmps/Anand.xls; I have completed a java program in
A beginner question, bear with me: I'm just wondering under what circumstances one should
A beginner's question. I'm building a .swf with Flex Ant. To my .swf I

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.