How come this works:
lA = new List(List.ORDERED, List.ALPHABETICAL);
lA.setLowercase(List.LOWERCASE);
lA.setPostSymbol(") ");
but this doesn't:
lQL = new List(List.UNORDERED);
lQL.setListSymbol("=");
lQL.setPostSymbol(" ");
?
In the first example in front of every item is "a) " or "b) " or "c) " etc...
In the second example in front of every item is only "=".
Before any ideas, two things. Firstly, I can't do it this way: lQL.setListSymbol("= ");. I could explain it, but just go with it, it's simpler. Secondly, I tried setting second string to "k " (so it's not only spaces), but the output was still "="...
What's happening?
Because you have explicitly indicated that you want an unordered list with List.UNORDERED. Thus, you'll not get any order for your items, and therefore the post symbol will not be used.
From the documentation of setPostSymbol:
Sets the String that has to be added after a number or letter in the
list symbol.
And from the source code of List:
137 /**
138 * In case you are using numbered/lettered lists, this String is added after the number/letter.
139 * #since iText 2.1.1
140 */
141 protected String postSymbol = ". ";
This number or letter will only be added for ordered lists, i.e., List.ORDERED. If you inspect the first argument of the constructor of List, you'll see that it receives a boolean to indicate if the list is numbered or not. Since you are passing it List.UNORDERED, whose value is false, you won't get a numbered list, and thus, postSymbol will not be appended.
To set or unset number ,letter or bullet use the following single code for List with usage of iText pdf out or simply with list.
For example,for List with no bullet or string or number usually required in printing bills.
List list = new List(false, false, 50);
list.setListSymbol(" ");
Thank you for your queries..
Related
im working on a problem where i have to obtain all permutations of an arraylist of numbers. The only restriction is that any number cant start with 0, so if we have [0,1,2] we would obtain
[1,2,0]
[1,0,2]
[2,0,1]
[2,1,0]
i know how to do this with 3 loops but the thing is that i have to repeat this to different sets of numbers with differentes sizes, so i need one method that i can apply to different sets of numbers but i have no clue on how to do this. I imagine i have to used some kind of recursive function but i dont know how to implement it so the numbers cant start with a 0. Any ideas? please dont just post the code i want to understand the problem, thank you in advantage!!
Curious question! Interesting code kata.
I naively think I would have a recursive method that takes:
a list of the items currently chosen by the caller
a set of the items available for the callee
The method would iterate over the set to chose 1 more item and call itself with the list extended by this item, and the set reduced by this item. Upon return, remove from list, add back to set and go on with next item (take a defensive copy of the set of course).
If the current list is empty, the selected first item cannot be 0, as per your rules. If you must collect the permutations somewhere (not just print), a 3rd argument would be required for a collection or an observer.
The recursion obvioulsy stops when the available set is empty, at which point the permutation is sent to the collection or observer.
If items can repeat, you may have benefit from sorting them first in order to skip electing the same item again at a given position.
Beware this quires a recursion depth of N, for N items. But the danger is minimal because even with N=10000, it may not stackoverflow, but the CPU time to complete would be order(N!) (probably end of universe...)
You could solve this recursively as described here: Permutation of an ArrayList of numbers using recursion.
The only thing that is missing there is your restriction with the zeros, which could be solved somehow like this (the loop is taken from the example above):
for (List<Integer> al : myLists) {
// The part you need to add:
if (al.get(0) == 0) {
continue;
}
String appender = "";
for (Integer i : al) {
System.out.print(appender + i);
appender = " ";
}
System.out.println();
}
You basically check the first element of each permutation and skip the ones with a leading zero. The continue jumps to the next iteration of the loop and therefore to the next permutation.
I am new to Java and using the JIRA MISC Custom Fields add-on and require some logic assistance to solve math functions between two drop down fields.
Field one is "User Cost"
This field contains four string selections with the user price posted at the end of the string.
sam costs .21
mitch costs .419
Lance costs 2.66
xmen costs 13.338
Field two is "Usage"
This field contains two string selections:
24 hours (unless maintenance)
12 Hours (7a-7p)
The argument should be invoked into a new field called "Total User Cost." This field would automatically display the correct price for user and usage amount.
The equation blueprint would be as follows:
Cost*31(calendar days)*usage(12 || 24)
I would want my form to update based on user input selection of these two fields and other variables in my equation.
This is what I have so far:
[
Thank you in advance for any feedback!
If I understand correctly, you first need to initialize issue to something. (It looks red in your images like that variable doesn't exist)
Then, you can do something like this
double costSam = 0.21;
String userSam = issue.get("customfield_10620");
Then, if you are needing to convert or otherwise do some math on userSam, then you need this
double samTotal = costSam * Double.parseDouble(userSam);
Some flaws with your code
You have to define types for your values like String or double.
If you have String x = "hello" on one line, then x = 0.4 on the next, that won't work because of incompatible types
If you did have compatible types on consecutive lines, then the first line is pointless unless using the value from the first, as the second line overwrites the value of the first one
I have two Arraylists, one contains an entire list of objects, the second contains objects to remove from the first list.
When I remove the objects from the first list and when I output those objects to a file using a Formatter, nothing is written to the file. However if I output the objects from the first Arraylist, without removing any objects, all of those objects appear in the file.
For example:-
for(Invoice inv : tempStore)
{
if(invoiceLines.contains(inv))invoiceLines.remove(inv);
}
//for each invoice in the ArrayList
for(Invoice invoice : invoiceLines)
{
output.format("%"+this.spec.getLength("XXXX")+"s\t",checkString(invoice.getInvoiceDate()));}
gives me no output, however doing just:-
//for each invoice in the ArrayList
for(Invoice invoice : invoiceLines)
{
output.format("%"+this.spec.getLength("XXXX")+"s\t",checkString(invoice.getInvoiceDate()));}
gives me output information, when manually debugging the application the arraylist (the one with the objects removed), does contain objects still and those objects contain the correct values. It's almost as if the Arraylist, once objects are removed is losing the pointers in memory.
Any ideas? Unfortunately I can't give much in the way of specific code, however ask any questions and I will try to answer as best as I can. The language is Java and I'm using Java compliance 1.5 in the SDK.
To identify the error you can add more output (as console output or using a Logger) on the removing operation:
System.out.println("Items before removal: " + invoiceLines.size());
for(Invoice inv : tempStore)
{
if(invoiceLines.contains(inv)) {
invoiceLines.remove(inv);
System.out.println("removed " + inv + ". Items left: " + invoiceLines.size());
}
}
If there are more than items removed during one iteration, you want to check the equals() and hashCode() Implementation of the Invoice class.
If you have your own implementation, make sure you followed the Object.equals()-Contract.
Formatter.flush() and Formatter.close() need to be called at the end of output.
I'm trying to add 1 to an integer in a 2-dimensional ArrayList.
I'm using the set() method with the element + 1 as the second argument, but the "+ 1" isn't working. When I retrieve the element, it defines it as an object, not an integer. How do I get around this?
Code:
ArrayList<ArrayList> inventoryList = new ArrayList(
Arrays.asList(new ArrayList<String>(), new ArrayList<Integer>()));
...
(inventoryList.get(1)).set(i, ((inventoryList.get(1)).get(i) + 1));
Error:
Main.java:47: error: bad operand types for binary operator '+'
(inventoryList.get(1)).set(i, ((inventoryList.get(1)).get(i) + 1));
^
My code is at this ideone page. This code is translated from python and I'm currently debugging it so don't worry about the other errors.
ArrayList<ArrayList> inventoryList = ...
You are using the raw variant of ArrayList for your inner lists, such an ArrayList indeed contains Objects instead of Integers. You shouldn't use those raw ArrayLists and instead use generic ones:
What is a raw type and why shouldn't we use it?
Looking at your code a bit more, it seems that inventoryList is supposed to contain two lists, one that contains the items you have (as strings) and one that contains how many you have (as integers) where you can find how many you have of the item at index i in the first list by looking in the second list at that same index i.
If that is correct there are multiple ways to fix this, indeed, casting the Objects to Integers works, but then you are still using raw types, which you probably shouldn't. To fix this you should just not keep the ArrayList<String> and the ArrayList<Integer> in the same list. You could just have:
ArrayList<String> inventoryItems = ...
ArrayList<Integer> inventoryItemCounts = ...
separately (you don't need a list if it always contains exactly 2 items, a list of strings and a list of integers). However a cleaner solution would be, as was suggested in the comments by user2418306, to use a map
Map<String, Integer> inventory = ...
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html, that way each string (item) in your inventory has exactly one corresponding integer (number you have of that item) and you don't have to get that by using the "at the same index" trick.
Looking at you code a bit though, I would say that more is going wrong with the inventory. You print your inventory using:
for (int i = 0; i < inventoryList.size(); i++){
System.out.println((inventoryList.get(1)).get(i) + " : " + (inventoryList.get(0)).get(i));
}
and you iterate through it in that way in other places as well. However, if i'm not misunderstanding anything, inventoryList.size() is always going to be 2 (the inventoryList contains 2 lists, one of strings and one of integer). To get the number of distinct items (strings) in your inventory you'd have to do inventoryList.get(0).size() (or inventoryList.get(1).size() because that is going to be the same). However, things will get easier if you chose a better datatype for your inventory. I would look into the mentioned Map. Using that, you easily get the correct number using inventory.size().
You can solve your problem by casting to Integer
inventoryList.get(1).set(i, (Integer) inventoryList.get(1).get(i)+1);
Of course first take a look on comments below your question to see how to properly init your list, so you wont need explicit cast.
The way it is declared, your lists are list of Object. You need to cast the result from second get() with (Integer), like:
inventoryList.get(1).set(i, (Integer)inventoryList.get(1).get(i)+1);
Note: There are unneeded ().
I'm operating on a file that has this kind of format:
LAWS303 RHLT1 10 84 AITKEN WU
LAWS314 RHLT3 15 2 PARADZA VISSER
LAWS329 EALT006 6 62 AITKEN WILSON
LAWS334 HMLT105 2 43 ANDREW INKSTER
LAWS334 HMLT206 2 62 JULIAN YOUNG
LAWS340 RHLT1 11 87 AL YANG
The goal of this program is that for each day (the third column) of the month, each course code (first column) should be printed along with the total number of students attending (fourth column) for the course on that day. From my thinkering, this involves either reading the file a lot of times (ew!) or loading the three salient values (day, course, headcount) into some sort of array and operating on that instead. Despite being fairly familiar with what a multi dimensional array is, this one has repeatedly caused my head to implode. I've got the pseudo code for this program written out in front of me and my mind draws a blank when it comes to the line that defines the array.
The dayOfMonth can remain a string because it'll only be compared to another string. The courseCode obviously needs to be a string, too. However, the headCount ideally would be numeric; it'll be added to as each line of the file is processed. The relationship between the three is basically that there can be many courseCodes per dayOfMonth, but only one headCount per courseCode as I'll be adding to it as I read it all into the array.
So, in derpspeak, this is how it should roughly look:
{String dayOfMonth = {{String courseCode}, {int headCount}}}
The two issues I have here, are...
a) that I'm not sure how to actually code this kind of funky array in there and
b) as I can't really wrap my brain around it to begin with, there's a really good chance that I've essentially just designed something completely wrong for what I need. Or impossible. Both?
For example, the array will start off empty. I'd want to add a dayOfMonth, courseCode and headCount to start it off. But I couldn't just go array.add(dayOfMonth) because it's expecting an array, leading me to suspect I should be using something else. Argh!
Oh god my brain.
This looks like homework, so my answer will consist of hints.
Hint #1 - there are some entities in those rows. Work out what they are and write a class for each one.
Hint #2 - Use List types not arrays. Arrays have to be preallocated with the right number of elements.
Hint #3 - Use Map types (e.g. HashMap or TreeMap) to represent mappings from one kind of thing to another kind of thing.
If you want to store and retrieve the values then use #Stephan C's input.
Here is code snippet to print values using sysout. You can modify to hold the values as you wish.
BufferedReader reader = new BufferedReader(new FileReader("< your file here >"));
String string = reader.readLine();
while (string != null) {
StringTokenizer tokenizer = new StringTokenizer(string);
String print = "";
if (tokenizer.countTokens() > 4) {
print = tokenizer.nextToken();
tokenizer.nextToken();
print = tokenizer.nextToken() + " " + print;
print = print + " " + tokenizer.nextToken();
}
System.out.println(print);
string = reader.readLine();
}