maya24
maya24

Reputation: 3

Shuffling string word by word

Good day. We have an assignment that requires a user to input something on the text area and when he hits the ok button, the popup box should have what he entered jumbled. (ex: "Hello World Tuna" turns to "olHel odlWr Tnau"). I managed to do a code for the shuffle, but it jumbles all the words instead of having it word by word.

We're not allowed to use Collections or anything, so is there a way to do it without using them?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class ChallengeSwapper2 extends JFrame
{
JTextArea txtArea;
JScrollPane scrPane;
JLabel lbl;
JButton btn;
Container con;

public ChallengeSwapper2()
{
    super("Challenge Swapper!");
    setLayout(new FlowLayout());
    con = getContentPane();

    lbl = new JLabel("INPUT");
    lbl.setFont(new Font("Arial", Font.BOLD, 20));
    con.add(lbl);

    txtArea = new JTextArea(10,15);
    txtArea.setWrapStyleWord(true);
    txtArea.setLineWrap(true);
    txtArea.setFont(new Font("Verdana", Font.BOLD, 20));

    scrPane = new JScrollPane(txtArea);
    scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    con.add(scrPane);

    btn = new JButton("OK");
    btn.setFont(new Font("Arial", Font.BOLD, 10));
    btn.setBounds(10,10, 10, 10);
    btn.addActionListener(new btnFnc());
    con.add(btn);
}

public class btnFnc implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {

        if (txtArea.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null,"Please enter something tanga", "Error Message",JOptionPane.INFORMATION_MESSAGE);
        }
        else 
        {
            String sentence = txtArea.getText();
            String[] words = sentence.split(" ");
            char [] letters = sentence.toCharArray();

            for( int i=0 ; i<words.length-1 ; i++ )
            {
                int j = (char)(Math.random() * letters.length);
                char temp = letters[i]; 
                letters[i] = letters[j];  
                letters[j] = temp;
            }

            String newtxt = new String(letters);

            JOptionPane.showMessageDialog(null, newtxt, "Swapper Challenge", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}


public static void main (String [] args)
{
    ChallengeSwapper2 gui = new ChallengeSwapper2();
    gui.setResizable(false);
    gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
    gui.setBounds(300,300,330,370);
    gui.show();
}
}

Upvotes: 0

Views: 592

Answers (2)

user4910279
user4910279

Reputation:

Try this.

    String s = "Hello World Tuna";
    Random r = new Random();
    String result = Stream.of(s.split(" "))
        .map(x -> x.chars()
            .mapToObj(c -> "" + (char)c)
            .reduce("", (a, c) -> {
                int i = r.nextInt(a.length() + 1);
                return a.substring(0, i) + c + a.substring(i);
            }))
        .collect(Collectors.joining(" "));
    System.out.println(result);

Upvotes: 0

Matt
Matt

Reputation: 3760

Your current code splits the sentence into words, but then you also convert the sentence into a character array and manipulate the whole sentence.

Having split the sentence into words, you should convert each word into a character array, and shuffle the letters in each word, and then reconstruct the sentence.

I would encourage you to move the shuffling into a new method so that you can isolate the shuffling procedure for clarity and easier testing.

Upvotes: 1

Related Questions