Help! I’m trying to simulate specific key presses from a text document, the problem is that when ever there is a new line in the document it reads that as nothing. Specifcly " ‘’ ", here’s my code, please help:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
char[] letters;
String[] string;
int amountOfLetters = 0;
int saveOOOI;
void setup() {
size(200, 200, P2D);
string = loadStrings("input.txt");
for (int i = 0; i <= string.length - 1; i++) {
for (int OI = 0; OI <= string[i].length() - 1; OI++) {
amountOfLetters++;
}
}
letters = new char[amountOfLetters];
saveOOOI = 0;
int OOOI;
for (int OOI = 0; OOI <= string.length - 1; OOI++) {
for (OOOI = saveOOOI; OOOI <= string[OOI].length() + saveOOOI - OOI - 1; OOOI++) {
if (string[OOI].charAt(OOOI - saveOOOI) == ENTER) {
// here the problem occurs, it doesn't detect enter
letters[OOOI + OOI] = '@';
}
else {
letters[OOOI + OOI] = string[OOI].charAt(OOOI - saveOOOI);
}
}
saveOOOI = OOOI;
}
printArray(string);
printArray(letters); // the spot where there should be "@" is shown as " '' " for some reason
println(amountOfLetters);
}
Try: if (string[OOI].charAt(OOOI - saveOOOI) == '\n') { // here the problem occurs, it doesn't detect enter letters[OOOI + OOI] = '@'; }
‘\n’ is a new line
I could not follow your code easily with the variable names you used.
loadstrings() removes the \r and \n from the file for the string array elements.
Sometimes you need to rebuild from scratch and add test code.
Simplified example:
String[] strings;
strings = loadStrings("file.txt");
println(strings.length);
for (int si = 0; si < strings.length; si++)
{
print(strings[si]); // These array elements DO NOT contain an \r or \n
for (int li = 0; li < strings[si].length(); li++)
{
//print(strings[si].charAt(li)); //test ok
//if (strings[si].charAt(li) == '\n')
if (strings[si].charAt(li) == 'F')
{
print(" X ");
}
}
}
loadStrings splits the text from the text file at the new lines.
Hence it fills an array ( String[] string; - not a good name btw, stringList would be better); so basically the line breaks are deleted by loadStrings.
Therefore, you won’t find line breaks.
Solution: in your nested for loop, after checking one slot in string array, pretend a line break has been sent and act accordingly (after the end of the inner for loop imho).