#Illegal escape character in string literal

42 messages · Page 1 of 1 (latest)

thorny geodeBOT
#

This post has been reserved for your question.

Hey @woven cargo! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

burnt rivet
#

% isn't a special character for strings, you don't escape it with a backslash

#

so \ is the escape character for strings, right
% is similarly an escape character for formatting, it's just processed within java this time

#

how would you get a \ in a string, where it's an escape character?

woven cargo
#

if i get rid of it, it says it is a "malformed string" - should i just ignore this warning?

burnt rivet
#

no

woven cargo
burnt rivet
#

yep

#

in the same vein, %% is how you get a % in a format string, where it's an escape character

woven cargo
#

oooooh that makes a lot more sense now

#

gonna test it

#

sickkk bro it worked, thank you so much for your time and help! it's so satisfying to see the warnings and errors go to 0

thorny geodeBOT
burnt rivet
#

the string sees: \ is an escape character, an escape sequence is expected, however \% isn't a valid character for strings

#

when you remove the backslash, the string processes the content just fine, so other stuff can be evalutated

#

then, the formatting sees: % is an escape character, an escape sequence - or a format specifier, in this context - is expected, however %\n isn't a valid format specifier

woven cargo
#

are \ and % the only two format types?

#

or are there other characters that would produce this similar issue

burnt rivet
#

\ is the only one in java, the language, for string literals

#

% is used by other java code, but since that java code is standard and built-in, IDEs check for it for QOL

#

you can write your own java code (or use anyone else's) that has some kind of escape/replacement specifier with its own escape character and sequences
and you would get errors back (if the parser was made well) if you give it malformed input, like with invalid escapes here
it's just that the IDE wouldn't be able to tell you that, since that other thing would be non-standard (you could make an extension that does handle that escape though)

woven cargo
#

that's really helpful info, thank you so much!

thorny geodeBOT
burnt rivet
#

i'm not sure how good IDEs' static checking is tbh, but i think with one of these:

String.format("%" + "_");

Function<String, String> wrapper = (fmt) -> String.format(fmt);
wrapper.apply("%_");

#

/run

java.util.function.Function<String, String> wrapper = (fmt) -> String.format(fmt);
wrapper.apply("%_");
tropic prawnBOT
#

@burnt rivet I only received java(15.0.2) error output

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '_'
	at java.base/java.util.Formatter.checkText(Formatter.java:2732)
	at java.base/java.util.Formatter.parse(Formatter.java:2718)
	at java.base/java.util.Formatter.format(Formatter.java:2655)
	at java.base/java.util.Formatter.format(Formatter.java:2609)
	at java.base/java.lang.String.format(String.java:3292)
	at temp.lambda$main$0(file0.code.java:2)
	at temp.main(file0.code.java:3)
burnt rivet
#

oh also backslashes as characters (so would be \\ inside a string) are used in regexes, and $ is used in regex replacements, and since regex is part of standard java, using them incorrectly might also yield ide warnings

#

to summarize, difference (in terms of checking/processing) between format/regex/replacement escapes and string escapes is where they're processed
string escapes are built into the language
the others are built into the standard library of the language

#

the standard library, at least java's, is running with java, so there's no runtime difference if you make a similar parser

#

the difference between format/etc escapes and custom escapes is that the java's are built-in and ides know to check for them

woven cargo
#

ty again!

burnt rivet
#

the Function and lambda (->) there are basically to create an function expression for java (where functions are not first-class objects, the wrapper there is still technically an object so i can't directly call it)
if that example works (to not have the ide warn) then having any other kind of separation between the string and the method may work as well

#
String fmt = "%_";
String.format(fmt);
```maybe
#

i should probably not have used such an involved example there

#

that aside; the first language is always the hardest, if you're proficient in lua (or any other programming language) then another language, like java here, will be comparatively easy to learning how to write logic, etc

#

learning a new programming language is learning the syntax, the library, the toolings, you have to do that for each language
learning to program is learning logic and data, you only have to do that once

#

good luck

thorny geodeBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

thorny geodeBOT
# thorny geode

Before your post will be closed, would you like to express your gratitude to any of the people who helped you? When you're done, click I'm done here. Close this post!.