How to use indexOf operation with the rss feed title - java

I have got 3000 symbols present in java array .
I am using ROME API to fetch the rss feeds ,
then i am trying to check if the title contains anything part of array then only i must display the title
This is my program
String[] myFirstStringArray = new String[] {"ONE","TWO"}; // 3000 symbols
try {
String url = "http://www.rssmix.com/u/8159030/rss.xml";
URL feedUrl = new URL(url);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
for (SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
JSONObject jsonobj_latestnews = new JSONObject();
String title = entry.getTitle();
jsonobj_latestnews.put("title", title)
latestnews.put(jsonobj_latestnews);
}
} catch (Exception e) {
e.printStackTrace();
}
Could you please tell me how to check

assuming that the title is likely to be much less than the size of array.
String [] titleWords = title.split(" ");
ArrayList<String> wordList = Arrays.asList(myFirstStringArray);
for (String word : titleWords)
{
if(wordList.contains(word))
{
/*do something here. you can get the index of the word by
wordList.indexOf(word); this index will the same as the index in myFirstStringArray*/
break;
}
}
this should do it

Related

How to ignore whitespace and read from a text file in java?

I am having troubles with the following code.
public class ReadFromFile {
public static void main(String[] args) {
Scanner scan=null;
try {
scan=new Scanner(new File("AllAccInfo.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String input=scan.nextLine();
while(input!=null) {
String[] data=input.split("[\\: \t]");
try {
input=scan.nextLine();
}
catch(NoSuchElementException e) {
break;
}
}
}
}
Here is my Text File
SavingAccount: Sourav Roy Komol 25478963 79863 77010.0 5000.0 45000.0
CurrentAccount: Shami Kaiser 1234789 22167 88000.0 5000.0
The Output of the code is
Roy
Kaiser
I want to pick the the whole name like Sourav Roy Komol and Shami Kaisar into one String variable. Such that 1st My Programme 1st read the 1st line and pick the "Sourav Roy Komol" will pick the "String x". Same Process same for "Shami Kaisar". How can I pick these values with ignoring whitespace? Here 1234789 and 22167 is also a String variable.
Since the input data is tab-delimited (I copy+pasted your input data where each field is separate by a tab), you could easily separate the fields using a StringTokenizer. Here's a simple example showing how that could work, using the first line of your input example, and also showing the output when it runs.
String one = "SavingAccount: Sourav Roy Komol\t25478963\t79863\t77010.0\t5000.0\t45000.0";
StringTokenizer tokenizer = new StringTokenizer(one, "\t");
while (tokenizer.hasMoreTokens()) {
System.out.println("token: " + tokenizer.nextToken());
}
And the output here:
token: SavingAccount: Sourav Roy Komol
token: 25478963
token: 79863
token: 77010.0
token: 5000.0
token: 45000.0
This should be enough of working code to get you going. You will probably want to exclude the "SavingAccount: " part from the first token, leaving just the name ("Sourav Roy Komol"). There are a few options for how you could do that, but the code here should be enough to address your parsing issues.
Since there is no full code so I am assuming you are looking for splitting your string so that name will appear as one string.
In that case, you can use below regex in your split & later trim() to remove trailing whitespace:
String[] data=input.split("[\\^0-9]");
String name = data[0].trim(); //this will trim trailing whitespace of your returned string.
If you print, String name in console, you will see full name with trailng whitespace removed. If you want to keep trailing white space, you can ignore doing trim() to returned string.
This suggestion assumes that your input file will always have names at first place.
Your regex will only split for one of the three characters you specified. What you should do is to use a capturing group to either split by a colon or a set of white spaces followed by one or more digits. This will give you: the first string before the colon, the name and then each number.
public class Main {
public static void main(String[] args) {
Scanner scan = null;
try {
scan = new Scanner(new File("AllAccInfo.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] data;
String input = scan.nextLine();
while (input != null) {
data = input.split("(:|\\s+(?=\\d+))");
try {
System.out.println(data[1].trim());
input = scan.nextLine();
} catch (NoSuchElementException e) {
break;
}
}
}
}
Output
Sourav Roy Komol
Shami Kaiser
Method 1
Assuming you have a plan txt as your database and knows how to get only names from the txt file. Then do this
Replace whitespace with underscore
st = st.replaceAll("\\s+","_"). This gets all names to have an underscore like this Sourav_Roy_Komol
Then when rendering it to the screen
Do this
st = st.replaceAll("_"," "), this replaces the underscore with a whitespace.
Method 2
Replace split("[\: \t]") to split("[\s+]");
Hope is clear enough?.
BASED ON YOUR REQUEST FOR A JSON METHOD: SEE BELOW
Step 1: Add Dependency
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Step 2: Import the library
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
Step 3: Create JsonObject.
JSONObject employeeDetails1 = new JSONObject();
employeeDetails.put("AccountType", "SavingAccount");
employeeDetails.put("Fullname", "Sourav Roy Komol");
employeeDetails.put("AccountNumber", "25478963");
JSONObject employeeObject = new JSONObject();
employeeObject.put("employee", employeeDetails1);
Repeat the above code statically for other users OR use loop in filling them up.
Step 4: Add employeesObj to list
JSONArray employeeList = new JSONArray();
employeeList.add(employeeObject);
Step 5: Write to file
try (FileWriter file = new FileWriter("employees.json")) {
//We can write any JSONArray or JSONObject instance to the file
file.write(employeeList.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
NOW!!. The above code ONLY writes data to file (employees.json)in JSON FORMAT.
TO READ THE DATA
Step 6:
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("employees.json"))
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray employeeList = (JSONArray) obj;
System.out.println(employeeList);//you can delete this line
//Iterate over employee array
employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
Step 7: Create a function to parseobjects
private static void parseEmployeeObject(JSONObject employee)
{
//Get employee object in list
JSONObject employeeObject = (JSONObject) employee.get("employee");
//Get employee fullname
String fullname= (String) employeeObject.get("fullname");
System.out.println(fullname);
//here you can format the output......
}

String array suddenly losses stored values

I'm using a dat file to store underscore separated values and comma separated values at the same time but every time I want to get the values of a the second String[] called basics in order to set values of another object but it suddenly resets it's size and stores values to the first splitted one.
String data in this case would be like:
String data = "0234_ADMIN_12-Jun-2022 21:58:59,5635_PAL_16.0_54";
private void cargarNotas() {
try {
BufferedReader leerNotas = new BufferedReader(new FileReader(regNotas));
String data = leerNotas.readLine();
while (data!= null) {
String[] items = data.split(",");
String[] basics = items[0].trim().split("_");
System.out.println(basics.length);
NotaDeVenta tempNota = new NotaDeVenta();
tempNota.setnoNota(basics[0]);
tempNota.setUsuario(basics[1]);
tempNota.setFecha(FORMAT.parse(basics[2]));
String[] values = null;
for(int i = 0;i<items.length-1;i++) {
values = items[i+1].split("_");
ItemParaMovimientos tempItem = new ItemParaMovimientos(Control.getProducto(values[1]),Integer.parseInt(values[3]));
tempNota.addItem(tempItem);
}
Control.addNota(tempNota);
data = leerNotas.readLine();
}
leerNotas.close();
}
catch (Exception e) {
e.printStackTrace();
}

Add json file into object and saved into arraylist

I m trying get data from json file and saved as objects and then put into different arrylist, i m stuck on the point where i coundnt get "A320" into a obejcts, "type_ratings": [
"A320"
]
List<Crew> Allcrews = new ArrayList<>();
List<Pilot> pilotsList = new ArrayList<>();
List<CabinCrew> Cabincrews = new ArrayList<>();`
public void loadCrewData(Path p) throws DataLoadingException{
try {
BufferedReader reader = Files.newBufferedReader(p);
String jsonStr = "";
String line = "";
while ((line=reader.readLine()) !=null)
{
jsonStr =jsonStr+line;
}
System.out.println ("Pilots Informations: ");
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray pilots = jsonObj.getJSONArray("pilots");
for(int j =0; j<pilots.length(); j++) {
JSONObject pilot = pilots.getJSONObject(j);
Pilot pil = new Pilot();
pil.setForename(pilot.getString("forename"));
pil.setHomeBase(pilot.getString("home_airport"));
pil.setSurname(pilot.getString("surname"));
pil.setRank(Rank.CAPTAIN);
pil.setRank(Rank.FIRST_OFFICER);
pil.setQualifiedFor(pilot.getString("type_ratings"));;
pilotsList.add(pil);
Allcrews.add(pil);
System.out.println( "Forename: " +pilot.getString("forename"));
System.out.println( "Surname: " +pilot.getString("surname"));
System.out.println( "Rank: " +pilot.getString("rank"));
System.out.println("Home_Airport: " + pilot.getString("home_airport"));
System.out.println("type_ratings: " + pilot.getJSONArray("type_ratings"));
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}[![JSONFile][1]][1]
The "type_ratings" key will return a JSONArray, not a String. This is because of the square brackets that surround the string, making it an array with 1 element. You could either change the JSON structure and remove the square brackets in order to only make it a String, or you could simply do pil.setQualifiedFor(pilot.getJSONArray("type_ratings")[0]);, which will get the array, and return the first element, the string. Keep in mind that you should only do this if you know that type_ratings will always contain an array with 1 string.

How to load text data into a table using Java

I have a text data like
name = abc
id = 123
Place = xyz
Details = some texts with two line
name = aaa
id = 54657
Place = dfd
Details = some texts with some lines
I need to place them in a table or csv and my output should look like
name id Place Details
abc 123 xyz Some texts
dfd 54657 dfd Some texts
How can I do this with java?
Code for the CSV version :) It reads the input file and create a CSV in the format you asked for:
try {
BufferedReader sc = new BufferedReader(new FileReader("input2.txt"));
ArrayList<String> name = new ArrayList<>();
ArrayList<String> id = new ArrayList<>();
ArrayList<String> place = new ArrayList<>();
ArrayList<String> details = new ArrayList<>();
String line = null;
while ((line = sc.readLine()) !=null) {
if (!line.trim().equals("")) {
System.out.println(line);
if (line.toLowerCase().contains("name")) {
name.add(line.split("=")[1].trim());
}
if (line.toLowerCase().contains("id")) {
id.add(line.split("=")[1].trim());
}
if (line.toLowerCase().contains("location")) {
place.add(line.split("=")[1].trim());
}
if (line.toLowerCase().contains("details")) {
details.add(line.split("=")[1].trim());
}
}
}
PrintWriter pr = new PrintWriter(new File("out.csv"));
pr.println("name;id;Place;Details;");
for (int i = 0; i < name.size(); i++) {
pr.println(name.get(i) + ";" + id.get(i) + ";" + place.get(i) + ";" + details.get(i) + ";");
}
pr.close();
} catch (Exception e) {
e.printStackTrace();
}
Sample file content it processes:
name = abinhav
Location =Bangalore
Id =613636064725610496
Details = infoodnetwork: Q2 is up. You can still join the Megakitchens in India contest and grab some exciting vouchers. RT if you are enjoying…
name = Mathi
Location =Chennai
Id =613636066474508289
Details = i am the drifter Of course they can, but the BBC needs a daily negative story on India.
Reading from text file and writing to csv(comma seperated values) can be achieved using java io.
your logic should once write the headers to a text file with separator as comma and then read the corresponding values from the text may be use split("=") and append to the file with comma separator. You can create new files write the values and save the file with csv extension
try {
BufferedReader bReader = new BufferedReader(new FileReader("input file"));
String line = "";
while ((line = bReader.readLine()) != null) {
String[] strArray = line.split("=");
// write this to file
System.out.println( strArray[1]);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Parse the text file with a Scanner (doc here)
Create a DefaultTableModel (doc here). DefaultTableModel model = new DefaultTableModel(data, new String[]{"name","id","Place","Details"});, where data is a 2D String array with your data.
Create a JTable (doc here) with the model you just created. JTable table = new JTable(model);
Add the table to a JPanel, or JFrame, with a JScrollPane (if needed): panel.add(new JScrollPane(table));.

OutOfMemoryError in StringBuilder and HashSet

I have a JSON file (.json) in Amazon S3. I need to read it and create a new field called Hash_index for each JsonObject. The file is very big, so I am using a GSON library to avoid my OutOfMemoryError in reading the file. Below is my code. Please note that I am using GSON
//Create the Hashed JSON
public void createHash() throws IOException
{
System.out.println("Hash Creation Started");
strBuffer = new StringBuffer("");
try
{
//List all the Buckets
List<Bucket>buckets = s3.listBuckets();
for(int i=0;i<buckets.size();i++)
{
System.out.println("- "+(buckets.get(i)).getName());
}
//Downloading the Object
System.out.println("Downloading Object");
S3Object s3Object = s3.getObject(new GetObjectRequest(inputBucket, inputFile));
System.out.println("Content-Type: " + s3Object.getObjectMetadata().getContentType());
//Read the JSON File
/*BufferedReader reader = new BufferedReader(new InputStreamReader(s3Object.getObjectContent()));
while (true) {
String line = reader.readLine();
if (line == null) break;
// System.out.println(" " + line);
strBuffer.append(line);
}*/
// JSONTokener jTokener = new JSONTokener(new BufferedReader(new InputStreamReader(s3Object.getObjectContent())));
// jsonArray = new JSONArray(jTokener);
JsonReader reader = new JsonReader( new BufferedReader(new InputStreamReader(s3Object.getObjectContent())) );
reader.beginArray();
int gsonVal = 0;
while (reader.hasNext()) {
JsonParser _parser = new JsonParser();
JsonElement jsonElement = _parser.parse(reader);
JsonObject jsonObject1 = jsonElement.getAsJsonObject();
//Do something
StringBuffer hashIndex = new StringBuffer("");
//Add Title and Body Together to the list
String titleAndBodyContainer = jsonObject1.get("title")+" "+jsonObject1.get("body");
//Remove full stops and commas
titleAndBodyContainer = titleAndBodyContainer.replaceAll("\\.(?=\\s|$)", " ");
titleAndBodyContainer = titleAndBodyContainer.replaceAll(",", " ");
titleAndBodyContainer = titleAndBodyContainer.toLowerCase();
//Create a word list without duplicated words
StringBuilder result = new StringBuilder();
HashSet<String> set = new HashSet<String>();
for(String s : titleAndBodyContainer.split(" ")) {
if (!set.contains(s)) {
result.append(s);
result.append(" ");
set.add(s);
}
}
//System.out.println(result.toString());
//Re-Arranging everything into Alphabetic Order
String testString = "acarpous barnyard gleet diabolize acarus creosol eaten gleet absorbance";
//String testHash = "057 1$k 983 5*1 058 52j 6!v 983 03z";
String[]finalWordHolder = (result.toString()).split(" ");
Arrays.sort(finalWordHolder);
//Navigate through text and create the Hash
for(int arrayCount=0;arrayCount<finalWordHolder.length;arrayCount++)
{
if(wordMap.containsKey(finalWordHolder[arrayCount]))
{
hashIndex.append((String)wordMap.get(finalWordHolder[arrayCount]));
}
}
//System.out.println(hashIndex.toString().trim());
jsonObject1.addProperty("hash_index", hashIndex.toString().trim());
jsonObject1.addProperty("primary_key", gsonVal);
jsonObjectHolder.add(jsonObject1); //Add the JSON Object to the JSON collection
jsonHashHolder.add(hashIndex.toString().trim());
System.out.println("Primary Key: "+jsonObject1.get("primary_key"));
//System.out.println(Arrays.toString(finalWordHolder));
//System.out.println("- "+hashIndex.toString());
//break;
gsonVal++;
}
System.out.println("Hash Creation Completed");
}
catch(Exception e)
{
e.printStackTrace();
}
}
When this code is executed, I got the following error
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2894)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:117)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:407)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at HashCreator.createHash(HashCreator.java:252)
at HashCreator.<init>(HashCreator.java:66)
at Main.main(Main.java:9)
[root#ip-172-31-45-123 JarFiles]#
Line number 252 is - result.append(s);. It is Inside the HashSet loop.
Previously, it generated OutOfMemoryError in line number 254. Line number 254 is - set.add(s); it is also inside the HashSet array.
My Json files are really really big. Gigabytes and Terabytes. I have no idea about how to avoid the above issue.
Use a streaming JSON library like Jackson.
Read in a some JSON, add the hash, and write them out.
Then read in some more, process them, and write them out.
Keep going until you have processed all the objects.
http://wiki.fasterxml.com/JacksonInFiveMinutes#Streaming_API_Example
(See also this StackOverflow post: Is there a streaming API for JSON?)

Categories

Resources