I have a HashMap with multiple values at runtime. In that map one key has empty value , how to avoid this value to add in a list. some code sample is below:
public HashMap getLoop2Map(Map map, String xslFile , int sheetNo){
HashMap hashMap = new HashMap();
try{
ArrayList list = new ArrayList();
System.out.println("-------Map : " +map);
list.add(map.values());
//System.out.println("------- boolean : " +val);
System.out.println("------List : " +list);
}
I do not want to add the empty value in a list ...
result in map
-------Map : {Free Text Entry={},
Mouth / Throat={Free Text Entry={Free Text Entry=<FORMFILENAME>EditChartPhysicalExamText.form
</FORMFILENAME><TAG>"<MUSCULOSKELETAL.PE>"</TAG>},
Salivary Glands Condition={Salivary Glands Condition=MouthAttribute},
Examination Overview- Mouth / Throat={Examination Overview- Mouth / Throat=MouthAttribute},
Tonsils={Tonsils=MouthAttribute},
I have been playing around with my code, but how to avoid NullPointerexception.. So my program's point is simple, use Jsoup to scrape html of certain webpage, then i search the things i want and print them out. Problem is, when scraped html doesnt contain even 1 thing on my search list, i get NullPointerException... i understand why, Heres part of my code:
Java Code:
//Things i need to search from html String[] MySearchArray = new String[]{"138","146","474"}; //Search things contained in MySearchArray and print them out for (String Ml : MySearchArray) { Element flights = doc.select(String.format("tr:contains(%s)", Ml)).first(); Elements flights2 = flights.select("td"); System.out.println(flights2.get(4).text() + " " + flights2.get(0).text()+ " " + flights2.get(3).text()); } mh_sh_highlight_all('java');
I'm struggling with that piece of code, my intention is to check for the object I want to add before adding it, so there won't be any duplicate on my list. I'm not sure how could I do that, since I'm working with objects.
Person is a class with few parameters such as id, name, and few others.
I guess I should search for a person with the same id, since that has be unique, but can't get it right.
private ArrayList<person> model= new ArrayList<>(); //... if (model.contains(person))throw new IllegalArgumentException("duplicate"); else model.addElement(person);
I wanted to make a small program to move a small rectangle by pressing the WASD keys. The program works, except that when I hold a key to move the rectangle, after a second, auto-repeat starts up, and the rectangle motion accelerates. I want to prevent automatic repeat to activate, so that the rectangle moves at a constant speed when I hold a key and stops when I released. Here is the ButtonMotion classe :
I have code which query value from database,the use case is the user can enter value as 1,01,11 in database but when the user enter value in xml file he can only enter 11,01,12 in database there is two columns lets say column test1=1 and test2=2 combination of this is 12,which is the value the use will enter in xml, but sometime the use can enter test1=04 than column test2=00 ....
I'm using EclipseLink, WildFly, EJB, postgresSQL and JSF.I'm trying to persist some countries and their localities.So I've:
- Two entities Countries and Localities in which I specify respective columns and relations. - Abstract Session beans for entity classes: AbstractFacade providing basic crud methods and entity manager. - Two concrete session bean for entity classes: CountriesFacade and LocalitiesFacade. - A JSF managed bean named geoJSF. - A JSF page with a form allowing to insert new country and localities.
In geoJSF I'm injecting via EJB CountriesFacade as property named cf and LocalitiesFacade as property named lf.For the actual inserting country and locality I'm using geoJSF.country and geoJSF.locality. When the form is submitted I'm simply doing:
this.cf.create(this.country); this.locality.setCountry(this.country); //<- this throw an exception (unique constraint violation) due to the attempt to reinsert this.country this.lf.create(this.locality);
I disabled all cascade among relations definitions.Based on what I know this.country should appear detached to entity manager so, setting relation the entity manager try to re-persist it.
The method I am trying to successfully write is startsWith(String s1, String s2)
The intention of the code is to return true if s2 starts with the string s1, and false otherwise. For example,
x.startsWith("Maplestory","Maple"); Should return true.
This code partially works, depending on the values entered into it. When it is supposed to return true, it does do this. When it is meant to return false, I get a StringIndex OutOfBounds Exception.
public class CC02{ String remove1(char c, String s){ String to_return=""; while(true){ if(s.equals("")) return to_return;
[Code] ....
The method I have written uses one other method from my code, which is "reverse". Any way to return false without getting this error.
I'm writing an IRC server in Java, however when a client tries to connect I get a ConcurrentModificationException trying to add them to an ArrayList. My code is Here: [URL]
I don't understand why I am getting a ConcurrentModificationException or what one is.
I have a practice program (written with NetBeans IDE 7.4) that calls methods to:
(1) fill an array with user input values
(2) sort the array values into ascending order
(3) print the array in assorted order.
(4) print the array without any duplicates
Method (4) prints the contents of the array without printing any duplicates. That is, if a number in the array has already been printed, it will not be printed again.
Method (4) seems to work, but may be inefficient or I may have done too much work making it difficult on myself or making the logic too confusing.
Is there a better way to do this WITHOUT using built-in functions in Java's library?
(Such as writing this method using for loops and counter variables: As practice I am supposed to avoid using Deleting functions for duplicates)
Here is my code:
public static void Duplicates(int [] array){ int duplicates = 1; String Output = ""; for(int i = 0; i < array.length -1; i++) { if (array[i] != array[i+1]){ duplicates ++;
I want to create a ScrollPane where I can scale the contents. Think of a simple drawing program that allows you to zoom in and zoom out.
If I create a ScrollPane in SceneBuilder and then drop a couple of Circles into the AnchorPane all looks good.
If I then set a scale of 0.5 for the AnchorPane Scale X and Scale Y the AnchorPane gets scaled about its centre leaving space between the left and top edge of the AnchorPane and the left and top edge of the enclosing ScrollPane.
The only way to get the AnchorPane so that it aligns with the top left of the ScrollPane seems to be to add a negative amount to Translate X and Translate Y.
In my application I would like the user to be able to adjust the zoom (Scale) continually and also the size of the AnchorPane must be allowed to change as the user add/deletes/edits content.
Does this mean I have to continually monitor the AnchorPane properties and set compensating Translate X and Translate Y values each time? Or is there an easier way to achieve what I want?
We have developed a java application, whose primary objective is to read a file(input file), process it and convert it into set of output files. (I have given a generic description of our solution, to avoid irrelevant details).
This program works perfectly fine when the input file is of 4 GB, with memory settings of -Xms4096m -Xmx16384m in a 32 GB RAM
Now we need to run our application with the input file of size 130 GB.
We used a linux box with 250GB RAM and with memory setting of -Xms40g -Xmx200g (also tried couple of other variations) to run the application and hit OutOfMemory Exception.
At this stage of our project it's very hard to consider redesigning the code to accommodate hadoop ( or someother large scale data processing framework), also the current hardware configuration which we can afford is 250GB of RAM.
Any ways to avoid OutOfMemory Exceptions, what is the general practice when developing these kind of applications.?
@ManagedBean @SessionScoped public class HelloBean { public void downloadFile() { File file = new File("C:datacontacts.doc"); HttpServletResponse response = (HttpServletResponse)
1) productType that have name and price of the productType and an empty consturcor. 2) superMarket that have name of supermarket and arraylist of productTypes (
private ArrayList<ProductType> products;
) and an empty constructor.
In each class I have function that get input from console and should store it into each class variables.
In productType i have function:
public void getFromUser() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter product name:"); name = br.readLine(); System.out.println("Enter price:"); price = Integer.parseInt(br.readLine());
[Code] ....
The main is something like:
public static void main(String[] args) throws IOException{ SuperMarket s1 = new SuperMarket(); SuperMarket s2 = new SuperMarket(); s1.getFromUser(); }
The problem is when i get to line "products.add(i,p)" I get java.lang.NullPointerException
In the debug mode I can see that when I get to this line the "products" is null.
why do you think this happening, when I do "new SuperMarker()" in the main it should run the empty constructor and create new arraylist...
I've been beavering away with Java for a few months. But as with all languages the String implementation looks designed to trip up even experienced programmers.
My current development gets data from various sources outside my control. When I get a string I want to test if it is empty/null/or whatever. Simple enough one thinks.
But if you search the internet you see everone seems to have a slightly different approach. So what is the best way of determining that a string is not useful to you?
I've had success with this
if(string == null || string.length() == 0)
But I've seen people using methods - not necessarily of String (e.g equals, empty) and regular expressions.
What is the best approach to this considering coding efficiency and/or processing efficiency (accepting you'd have to be processing a lot of strings for the latter to be an issue).
im trying to make a gui im trying to add a new jbutton for every empty cell in the array, and for some reason its giving me array index out of bounds error, this is what i have, im trying to to do it in an 80 by 80 array.
public JButton[][] buttons = new JButton[80][80];
public void addButtons(){ for(int i=0;i<buttons.length;i++){ for(int j=0;i<buttons[i].length;j++){ buttons[i][j]= new JButton(); } }
I'm writing basically my first program for school. I've written small ones, following instructions, but this is the most vague. I'm having issues. I can't figure out what the error means. I'm not done with the code, but I think the ArrayList is throwing me off. I'm trying to gather user input and sum the total. Here's the code:
package graduationplanner; import java.util.ArrayList; import java.util.Scanner; import java.lang.Double; public class GraduationPlanner { public static void main(String[] args) {
I am learning Java on my own, and I am creating little programs to do so.The program below asks the user to input text and hit enter. When user does that, it asks if that is correct. If the user enters "y," the program ends. That's good.If the users enters "n," the strGrategul is set to empty which triggers the while statement to start over again. That's good. However, when the program asks the user to "Tell me one thing you are grateful for..." it doesn't wait for user input. Instead it skips to "You said ''? Enter 'y' or 'n'" It thinks the user enter an empty line.