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

**URL:** <https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446>\
**Category:** Coding Questions\
**Created:** [February 25, 2022, 7:15pm UTC](https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446 "2022-02-25T19:15:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TT3overnot](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tt3overnot/32/16608_2.png) [@TT3overnot](https://discourse.processing.org/u/TT3overnot)\
**Post date:** [February 25, 2022, 7:15pm UTC](https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446/1 "2022-02-25T19:15:09Z")

</div>

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:

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [February 26, 2022, 9:26pm UTC](https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446/2 "2022-02-26T21:26:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TT3overnot](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tt3overnot/32/16608_2.png) [@TT3overnot](https://discourse.processing.org/u/TT3overnot)\
**Post date:** [February 26, 2022, 9:40pm UTC](https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446/3 "2022-02-26T21:40:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 26, 2022, 9:48pm UTC](https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446/4 "2022-02-26T21:48:21Z")

</div>

> [@Flolo](#):
>
> if (string[OOI].charAt(OOOI - saveOOOI) == ‘\n’) {

Try  
with  
“” please

Chrisir

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 27, 2022, 12:22am UTC](https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446/5 "2022-02-27T00:22:14Z")

</div>

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:

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

```

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 27, 2022, 11:20pm UTC](https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446/6 "2022-02-27T23:20:48Z")

</div>

> [@TT3overnot](#):
>
> the problem is that when ever there is a new line in the document it reads that as nothing

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 1, 2022, 4:09pm UTC](https://discourse.processing.org/t/how-do-i-detect-enter-from-a-string-loaded-from-a-text-document-variable/35446/7 "2022-03-01T16:09:12Z")

</div>

Hello @TT3overnot,

> [@TT3overnot](#):
>
> It returns an error saying it’s an invalid character constant

I get this error if I use a _‘/n’_ which is an _invalid character constant_:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/ddf37c11a3542691a25fd4eee28ffdd168608fe8.png)

An exploration of this topic:

```auto
// 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");

```

`:)`
