0

I'm new to Java and programming in general. This is baffling me, and I feel like I may just need to scrap what I have and go a different route, but I don't know what route to take.

Below is the code I've written for translating a line from the user into braille. It works! Unfortunately, it works vertically. How can I, after using three lines to create the braille, make the next braille character appear next to the previous one instead of below it?

import java.util.*;

public class Program4
{

    public static String code1 = ". |";
    public static String code2 = " .|";
    public static String code3 = "..|";
    public static String code4 = "  |";

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Program 4 by Ross Walker");
        System.out.println();

        String s;
        System.out.println("Please give a line to translate");
        s = keyboard.nextLine();

        for(int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            if(c == '1' || c == 'a')
            {
                //new Program4().letterA(s.charAt(i));
                System.out.printf("%s%n%s%n%s%n", code1, code4, code4);
                System.out.println();
            }

            if((c == '2' ) || (c == 'b'))
            {
                System.out.printf("%s%n%s%n%s%n", code1, code1, code4);
                System.out.println();
            }
            if((c == '3') || (c == 'c'))
            {
                System.out.println(code3);
                System.out.println(code4);
                System.out.println(code4);
                System.out.println();
            }
            if((c == '4') || (c == 'd'))
            {
                System.out.println(code3);
                System.out.println(code2);
                System.out.println(code4);
                System.out.println();
            }
            if((c == '5') || (c == 'e'))
            {
                System.out.println(code1);
                System.out.println(code2);
                System.out.println(code4);
                System.out.println();
            }
            if((c == '6') || (c == 'f'))
            {
                System.out.println(code3);
                System.out.println(code1);
                System.out.println(code4);
                System.out.println();
            }
            if((c == '7') || (c == 'g'))
            {
                System.out.println(code3);
                System.out.println(code3);
                System.out.println(code4);
                System.out.println();
            }
            if((c == '8') || (c == 'h'))
            {
                System.out.println(code1);
                System.out.println(code3);
                System.out.println(code4);
                System.out.println();
            }
            if((c == '9') || (c == 'i'))
            {
                System.out.println(code2);
                System.out.println(code1);
                System.out.println(code4);
                System.out.println();
            }
            if((c == '0') || (c == 'j'))
            {
                System.out.println(code2);
                System.out.println(code3);
                System.out.println(code4);
                System.out.println();
            }
        }
    }
}

I've only written up to j in the alphabet. I'll continue if this is the only way I can get it done, but I'd like your help to do it the right way if possible.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Walker
  • 1
  • 1
  • Possible duplicate of [How can I move a cursor up the lines in a terminal?](https://stackoverflow.com/questions/26439182/how-can-i-move-a-cursor-up-the-lines-in-a-terminal) – Jolta Oct 10 '17 at 16:30
  • Perhaps the easiest approach would be to to build your whole output first, in an array or similar, then print it once you know all the characters. If you prefer, you can find an "ncurses" library to move the cursor around wherever you like. In either case, I think this is a duplicate of the question I linked above, even if the answer there is not very thorough. – Jolta Oct 10 '17 at 16:32
  • When you say build the whole output in an array, do you mean designate an array in braille to each of the 26 letters, and then have the array printed when the specific char is detected by the scanner? – Walker Oct 10 '17 at 16:44
  • No, I mean that if you really want to use the `println()` method, you can't move upwards in a simple way. So, you should instead prepare all the output for a whole line, then print it as the last stage. Putting your output characters into some sort of output buffer - an analog to the double buffering that graphics drivers do. But really, read the question I linked, and the answer! – Jolta Oct 11 '17 at 07:42

0 Answers0