How to pass empty args to Netbeans [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I tried to pass blank command line arguments in a Netbeans Java project using Properties-->Run-->Arguments with "" or '' but nothing.
I have some arguments that some times have to be empty or null.

The common way to handle that is to not specify arguments when they are blank or empty. It is quite the norm in the CLI/scripting world to work like this. Unspecified arguments mean either to use the defaults or no value.
Aside there are Java nice libraries to help you parse your program options or arguments, such as:
Args4j http://args4j.kohsuke.org/
Apache CLI http://commons.apache.org/proper/commons-cli/
EDIT
Additonal remarks:
Without a smarter options/arguments parser, it is not possible to handle empty arguments properly (i.e. just by setting a blank, whitespace or quoted empty string). The only way to achieve that would be to define a keyword or special character in order to identify empty/unspecified arguments when processing args[].
This behavior is not bound to Netbeans. Rather it relates to how Java parses the varargs of the main() method from the command-line.
Interesting links:
What is "String args[]"? parameter in main method Java
How to handle empty parameters in a main method java call

Related

Creating a simple calculator using strings in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to learn Java. My current assignment is to build a simple four function calculator..... this would be easy given if/else and/ or switch statements, but I'm supposed to build this using methods.
The original input has to be put in as a single string, so, in my mind, I'm going to have to take the single string and create substrings, then somehow convert these substrings into double values, while deleting whatever whitespace could possibly be between characters. My current idea is to somehow identify the "+,-,*, or /" within the string and divide into substrings before and after these values, using the appropriate defined method for whichever operator to do the calculations....
The problem is that I can't see a good way to divide these up into substrings or how to convert the numbers involved into double values. Anyone got any advice for me? Keep in mind, what we have gone through is pretty limited and I feel like I'm missing something REALLY simple out there.
You can split a string based on a particular character using str.split("\\+"), for example. You can convert the split pieces of the string to doubles by using Double.parseDouble(str);

Need of println command in java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In java language we use two print method. One is "print" and second is "println".
But why we need an extra command "println" if there is only one difference is that it insert a new line in last but we can use a new line character with print command like we use in c/c++.
Why an extra command added in java.
I m not sure about my answer, but maybe it is because if we have to give new line by ourselves in c we give printf("\n"), here the problem is that for every operating system new line is different.
For example:
in windows - "\r\n"
in unix - "\n"
in mac - "\r".
So in order to save the developer from remembering this println is used.
edits
for mac's new line feed refer this discussion- https://superuser.com/questions/439440/did-mac-os-lion-switch-to-using-line-feeds-lf-n-for-line-breaks-instead-of
Note as suggested in below comment by Honza Zidek:
1. Java also has printf() method and this one accepts a control sequence %n which is replaced by the proper end-of-line for the target OS.
2. There is a method System.lineSeparator() returning the proper end-of-line marker. See How do I get a platform-dependent new line character?

Is a multiline toString() output discouraged? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
The javase docs state:
toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
When handling an object with multiple parameters is it good practice to format the output to display nicely formatted in the console? The default eclipse generated implementation concatenates the variables in a single line which gets troublesome to read, needing to scroll back and forth multiple times and does not group the parameters logically.
I would like to format the output inserting line breaks, but have never actually seen someone doing this before. Will I eventually run into issues with loggers or anything else or is it perfectly fine to format the output of toString()the way I want?
Is it better to implement a additional method toStringPretty()?
Usually it is not discouraged to avoid producing multi-line string from toString().
as java doc said it should be concise and informative but if you find yourself in a situation where lots of field should be formatted and represented in log file, notice that nobody looks for pretty log. the log should be searchable with regex to facilitate the finding what you are looking for.
What i can suggest you is that instead of formatting multi-line for readability in log file, print your elements in a single line Json format which is both easy to read and search, but if you what to show an output on console to the user it is better off to extract the formatting logic outside of the toString()

Using StringTokenizer to do This [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to use StringTokenizer to search through a string, and if they find this two words in the string, they will print "Yes.", if not, it'll print "No."
These two words are "eat", and "yet", and the string is "Did you eat yet?"
If it finds both words in that string it's suppose to print out yes, and if not, it's suppose to print out no. I have no idea how to do this. If you do, then please help.
StringTokenizer st = new StringTokenizer("Did you eat yet?");
This is how you initialize a StringTokenizer. Then your implementation should use the .hasMoreTokens() method to step through the tokens and check if they are equal to the words you are searching for.
Not sure why would want to use this method however, as..
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
From the documentation at http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html
As others have suggested, using StringTokenizer is discouraged and in this case is over complicating the procedure in the first place.
Getting familiar with the Java String class is the right start. Here we find it's possible to determine if a string contains() the target string with yourString.contains(yourSubString).
You can combine this with another call to contains() in a conditional with...
if (yourString.contains(someSubString) &&
yourString.contains(someOtherSubString)) { ... }

How do you add JTextFields? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a GUI in Java, and I was wondering how do you add user inputs (integers) in a JTextField? If so, please post with some code so I can understand. (I am just a beginner programmer)
What all you need to do:
jTextField.setText(String.valueOf(intValue));
Because JTextField takes a String to set its value you have to convert the int to String.
See This for more details.
A JTextField contains text. You get this text using textField.getText(), which returns a String. This String might happen to represent a valid integer. If you want to get the value of the integer, you need to transform the String into an int. That's done using the Integer.parseInt() method:
int sum = Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText());
Of course, if one of the text fields contains text that doesn't represent an integer, you'll get an exception, as documented in the Integer.parseInt() javadoc.
Given your question, it seems you don't really understand basic notions such as types, and basic classes such as String and Integer. My advice would be to forget about Swing for the moment, and exercise with basic programs not involving any UI.

Categories