3

I'm new to Android Studio, and have ran into an issue where I paste in a code from a web tutorial into a project, fix imports, and still have every single line having some leading whitespace which the Android Studio recognizes as "Unexpected Token". After manually selecting the offending whitespace and deleting it, the errors disappear.

I tried Option + command + L shortcut for code formatting, but that did not fix the issue

How can I fix "Unexpected Token" error for code pasted into a .java file in Android Studio?

enter image description here

Community
  • 1
  • 1
Alex Stone
  • 46,408
  • 55
  • 231
  • 407

1 Answers1

3

Can't comment due to user level, but this is definitely a case of invisible characters from whatever web encoding you copied from. It's pretty common to copy whitespace from a website and it's not actually whitespace. It's a space-keeping invisible character used for website formatting, or even just an artifact from diferent character encoding that didn't copy over properly due to different character formats.

In short, just find/replace (⌘+R). Any time you copy something from a website, you run the chance of this error. Just be glad your IDE lets you know about it.

enter image description here

Alex Stone
  • 46,408
  • 55
  • 231
  • 407
mrfixij
  • 132
  • 1
  • 8