Alternative to printing a list in a text box? - java

Hi i'm using a GUI and at the moment printing a list to console. I understand you can print strings with text boxes using textBox.setText("") but instead of printing my list to console I want to use a text box or any other alternative.
At the moment ive got this as my printlist method:
private void printList(){
System.out.println(myList);
}
After attempting to do:
System.out.println(resultsBox.setText(myList));
I realize, this will not work as it only works with strings not lists. So yeah, what else could I use?
Thanks in advance

Do this to convert your list to a String,
if(myList!=null) {
resultsBox.setText(myList.toString());
}
and if there's a valid getText(), you can test the value that you set by printing it,
System.out.println(resultsBox.getText());

You just need to call
resultsBox.setText(myList)
separately.
Notice that this is a method call by itself that sets the values in the box. It does not return a value so you cannot pass it as an argument to println().

Related

I am trying to use the isEmpty() method and I keep getting an error saying: "Cannot resolve method 'isEmpty'. "

this is my click event. The code is supposed to extract entries from text controls and use it to make calculations. At this stage I m trying to make sure that the user enters all of the values that are required, that is: marks for assignments 1, 2, 3 and the exam mark. I realize that I had not entered the "!" to indicate that the controls should not be empty
I believe you wanted to do something like this:
Double assign1;
String stringAssign1 = mark1.getText().toString();
if (!stringAssign1.isEmpty()){
assign1 = Double.parseDouble(stringAssign1);
}
Firstly, you should check if string is empty (also as mentioned before isEmpty() is string function), then if not, parse it to double.
Next time please use codeblock instead of screenshot of code :)

RecyclerView Adapter Map instead of Arraylist [duplicate]

Edit: Figured it out, check my posted answer if you're having similar issues.
I know there are several questions about this issue, but none of their solutions are working for me.
In my model class I have made sure to use List instead of Arraylist to avoid Firebase issues, but am still getting this error. It's a lot of code but most questions ask for all the code so I'll post it all.
TemplateModelClass.java
//
I've used this basic model successfully many times. For the
HashMaps<String, List<String>>,
the String is an incremented Integer converted to String. The List's are just Strings in a List. Here's some sample JSON from Firebase:
//
Formatted that as best as I could. If you need a picture of it let me know and I'll get a screenshot
And am getting this error, as stated in the title:
com.google.firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.util.ArrayList
The most upvoted question about this seems to have something to do with a problem using an integer as a key, but I think I've avoided that by always using an integer converted to a string. It may be interpreting it strangely, so I'll try some more stuff in the meantime. Thanks for reading!
Alright, figured it out. If anyone reading this has this problem and are using incremented ints/longs/whatever that get converted to strings, you must add some characters to the converted int. Firebase apparently converts these keys back into non-Strings if it can be converted.
For example, if you do something like this:
int inc = 0;
inc++; // 1
map.put(String.valueOf(inc), someList);
Firebase interprets that key as 1 instead of "1".
So, to force Fb to intepret as a string, do something like this:
int inc = 0;
inc++; // 1
map.put(String.valueOf(inc) + "_key", someList);
And everything works out perfectly. Obviously if you also need to read those Strings back to ints, just split the string with "[_]" and you're good to go.
The main issue is that you are using a List instead of a Map. As your error said, while deserializing it is expectig a Map but is found an ArrayList.
So in order to solve this problem youd need to change all the lists in your model with maps like this:
private Map<String, Object> mMapOne;
After changing all those fileds like this, you need also to change your public setters and getters.
Hope it helps.

Fill arrays with data from block of string in java

I want to fill 4 arrays with specific data from block string
I got blocks like this
00:0035:0063:1705211023:00:
11::7027661000300976376:
99:59:07027661000300976376:::::
05:11:10000:::00 09:11:8510 07:::::1490:::
99:65:00:00:00:00:00:01000000000002140331410062269000000126300000000
99:64:00:00:00:00:00:00000355600200000000022700000000000000000001
99:01:227:1490:30:0:0:0:0:0:1:0:324
****Segundo Ticket PANGUI**** 99:00:35:63:1705211023:0:1:19353:63895896:1490:0:::::
99:150:0|1|H014|35|63|210517102100|
and I want to check if 00:.. , 05:11:.. , 99:65.. , 99:64... and 99:01...headers exists and stores data for specific field from each row, for example in line or row 99:65.. I will store the last field. If no exists one or more, I must be store zero, something like this
if exist 99:11 then Arr11 =specificfieldfrom9911, else arr11 = "0"
So that for each block have a structure or set of arrays that identifies the fields of each block
Arr00
Arr0511
Arr9965
Arr9964
Arr9901
how can I achieve this? any help would be great.
after you get the individual string you can use startsWith("") method on the strings. Eg. Assuming the string that came is on a variable called inputString you can use
if(inputString.startsWith("99:65")){
//do what you want to do
}
i hope this helps

Writing things in labels to the next line(JAVA)

I am using JLabels and I want to write something like this.
"A" after writing "A" goes to the next line and writes "B".After that writes a variable coming from a method
I can accomplish writing "A" and "B" like that with the following code
JLabel label105 = new JLabel();
label105.setText("<html>Gas Company</html>");
But when I try to insert an integer value from system to set text I fail.Either it writes to the same line or it doesnt work at all.Can anyone write how can I use setText to accomplish this?
Basically what I want is the following in labels.
System.out.print("A");
System.out.println("B");
System.out.println(getValue());
Try this
String string="<html>A<br />B<br />"+Integer.toString(intValue)+"</html>";
label105.setText(string);
The variable intValue is your integer value

How do you handle incomplete data in a Processing table?

I'm parsing a CSV using Processing's Table interface, but some rows are missing some data. I want to pull all the data available into my table, but I'm not sure how to handle the missing data--I keep getting NullPointerException when I loop over the table with dataTable.getInt on the missing values.
I don't have a background in statically typed languages, so I've no idea how to conditionally assign this data short of putting a separate try/catch around each assignment. Surely there's a better way?
Before calling dataTable.getInt method check if dataTable is not null like
if(dataTable != null) {
int my_nt = dataTable.getInt
}
//else skip since it is empty
Since your're using getInt--you should perform a regex search/replace ,<not numeric>, with ,<some int>,. In your case it may be as simple as replacing ,, with ,0,
Also, as Hassan suggests, double check that dataTable is not null.
Ok, so I figured out a way to do this:
First, call dataTable.makeNullEmpty(), which turns all the null values into empty strings.
Then, you can use a pattern like this:
String total_value = dataTable.getString(i, 4);
if(total_value.length() > 0) s.total_value = parseInt(total_value);
and you get assignment only if an int is there to be parsed.

Categories

Resources