I'm trying to write a program that will convert a numerical representation of ASCII art to the art itself.
For example,
3 2,+,3,* 5,- 1,+,1,-
Should give:
3
++***
+-
The number that appears at the top of the "art" and numerical representation is the amount of lines that appear in the file.
Here is the code that I have written for this task. The file "origin.txt" contains a numerical representation of ASCII art while the destination folder should get the art itself.
import java.io
import java.util.*;
public class Numerical
{
public static void main(String[]args)
throws FileNotFoundException
{
File input = new File("origin.txt");
File output = new File("destination.txt");
numToImageRep(input,output);
}
public static void numToImageRep(File input, File output)
throws FileNotFoundException
{
Scanner read = new Scanner(input);
PrintStream out = new PrintStream(output);
int size = read.nextInt();
String symbol;
out.println(size);
for(int i = 0; i < size; i++)
{
int x = 0;
symbol = read.next();
while(x < symbol.length())
{
char a = symbol.charAt(x);
int c = Character.getNumericValue(a);
x = x + 2 ;
char L = symbol.charAt(x);
for(int j = 0; j < c; j++)
{
out.print(L);
}
x = x + 2;
}
out.println();
}
}
}
This code seems to work for a numerical format that contains no spaces, but as soon as a space appears in the numerical format, I get a string out of bounds exception.
For example:
2 2,+,* 1,+,-
will convert to art,
but,
2 2, ,5,* 3, ,9,*
Will result in:StringIndexOutOfBoundsException: String index out of range.
I'm not sure why this could be happening. If anybody has any advice for me, I would really appreciate it.
Thank you.
Aucun commentaire:
Enregistrer un commentaire