Bizzare System.out.println() in Java Program - java

String messageFile = ... // Assume messageFile SHOULD have the string "MESSAGE"
System.out.println("The messageFile is: " + messageFile + "!!");
Normally, one would expect the above command to output:
The messageFile is: MESSAGE!!!!
However, I am receiving this instead:
!!e messageFile is: MESSAGE
See how the above statement, the "!!" points seem to wrap around the message. My theory is that the:
String messageFile = ...
contains more characters than my assumed "MESSAGE". As a result, it's wrapping the next input (in this case, the "!!") to the front of the System.out.println() message.
What character is causing this?
Extra info:
Btw, messageFile is being initialized by passing a command line argument to a java class, myClassA. myClassA's constructor uses a super() to pass the messageFile parameter to myClassB. myClassB passes messageFile into a function().

I would guess you have a stray carriage return (\r) within the messageFile variable that is unaccompanied by a line feed (\n).
EDIT - this tests as expected:
class Println {
public static void main(String[] args) {
System.out.println("xxxx this \rTEST");
}
}
Output:
TEST this

Your message variable possibly contains a '\r' (carriage return) or '\n' (line feed) character at the end. This may cause the cursor to return to the first column before printing the exclamation marks.

For debugging you should print the codepoint of each character of messageFile via codePointAt.
As as result you see exactly the content of messageFile.

Replace all carriage returns in the file with newlines and then replace all double-newlines with single-newlines:
messageFile.replace('\r', '\n').replace("\n\n", "\n)
Carriage returns should be banned :D

Related

substring using \(backslash) in java

I want to get file name from complete path of file.
Input : "D://amol//1/\15_amol.jpeg"
Expected Output : 15_amol.jpeg
I have written below code for this
public class JavaApplication9 {
public static void main(String[] args) {
String fname="D://amol//1/\15_amol.jpeg";
System.out.println(fname.substring(fname.lastIndexOf("/")));
System.out.println(fname.substring(fname.lastIndexOf("\\")));
}
}
but getting below output :
_amol.jpeg
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -1
at java.lang.String.substring(String.java:1927)
at javaapplication9.JavaApplication9.main(JavaApplication9.java:6)
C:\Users\lakhan.kamble\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53:
Java returned: 1
The string \15 is an "octal escape" for the carriage return character (0x0d, 13 decimal). There are two possibilities here.
You really meant \15 to be the octal escape, in which case you are trying to create a filename with an embedded newline. The actual contents of fname in this case could be expressed as
"D://amol//1/" + "\n" + "_amol.jpeg";
Windows will prevent that from happening and your program will throw an IOException.
You really meant
String fname="D://amol//1/\\15_amol.jpeg";
In this case the extra backslash is redundant and will be ignored by Windows because the filename will resolve (in Windows path terms) to D:\amol\1\\15_amol.jpeg and adjacent directory separators collapse to a single separator. So you could just omit the extra backslash altogether without changing the effective path.
As to your exception, the string as shown DOES NOT contain a backslash character (case 1 above), so
fname.lastIndexOf("\\")
returned -1, causing the exception

Java Regex - Match line word that not inside string

For my project, I want to get class using regex but not inside string
for e.g
class fo{
void foo{
System.out.println("example writing of class");
System.out.println("class cls{");
}
}
so, I hope result like this :
class fo{
I have try create a pattern but not working, Here.
Pattern.compile("\\b(?!(\"))class\\s+\\w+\\{\\b(?!(\"))")
Try this regex:
(?:^|\\n)\\s*class.*
Explaining:
(?:^|\\n) # from start or new line
\\s* # as many as possible spaces
class # the 'class' text
.* # all characters till the end of line
Hope it helps.

command line with regular expressions java

Sorry for my English. How to pass arguments to the command line that used for example if I write the word in quotation marks "something" I find it a regular expression, and if the word without quotation marks nothing.
That's how I thought about do it. But this is not correct and not nice.
if(args[0].charAt(0) == ' " ' && args[0].charAt(args[0].length()-1) == ' " ') {
System.out.println("Regular");
}
Try using '\"' when you check for it, and use '"somestring"' in the command line (single quote to mark args
Also works for '"some string"' (works with spaces)

Need help understanding unexpected output from Java regular expression

package com.j;
public class Program {
public static void main(String[] args) {
System.out.println(Puzzel.class.getName().replaceAll(".", "/")
+ ".class");
System.out.println(Program.class.getName());
}
}
in the above program i was expecting a output com/j/Program.class
But it is coming //////.class its y?
In the replacement, . is treated as a regular expression, where . means "any character" and here is replaced with / , so the output becomes
////////////.class
For the expected answer, change the expression to escape the .:
Name.class.getName().replaceAll("\\.", "/") + ".class");
Then the output will be what you expected:
com/j/Puzzel.class
Because . is a special char when it comes to regex. You should escape it with backslash.
replaceAll() takes a regular expression for the matcher. Your code says to replace every character (.) with a /. you need replaceAll("\\.") or maybe replaceAll("\\\\."). I can never remember how many escapes to use offhand.

How to append a backslash in a string in java

I want to add a '\' character to every string in a list of strings... I m doing something like this but it adds 2 backslashes instead.
feedbackMsgs.add(behaviorName+"\\"+fbCode);
result is like: "abc\\def"
how to make sure a single backslash is added??
I've just run a program with the following -
String s = "test" + "\\" + "test2";
System.out.println(s);
And it prints out the following -
test\test2
Are you sure there is no \ in the behaviourName or fbCode variables?
Looks like either your behaviourName ends with a \ or fbCode starts with one.
Try to Log/print behaviorName fbCode and find it yourself !
System.out.println(behaviorName);
System.out.println(fbCode);

Categories

Resources