• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help randomly scrambling a word, and allowing user to unscramble by swapping letters

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So  I am creating a game where a file of words is given, and the program randomly selects a word from that file, the program must then randomly scramble this word a random number of times and the user will then be given the option to swap letters around by selecting two indexes. I have achieved a lot of the program and I feel like I am on the right track with the swapping, but i just cannot figure it out. We have not learned arrays or anything and my teacher said not to use them. She said I "need to make a new string by concatenating the parts of the string where these two letters swap their places". So she wants me to use concatenation, but I just cannot figure how to use that to scramble the word randomly. I also cannot figure out how to allow the user to swap letters by choosing the indexes. I made it so they can select the indexes and it successfully replaces index 1 with index 2, but not index 2 with index 1. Here is my code so far


I am very sorry for how probably sloppy and hard to read it is, but I am very new to java and coding and really need some help. I'm not sure if I'm using this forum right, i tried to figure it out but I'm getting a little desperate so I apologize if this isn't formatted correctly or maybe in the wrong section. Thank you in advance because absolutely any advice is greatly appreciated.
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid that main method is much too long. I think you shou‍ld refactor that code to move nearly all of it out of the main method. I think you shou‍ld have a ScrambledWord object which takes a word as its constructor parameter and later scrambles the field. It can have scramble and unscramble methods; the latter might have a loop where the user enters numbers to swap and stops whenever a number out of range is entered (e.g. < 0).
Don't use file input stream to read a text file. Use file reader and buffered reader, as shown in the Java™ Tutorials (look for buffered streams section).
Avoid doing arithmetic with Math#random(), despite what it says in the Java™ Tutorials. Look at this old discussion, which discusses potential errors about “random” numbers.
Never write == true or == false. Both are poor style and error‑prone. It is only a matter of time until you write = by mistake, and then your program will start to go wrong.
Not
while(userConsent == true) ...
but
while (userConsent) ...
Not
while(userConsent == false) ...
but
while (!userConsent) ...
Don't use multiple if statements for menus using 1 2 3 etc. Use switch‑case‑break.

Strings are not intended for changing; the class is immutable. That is why you are finding it so awkward to change the String. I do not think you can use a replace method on a String to achieve what you want.
There are all sorts of design advantages to making things immutable. But many immutable classes have mutable counterparts which are intended for changing. String has a mutable counterpart and here it is, specially designed for changing text rapidly and painlessly. It has methods like insert delete append and replace, so all you need to do is work out the index and the char to replace. You can even chain method calls like this:-
mutableText.insert(index1, char2).insert(index2, char1);
When you have finished with the changes, you can retrieve the text with the toString method. If you need to test for equality during that procedure try
if (mutableText.toString().equals(correctText)) ...

It would have been much easier to achieve that with arrays; you have presumably been told not to use arrays to give you more of a challenge.
How are you scrambling the word? Are you doing that programmatically or asking a user to specify letters to swap?
 
John Johanson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

I am afraid that main method is much too long. I think you shou‍ld refactor that code to move nearly all of it out of the main method. I think you shou‍ld have a ScrambledWord object which takes a word as its constructor parameter and later scrambles the field. It can have scramble and unscramble methods; the latter might have a loop where the user enters numbers to swap and stops whenever a number out of range is entered (e.g. < 0).
Don't use file input stream to read a text file. Use file reader and buffered reader, as shown in the Java™ Tutorials (look for buffered streams section).
Avoid doing arithmetic with Math#random(), despite what it says in the Java™ Tutorials. Look at this old discussion, which discusses potential errors about “random” numbers.
Never write == true or == false. Both are poor style and error‑prone. It is only a matter of time until you write = by mistake, and then your program will start to go wrong.
Not
while(userConsent == true) ...
but
while (userConsent) ...
Not
while(userConsent == false) ...
but
while (!userConsent) ...
Don't use multiple if statements for menus using 1 2 3 etc. Use switch‑case‑break.

Strings are not intended for changing; the class is immutable. That is why you are finding it so awkward to change the String. I do not think you can use a replace method on a String to achieve what you want.
There are all sorts of design advantages to making things immutable. But many immutable classes have mutable counterparts which are intended for changing. String has a mutable counterpart and here it is, specially designed for changing text rapidly and painlessly. It has methods like insert delete append and replace, so all you need to do is work out the index and the char to replace. You can even chain method calls like this:-
mutableText.insert(index1, char2).insert(index2, char1);
When you have finished with the changes, you can retrieve the text with the toString method. If you need to test for equality during that procedure try
if (mutableText.toString().equals(correctText)) ...

It would have been much easier to achieve that with arrays; you have presumably been told not to use arrays to give you more of a challenge.
How are you scrambling the word? Are you doing that programmatically or asking a user to specify letters to swap?



I think this how you reply to someone, if its not im sorry. But thank you very much for all of your advice, I'm very very new to coding so everything was helpful. And about the scrambling, firstly the program after selecting the random word from the select, will randomly scramble it a random number of times, then present the user this word. The user will then be given the option to specify which letters to swap by inputting two index values separated by a space, to swap those two values. I've been told using a combination of .substring(); and .charAt(); and the two index values could be a solution, but I cannot figure out how to use these functions to achieve that. Thank you though for your help and in advance for your future assistance
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I'm going to try to explain by way of analogy. Imagine being asked to cook a seven course meal for Thanksgiving dinner. Then you're handed this long piece of paper with all the recipes and cooking instructions listed out. The only problem is, there is little or no indication as to where one recipe starts and another begins. It's just a long, confusing narrative that looks like someone sat down and wrote down whatever came to mind. That would be kind of a nightmare, right?

Now, imagine if you were handed seven index cards instead, with each card containing a coherent and nicely organized set of instructions for ONE SINGLE course. Then, a short cover letter suggesting the order in which you might want to start working on the courses so that when all is said and done, you have everything nice, warm, and fresh. That would make your job a lot easier, right?

The nightmare scenario is what putting all of your code into the main() method is like. When it was suggested that you should refactor your code, that means you should move things around and organize them better so that your code looks like the well-organized scenario. The thanksgiving dinner program might look something like this:

Does that make sense?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic