Im having trouble creating a Word Decoder, Basically I'm trying to write a method that chooses a random character, and increases in by one place (a to b, b to c) and so on.

Any ideas in how to approach that?

Thanks,

JavaRookie

Dani AI

Generated

This complements the thread and builds on ’s array suggestion while addressing ’s GUI behaviour and undo requirement.

Treat the set of allowed characters as a fixed, cyclic sequence (printable ASCII 32–126 is a convenient choice because it includes letters, digits and most symbols). Keep the current text in a mutable buffer (StringBuilder or a char[]). Each Encode press: pick a random index with Random.nextInt(length), compute the next character by advancing one position inside the cyclic range, replace it in the buffer, push an operation record (index and oldChar) onto a Stack, update the midField text, increment stepField (stack size) and write a short entry into bottomField (for example “pos 4: 'a'->'b'”). Decode is just popping the stack and restoring the saved oldChar at its index and updating the fields.

Sample utility and call pattern (use in event handler; buffer shown as StringBuilder buf):

private char incrementPrintable(char c) {
    if (c < 32 || c > 126) return c;               // leave unusual chars unchanged
    return (char) (((c - 32 + 1) % 95) + 32);      // wrap inside 32..126
}

Random rand = new Random();
int i = rand.nextInt(buf.length());
char old = buf.charAt(i);
char next = incrementPrintable(old);
buf.setCharAt(i, next);
history.push(new Op(i, old));                      // Op holds index and old char
stepField.setText(String.valueOf(history.size()));
bottomField.setText("pos " + (i+1) + ": " + old + "->" + next);
midField.setText(buf.toString());

Notes and cautions: use a mutable structure (StringBuilder) to avoid costly string recreation; decide whether repeats are allowed (if not, maintain a list of remaining indices); keep GUI updates on the Event Dispatch Thread; prefer consistent Swing components (JTextField/JLabel) instead of mixing heavy AWT components with Swing. This approach gives simple, reversible steps and a clear encoding key for the UI.

Recommended Answers

All 7 Replies

Use arrays. Create an array of letters a to z. For each letter you need to decode, look for it in the array. then output the array element.

I need to be able to use letters, numbers and symbols. I'm so lost as to how to get the method to select a random character, and increase its "place by one"

Here's my subclass so far,

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.util.Random;

public class Scrambler {

    private TextField topField, midField, bottomField, stepField;
    private final static String newline = "\n";
    private TextField quantityField;
    private Scanner scanner, string;
    private char chars;
    public Scrambler(JFrame window){
        Label directions, encodet, encodek, steps;
        directions = new Label("Enter Text Here");
        directions.setBounds(50,50,500,30);
        directions.setForeground(Color.black);
        directions.setBackground(Color.green);
        window.add(directions);

        encodet = new Label("Encoded Text");
        encodet.setBounds(50,125,500,30);
        encodet.setForeground(Color.black);
        encodet.setBackground(Color.green);
        window.add(encodet);

        encodek = new Label("Encoding Key");
        encodek.setBounds(50,200,500,30);
        encodek.setForeground(Color.black);
        encodek.setBackground(Color.green);
        window.add(encodek);

        steps = new Label("Encoding Steps Remaining");
        steps.setBounds(50,275,500,30);
        steps.setForeground(Color.black);
        steps.setBackground(Color.green);
        window.add(steps);

        topField = new TextField();
        topField.setBounds(55,75,400,20);
        topField.repaint();
        window.add(topField, 0);

        midField = new TextField();
        midField.setBounds(55,150,400,20);
        midField.repaint();
        midField.setEditable(false);
        window.add(midField, 0);

        bottomField = new TextField();
        bottomField.setBounds(55,225,400,20);
        bottomField.repaint();
        bottomField.setEditable(false);
        window.add(bottomField, 0);

        stepField = new TextField();
        stepField.setBounds(225,280,50,20);
        stepField.repaint();
        stepField.setEditable(false);
        window.add(stepField, 0);

    }

    public void startEncode(){
        String midFieldAtPre;

        midFieldAtPre = midField.getText();
        midField.setText(topField.getText());

    }

    public void encodeText() {
        char[] ch = {'a','b','c','d'};
        
       
    }
    

    public void decodeText(){

    }

    public void repaintWindow(){

    }

}

I can send you an example of how it is supposed to look if that helps any..

As I understand your problem.

1. User input text on midField.
2. Then you will get the text inputted and you will decode the whole string by changing each letter in the string to the next letter in the alphabet. eg (a to b, b to c)
3. Then you will display this decode text.

For example,

User Input: eric

Decoded text: fsjd

Is that right??

Well basically all four of those Methods are a button. Each time you hit Start "Eric" will move to the midField text box, and the number of Encryption steps will show in the stepField. Then every time you hit the encodeText button, it selects a random character and increases it by one place. so: "Esic" now appears, and that character place shows in the bottomField, while the stepField shows one. Hit encode again it selects another random character and increases it by one place so: "Esid" now appears and the character place shows in the bottom field, while the stepField shows 2. And so on. Then every time you hit the decodeText button, it removes one step/ecryption, so it goes back to "Esic" reduces the step and removes that character place.

User inputs Text on top field. But hits start so it appears on the midField.

Have you done studying arrays?? I think its possible with arrays. You need to create 3 arrays:

inputArray (where you put the inputted String)
encodeArray (consists of 1 or 0, 1 if that char is encoded, 0 if not)
lettersArray (where you pt the letters of the alphabet)

With regards to randomization, you can search Google for random java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.