How do I detect "enter" from a string loaded from a text document variable

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

It returns an error saying it’s an invalid character constant

Try
with
“” please

Chrisir

Hello @TT3overnot,

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 ");
        }
      }
    }

:)

Another thought:

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).

Chrisir

Hello @TT3overnot,

I get this error if I use a ‘/n’ which is an invalid character constant:

image

An exploration of this topic:

// These two are the same character:
println(hex(ENTER));
println(hex('\n'));

if ('\n' == ENTER)
  println("true");

String s0 = "01234567-@ABCDEF"; // Double quotes for strings

String [] string = {s0};

if(string[0].charAt(8) == '-') // Single quotes here for characters
  println("- found");
  
string[0] = string[0].replace('-', '\n');  

if(string[0].charAt(8) == '\n') // Single quotes here for characters
  println("\\n found");
  
if(string[0].charAt(9) == '@')
  println("@ found");
  
if(string[0].charAt(10) == 'A')
  println("A found");

if(string[0].charAt(0) == 'B')
  println("A found");

:)