I made a Java program that asks for a word, then a screen width, then whether you want it to go fast or slow. Then it outputs that word and bounces it across the screen. Is this code efficient for what it does? Is there a better way to do it (without a pre-defined method/class)? And does it look professional and easy to read?
/*
This program was designed for use with command prompt on an ASCII
character set. Output is run through CMD.
This was designed for CMD with only an 80 ASCII character width.
str1 is the string containing the spaces.
str2 is the string containing the input word.
*/
import java.util.Scanner;
class Wave6 {
//Causes the output to lag for a set amount.
public static void xwait(long L) {
for(long c=0; c<L; c++);
}
//Asks for a string to "bounce" across the screen.
public static String input_string(char chr, String str2)
throws java.io.IOException {
do {
System.out.print("\n"
+ "What word do you what to input? \n"
+ "Must be less than 81 characters: ");
/*
Reads a list of characters and
inputs it into a string (to allow
for spaces and other non-letter chars).
*/
do {
chr = (char) System.in.read();
if(chr != '\n') str2 += chr;
} while(chr != '\n');
//Checks if the string is in the acceptable range.
} while((str2.length() < 1) | (str2.length() > 80));
return str2;
}
//Asks for a screen width (this is arbitrary).
public static int input_width(int width, String str2){
Scanner input = new Scanner(System.in);
do {
System.out.print("\n"
+ "What is the width of the screen? \n"
+ "Must be less than 81, and more than " + str2.length() + " characters: ");
width = input.nextInt();
} while((width <= str2.length() ) | (width >= 81));
return width;
}
//Asks if the user wants the program to run
//at full speed, or slower.
public static long input_speed(char ch, char ign, long L)
throws java.io.IOException {
do {
System.out.print("\n
+ "Do you want it slow or fast? \n"
+ "S or F: ");
ch = (char) System.in.read();
//Gets rid of unwanted chars in
//the char buffer.
do {
ign = (char) System.in.read();
} while(ign != '\n');
//checks if ch == F or S
} while( !(ch == ('F')) ^ (ch == ('S')) );
//Returns a large or small number
//to slow down the program, or not.
L = 0;
if(ch == 'S')
L = 25550000L;
else if(ch == 'F')
L = 0;
return L;
}
public static void main(String args[])
throws java.io.IOException {
String str1 = "", str2 = "";
int width = 0, i;
char ch = '\0', chr = '\0', ign = '\0';
long L = 0L;
str2 = input_string(chr, str2);
width = input_width(width, str2);
L = input_speed(ch, ign, L);
/*
Part of the program that actually makes the word
bounce around.
*/
for(;;) {
for(;;) { //Right bound
/*
If the two strings added length is equal to the
Input width +1, do not output a character return.
This is the end of the right bound, break out
of the infinite loop.
Based on the code, between the conversion from
right bound to left bound, str1 has to equal width.
Otherwise the word only goes to the width -1 space.
So str1 + str2 must equal width +1
*/
if( width + 1 == (str1.length() + str2.length()) ) {
System.out.print(str1 + str2);
xwait(L);
break;
}
else {
System.out.println(str1 + str2);
xwait(L);
}
//If the two strings are not equal to width +1
//add one space to str1
if(width + 1 != ( str1.length() + str2.length()) )
str1 += " ";
}
for(;;) { //Left bound
/*
Right bound handles the code when str1 and str2
are equal to width +1, so left bound doesn't have to.
Similarly Right bound handles the output for when
str1 equals 0, but left bound makes it equal to 0.
*/
if( width + 1 != (str1.length() + str2.length()) ) {
System.out.println(str1 + str2);
xwait(L);
}
//Take a space off of str1.
str1 = str1.substring(0, str1.lastIndexOf(" "));
if(str1.length() == 0)
break;
}
}
}
}
Also, I use n++, and I have my tab set to two ASCII character widths, so when I copied and pasted all of it was set to four spaces, is there an easy way to copy it without having to go back and delete all of the tabs and replace them with spaces? It takes forever. (this isn't important to me, but it is a hassle when trying to ask questions about code on here)
This happened when I did this in Basic on my Ti 84, but what if I had made an output_leftBound()
and output_rightBound()
method, used main to call rightBound()
, then had rightBound()
and leftBound()
call each other? Would that be efficient or would it cause the memory to build up? On the calculator, if you goto
out of loops, the calculator builds up memory because it reserved it to look for an END command to signal the end of a loop, but it never finds one.
Someone mentioned that:
public static void main(String args[])
throws java.io.IOExeption {
was a code smell. How (in an inquisitive tone)? The book I am using uses it whenever it uses System.in.read()
. Though I did notice that when I didn't include it my other methods that use System.in.read()
, the compiler reported an error stating:
Unreported exception IOExeption; must be caught or declared to be thrown
Is this a problem?