class parameter missing final quote - java

I have the following code
public class MyClass {
public MyClass (String myString){
myFlag=myString
}
ProcessBuilder pb = new ProcessBuilder("Path to my application", "variousflags", myFlag)
Process p = pb.start();
}
When I run
myClass("worddocument.doc")
pb doesn’t start. In debugging MyClass, I noticed that:
myFlag ="worddocument.doc
without the final quote. For a flag to work in the ProcessBuilder, it has to within quotes.
I have to include the line
myFlag= myFlag.concat(""");
which gives me the error message in Netbeans "unclosed string literal". How can get rid of the neatbeans error, or even better, how can I get the final quote back?
Thanks

stringFlag= stringFlag.concat(""");
will cause a problem since your ide thinks you close the String at the second quotation mark.
Try to escape the second quotation mark.
stringFlag= stringFlag.concat("\"");

The issue has disappeared now :-) It must have been a bug in netbeans or java...
Unfortunately, I can't reproduce the issue, and I am not sure exactly when it fixed itself.
If encountering the same problem, I would suggest:
1-restarting computer,
2-restarting Netbeans,
3-commenting out
myFlag= myFlag.concat("\"");

Related

VSCode not reading Cyrillic from console

Well, here is the problem, I have started using VScode, and I can't read from console cyrillic characters.
My code:
import java.util.Scanner;
class App {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in, "UTF-8");
String word = input.nextLine();
System.out.println(word);
}
}
Now when I enter any cyrillic string it will print empty string back to me. If i write something like
System.out.println("Привет"); //cyrillic symbols
It will print "Привет", which is fine. So I am guessing it has something to do with reading the string rather than outputing it.
chcp command gives Active code page: 65001
I have tried setting encoding and without it, but it doesn't seem to work, is there something I missed?
Thanks in advance
I've tested the code on my machine and got the same result: nothing shown;
You can see, when run it in external Window PowerShell or Command Prompt, the result is different but still not shown correctly:
When we change the encode style to GBK(936), the cyrillic characters can be displayed correctly:
When it comes to changing integrated terminal encoding style in vscode and execute code again, it still shows nothing:
About these different results between external Command Prompt and integrated terminal in VS Code, I've put a github request. And I'm doing some research, if any useful imformation i get, i will update you.

Reading in from Text File, Mismatch Exception (When reading in Float)

I'm having issues reading a Float in from an external data file (.txt)
I've read a number of different posts about this error, and common problems, and I still don't know what I'm doing wrong. The advice varies, some people say use ".nextLine()", some people say use ".next()", I've tried a bunch of these combinations and am still having problems.
Here's the relevant section:
public void read_file(Interface iface) throws FileNotFoundException {
int readCount = 0;
File file = new File(""); \\FILE PATH REDACTED
Scanner reader = new Scanner(file);
reader.useDelimiter(",|\\n");
while(reader.hasNext()){
String type = reader.next();
float rate = reader.nextFloat();
String desc = reader.next();
//Some irrelevant code follows
Text file:
Test1
10.00
Test2
I get the following exception:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextFloat(Scanner.java:2496)
at ReadFile.read_file(ReadFile.java:20)
at Main.main(Main.java:21)
I conducted a test and it definitely reads in the first line (I was able to print that). So I'm guessing its something to do with the line return at the end of the line after Test1. But I thought that would get handled by the use of the delimiter.
I'm using IntelliJ (some people have suggested that using a ,(comma) instead of a .(period) can sometimes resolve the error - but this seems limited to NetBeans or Eclipse, I forget).
Does anyone have any suggestions?
I think the line breaks you set in 'reader.useDelimiter()' should be consistent with the line breaks in the file. Generally, it is '\r\n' for Windows, '\n' for Unix, and '\r' for Mac.I hope it can help you.
Apparently, you must add this line before or after useDelimiter :
reader.useLocale(Locale.US);
It's required for the scanner to be able to detect floats.
For whatever reason, once it started working, I tried removing the line and it kept working. I reverted back to your original script, and it was still working. I think the Locale settings are persistent or something.
Anyways, if adding this line works, I suggest leaving it like that :)

How to pass hieroglyphs as an argument to the main method from command line

I met some interesting question on which I was not able to find an answer. Does anybody know how to pass hieroglyphs as an argument to the main method from command line?
Below there is some pseudo code which will help to test suggested solution:
public class Test {
public static void main(String args[]) {
if ("香港政府".equals(args[0])) {
System.out.println("Match");
}
}
}
So question is how to call Test.class with an argument so the application prints Match to the console? args[0] can be transformed before passing to the if statement.
Thanks in advance.
After some additional research I was able kinda figured it out. So guys who commented on question were very near to the answer.
Encoding which I tried to find was 936. But it doesn't mean that you will be able to run chcp 936 if you OS locale is other than chinese. Once you will try to run it on other locale than chinese:
chcp 936
You will get following error:
Invalide code page
For making it working you have to change region. FOllowing steps will be needed:
Start - COntrol panel
Select "Region and Language"
Select "Administrative" and click "Change system locale..."
Select "Chinese (Simplified, PRC)" and reboot laptop
After restart when you will run chcp you will see following output Active code page: 936. Now you are ready to execute command line with hieroglyphs.

A URL specified in a separate line in Java doesn't issue compile-time errors. Why?

Some days age, I mistakenly pasted a URL in my Java program in a hurry, I attempted to run that specific program and in my curiosity, it ran successfully with no warnings, no errors and no exceptions. The similar code is as shown below. I compiles and runs successfully.
final public class Main
{
public static void main(String[] args)
{
System.out.println ("A curiosity");
https://localhost:8181/OnlineShoppingCartSystem-war/Restricted/Home
System.out.println ("compiled Ok!");
}
}
At the second line within the main() method, it should issue some compile-time errors but it works well and displays the respective messages on the console. Why?
The tokens https: gets interpreted as a label. And the // that follows starts a new-line comment. So, the posted code continues to be valid Java (if you notice the syntax coloring).
Because // starts a comment and https: is interpreted as a label.
This is because it looks like this to the compiler:
https: is a code label
//localhost:8181/OnlineShoppingCartSystem-war/Restricted/Home - is a comment
notice that the code highlighter works here as well as in your question.

Syntax error on token "Invalid Character", delete this token

I am not sure why is it giving this error. Braces seem to be right. Another thing is that the same program works in Windows-eclipse but not in eclipse for Mac. What could be the reason?
import java.util.Vector;
public class Debug
{
private int something = 0;
private Vector list = new Vector();
public void firstMethod()
{
thirdMethod(something);
something = something + 1;
}
public void secondMethod()
{
thirdMethod(something);
something = something + 2;
}
public void thirdMethod(int value)
{
something = something + value;
}
public static void main(String[] args)
{
Debug debug = new Debug();
debug.firstMethod();
debug.secondMethod();
}
}
Ah, ok - it's probably a control-Z or other unprintable character at the end of the file that is ignored in Windows but not on the Mac. You copied the source from Windows to the Mac. Delete the last few characters and re-enter them - I think it will go away. I don't do Mac, though - I'm just guessing.
I had the same problem importing my projects from mac to linux Slackware.
Mac OSX creates some temporary files with the same name of the files in folders (._filename) in all folders.
Usually these files are invisible in Mac OSX, but in the other OSs no.
Eclipse can find these files and tries to handle like sources (._filename.java).
I solved deleting these files.
Only way i could resolve this problem was press Ctrl+A to select all text of file then Ctrl+C to copy them then delete file and create new class with intellij idea then Ctrl+P to paste text in new file. this resolve my problem and compiler never show error after do this solution.
It can happen when we copy and paste .It happens when there may be some character which is unrecognized in one platform but recognized in other.
I would suggest don't copy rather try to write the entire code by yourself. It should work
I got the same error when I imported a project I created in a Mac, to Windows. As #Massimo says Mac creates ._filename,java files which eclipse running in windows consider as source files. This is what causes the problem.
They are hidden files, which you can see when you select the option, "Show hidden files and folders" under folder options in Windows machine. Deleting these files solves the problem.
I got this message trying to call a subjob from a tRunJob component. In the tRunJob I had both checked "transmit whole context" AND listed individual parameters in the parameters/values box. Once I removed the additional parameters it worked.
There are probably hidden characters in the line. If you move your cursor through the characters and your cursor doesn't move in one character, that means there is an invalid character in the line. Delete those and it should work. Also try copying and pasting the line to an hex editor and you will see the invalid characters in it.
i face this problem many times in eclipse . What i found is that select all code - cut it using Ctrl + x and then save the file and again paste the code using Ctrl + V . This works for me many times when i copy the code from another editor.
I also faced similar issue while copying the code from one machine to another.
The issue was with Space only you need to identify the red mark in your eclipse code.
On Windows, if you copy the source to Notepad - save the file (as anything), ensuring ASCI encoding is selected - the character will be converted to a question-mark which you can then delete - then copy the code back to Eclipse.
In eclipse right click on the file -> Properties -> resources
In Text file encoding select US-ASCII
This way you will see all the special char, you can then find & replace
And then format the code

Categories

Resources