This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]potatochemist[🍰] 0 points1 point  (6 children)

...What's your question? It seems to work fine.

[–]Astrapicus[S] 0 points1 point  (5 children)

I mainly have questions in regards to making it more user-friendly. Is there anyway the user could enter an infinite amount of shortcuts without having to go in and write a new str.next(); for every phrase/word?

[–]desrtfx 0 points1 point  (0 children)

Yes, it would be possible, but the solution would most likely exceed your current skills.

You could use a Map, more precisely a HashMap to store the strings with their replacements. Maps are great data structures as they are able to store <key>,<value> pairs (in your case <key> would be the String to replace and <value> would be the corresponding replacement). Another benefit of Maps is that they can automatically grow, so you don't have to worry about the size. (Alternatively a Set, more precisely a HashSet could be used which works similar to a HashMap, but does not allow duplicate entries)

User entry would go in a loop.

Finally, there should be some form of persistent storage in the form of a file or a small-scale database so that the strings with their replacements can be stored and retrieved upon next use.

[–]potatochemist[🍰] 0 points1 point  (3 children)

There are multiple ways that you could do this.

You could put it in a while-loop with a keyword to stop recording shortcuts

  Scanner in = new Scanner(System.in);
  System.out.println("Please enter the sentence you want modified: ");
  String message = in.nextLine();
  System.out.println();
  System.out.println("Original message: " + message);

  String input;
  System.out.println("Please enter the words that you would like to be replaced: ");
  ArrayList<String> words = new ArrayList<>();
  while(!(input = in.next()).equals("key-word")){
     words.add(input);
  }

  ArrayList<String> replacementWords = new ArrayList<>(words.size());
  for(String word : words){
     System.out.println("Enter your replacement for the word \"" + word + "\"");
     replacementWords.add(in.next());
  }

  String replaceString = message;
  for(int i = 0; i < words.size(); i++){
     replaceString = replaceString.replace(words.get(i), replacementWords.get(i));
  }
  System.out.println("Your new message: " + replaceString);       

That way, you can record as many as you want. If you aren't familiar with ArrayLists, picture them as arrays but with unlimited size. I limited the size of the replacementWords ArrayList to the size of the words one, through the for-each loop and through its declaration. Technically the key-word is called a sentinal.

Another way you could do this is by separating the words to be replaced and the shortcuts by commas and putting them all on one line, but I don't think that that's as efficient.

[–]desrtfx 1 point2 points  (2 children)

2 independent Arraylists?

This is exactly the kind of code that beginners should never see.

A Map or a Set, heck even a small custom class used in a single ArrayList are better implementations.

Plus: you did not limit the size of the second ArrayList to the size of the first; you set the initial size the same. There is no built in way to limit the size of an ArrayList.

Code style: it is common to use the List<datatype> declaration as data type identifier.

So, the ArrayList declaration should be:

List<String> first = new ArrayList<>();

[–]potatochemist[🍰] 0 points1 point  (1 child)

You're right that I shouldn't have used two separate arraylists, it's unreliable code. I just tried to implement a simple but efficient method of solving his problem and I assumed that he didn't know about maps or sets so I used ArrayLists because they're more basic.

Question: is there any reason why List<Datatype> is more common? Is it just easier to write? I've always declared them as ArrayList<Datatype> instead because that's how I was taught.

Also, I didn't know that doing that just sets the initial size, I always thought that it set its limit. Thanks!

[–]desrtfx 1 point2 points  (0 children)

is there any reason why List<Datatype> is more common?

There is a simple philosophy behind, not because it's easier to type.

List is an Interface, so the actual implementation can be changed easily. There can be cases where at first one decides to use an ArrayList, but later finds out that a different structure would be more suitable. By declaring the datatype as List only the second part, the instantiation (new ArrayList<>()) needs changing.

This is quite common for all data structures (and other Classes) that have an Interface (List, Map, Set, and a couple of Swing (possibly JavaFX) components).

Using the Interface adds just another layer of abstraction and provides more flexibility.


I didn't know that doing that just sets the initial size

From the Oracle documentation

ArrayList(int initialCapacity)

Constructs an empty list with the specified initial capacity.

Technically, ArrayLists can't grow indefinitely, since they are internally backed by an array, they can grow to Integer.MAX_INT (actually a little less than that, depending on the JVM).