Reputation: 300
Im doing a English to braille program. This is how im doing it
if(letter_saver.contains("a")){
System.out.println("|. |");
System.out.println("|..|");
System.out.println("| .|");
it out prints
|. | //but i need to be able to come back to this line to do the next letter
|..|
| .|
Instead of it printing it down here.
Upvotes: 4
Views: 723
Reputation: 378
Create an object which has separate objects for each line. Input a string and convert the string into braille with three loops. Each loop calls a different line and builds a string. This allows you to add new characters without manually adding the characters.
public class Braille {
char letter;
String lineOne;
String lineTwo;
String lineThree;
//More variables if necessary
public Braille(char letter, String lineOne, String lineTwo, String lineThree){
this.letter = letter;
this.lineOne = lineOne;
this.lineTwo = lineTwo;
this.lineThree = lineThree;
}
public char getLetter(){
return letter;
}
public String getLineOne(){
return lineOne;
}
public String getLineTwo(){
return lineTwo;
}
public String getLineThree(){
return lineThree;
}
}
Then create a new object to represent a letter.
Braille a = new Braille('a', "|. |", "|..|", "| .|");
//A data structure to store the letters is necessary.
ArrayList<Braille> brailleLetters = new ArrayList<Braille>();
brailleLetters.add(a);
public Braille getLetter(char letter){
int index = 0;
while(index < brailleLetters.size()){
if(brailleLetters.get(index) == letter){
return brailleLetters.get(index);
index++;
}
return null;
}
With three loops each line can be build by calling the appropriate getLine function. Letters are easily stored and strings can be easily converted.
String convertString = "Test";
String lineOne = "";
String lineTwo = "";
String lineThree = "";
int index = 0;
while(index < convertString.length){
lineOne += getLetter(converString.getCharAt(index)).getLineOne();
index++;
}
//More loops needed based on number of lines.
I haven't run this code but something like this will work.
Upvotes: 1
Reputation: 98
You're not going to be able to accomplish this using System.out.print.
Instead, you'll need some other variable to hold your text while you're working on it.
I'd recommend using a string array (or ArrayList if you don't know how many lines there will be in advance), with each entry corresponding to a line of text.
ArrayList<String> lines = new ArrayList<String>();
lines.add("|. |");
lines.add("|..|");
lines.add("| .|");
This way, you can easily go back to edit a previous line as follows:
lines.set(0, lines.get(0).concat("text to add"); // Edit a previous line
Then, when you're ready to display the results:
for(String s : lines)
System.out.println(s);
Upvotes: 4