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()
Related
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);
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
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 5 years ago.
Improve this question
I am wondering what will be the best approach for word case and word delimiter in JSON
considering that later on the JSON file will be converted to a Java Pojo and i would like this Java Pojo to be in ProperCase
I am debating between:
"sectionSuspensionTiresSteering": [{
"SectionSuspensionTiresSteering": [{
"section_suspension_tires_steering": [{
"section Suspension Tires Steering": [{
I don't really think that JSON has a naming convention, so you can "choose" sort of speak. As I have a Java background, I prefer using camelCase (your first option). I would avoid using blank spaces in the JSON keys, it is allowed but causes problems because most of the existing framework aren't able to deal with it.
So you are free to use the style you want. Regarding the conversion back to Java Pojo, this is just a matter of annotations. When you use for example Jackson, it allows you to annotate your fields in order to convert the JSON file back to a Java Pojo.
Use always the first one:
"sectionSuspensionTiresSteering": [{
Examples of similar files in official guides:
https://spring.io/guides/gs/authenticating-ldap/
As you can see here, the gradle file uses this notation and XMLs files too.
I understand that JSON properties are written in camel case (sectionSuspensionTiresSteering) style.
But it has nothing to do with how the property will be written or coded in java.
For example if you use Gson() to do the conversions between java an JSON you can name the java property whatever you want and annotate the property with #SerializedName("jsonName") passing in the JSON property name.
What I mean is that the two names are not coupled.
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 have recently started using JSON and was wondering if I am missing any other important reasons as to why I would use a JSON object instead of just returning a large string of data.
Here is what I have found so far:
A JSON object is a lot faster to process, easier to handle and parse. A JSON object is easier for a human to read compared to a big string of data output. JSON objects can be mapped easier and works well with object oriented systems.
JSON is just a [large] string of data.
The difference from a one-off/custom "blob'o'test" encoding is that JSON is a well-defined format that supports common ADTs (Arrays, Maps) and is a useful interchange format. Also, one doesn't work with JSON (which is just text) directly; one works with the object-graphs that are serialized to/from JSON - e.g. once you call JSON.parse(jsonText) you're dealing with regular objects.
While XML is another well-defined format, JSON has a better 1-1 mapping with simple object-graphs. This easier mapping eliminates the need for a specific DOM wrapper or other specialized access - who wants to deal with an object model when one can treat an object-graph as first-class data?1
The fact that JSON (which is just text) also looks like normal JavaScript Object Literal Notation (and excluding some odd Unicode issues, is a subset) makes human consumption relatively easy and has greatly helped the adoption.
Refer to the following questions for additional insight on "What?" and "Why?"
What is JSON and why would I use it?
What is the exact use of JSON?
Why use JSON instead a normal html output with AJAX?
Why is it a bad practice to return generated HTML instead of JSON? Or is it?
https://stackoverflow.com/questions/3536893/what-are-the-pros-and-cons-of-xml-and-json
Why is Everyone Choosing JSON Over XML for jQuery?
1 XML is much more than a simple markup format, but comparing XML to JSON in any more detail is outside the scope of the question.
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.