Version 2.0 - Refactored: Eliminate need for ArrayList + Documented Code
This commit is contained in:
1 parent
de1730192c
commit
73c0fa5b95
4 files changed
+34
-15
No files matched your search
BIN
Binary file not shown.
Binary file not shown.
@@ -1,36 +1,51 @@
|
||||
// Package declaration
|
||||
package eobard;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// Import dialog box library
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
// Class declaration
|
||||
public class Eobard {
|
||||
|
||||
// init main
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Receives and stores string from user
|
||||
String stringToReverse = JOptionPane.showInputDialog(null, "String To Reverse:");
|
||||
|
||||
String result = stringReverser(stringToReverse).toString();
|
||||
// Calls stringReverser method and stores output as String
|
||||
String result = stringReverser(stringToReverse);
|
||||
|
||||
// Displays result to user
|
||||
JOptionPane.showMessageDialog(null, result);
|
||||
|
||||
}
|
||||
|
||||
// Main string reverser method (functionality of program)
|
||||
public static String stringReverser(String stringMessenger) {
|
||||
|
||||
List<Character> stringReverserList = new ArrayList<>();
|
||||
// Declare character array that is equal to the length of the string
|
||||
char[] stringReverserArray = new char[stringMessenger.length()];
|
||||
|
||||
for(int i = stringMessenger.length() - 1; i > -1; i--) {
|
||||
stringReverserList.add(stringMessenger.charAt(i));
|
||||
}
|
||||
for(int i = 0; i < stringReverserList.size(); i++) {
|
||||
stringMessenger += stringReverserList.get(i);
|
||||
// Declares 'i' to count down, and j to count up, thus, allowing the program to manipulate characters both forward and backward
|
||||
// // includes correction for zero index(s)
|
||||
for(int i = stringMessenger.length() - 1, j = 0; i > -1; i--) {
|
||||
|
||||
// Adds characters from the length of the string to zero to the index of stringReverserList in the reverse counting direction
|
||||
stringReverserArray[j++] = (stringMessenger.charAt(i));
|
||||
|
||||
// Appends characters from stringReverserList to the String later to be returned.
|
||||
stringMessenger += stringReverserArray[j - 1];
|
||||
|
||||
}
|
||||
|
||||
// Purges first half of string (unreversed section)
|
||||
stringMessenger = stringMessenger.substring((stringMessenger.length() / 2), stringMessenger.length());
|
||||
|
||||
// Returns string in question
|
||||
return stringMessenger;
|
||||
|
||||
}
|
||||
|
||||
// %end
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
# Eobard
|
||||
Java application that reverses any given text. Built for practice.
|
||||
|
||||
This task can be done in half the lines and much less complexity by using the "StringBuilder" calss, but I wanted to build it on my own to get un-rusty from basic Java, so the only thing I allowed myself to use was basic programmatic operations and ArrayList.
|
||||
This task can be done in much less lines and much less visual complexity by using the "StringBuilder" class, but I wanted to build it on my own to practice writing Java, so the only thing I allowed myself to use is basic programmatic operations.
|
||||
|
||||
Changelog:
|
||||
|
||||
- Version 2.0 - The much unexpected release! Eobard is now completely refactored and documented. This version eliminates the need for the "ArrayList" library, and now the only non-default library used is JOptionPane. I also fixed the readme a bit.
|
||||
- Version 1.0 - Initial Release
|
||||
|
||||
Screenshots:
|
||||
|
||||
@@ -13,18 +17,18 @@ To run:
|
||||
|
||||
1. Download .jar
|
||||
2. Open shell
|
||||
3. cd to download directory
|
||||
3. cd to clone directory
|
||||
4. Type "java -jar Eobard.jar"
|
||||
5. Profit?
|
||||
|
||||
|
||||
Below is a much simpler way to do this. Enjoy:
|
||||
Below is a much simpler way to do this yourself using the StringBuilder class. Enjoy:
|
||||
|
||||
================================================
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
public class TestingGrounds {
|
||||
public class ReverseAString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
Reference in new issue
Block a user