JSON sorting large data sets - java

http://openlibrary.org/search.json?q=prolog
I have the above API which i am going to be implementing in a android application.
Is there a way to grab from the json on the fly. a specific field for instance:
if i search the above i would need the Author, Language, suggested_title and ISBN. for each result. (so in the above case there would be 100 results.)
which i the plan on storing in a array in the format of
Title|author|lang|ISBN

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class main {
public static void main(String[] args) throws IOException
{
URL testing = new URL("http://openlibrary.org/search.json?q=prolog");
BufferedReader in = new BufferedReader(
new InputStreamReader(testing.openStream()));
String inputLine;
String test = null;
int i =1;
while ((inputLine = in.readLine()) != null)
{
if (inputLine == "\"title_suggest\":")
{
test = inputLine;
}
}
in.close();
System.out.println(test);
}
}
appears to work
as i could then add test to array location [x][y]

Related

How to split file input into 2 different arrays java

How do I split a file input text into 2 different array? I want to make n array for the names, and an array for the phone numbers. I managed to do the file input, but ive tried everything and cant seem to split the names and the numbers, then put it into 2 different arrays. Im noob pls help
here is how the phonebook.txt file looks like
Bin Arry,1110001111
Alex Cadel,8943257000
Poh Caimon,3247129843
Diego Amezquita,1001010000
Tai Mai Shu,7776665555
Yo Madow,1110002233
Caup Sul,5252521551
This Guy,7776663333
Me And I,0009991221
Justin Thyme,1113332222
Hey Oh,3939399339
Free Man,4533819911
Peter Piper,6480013966
William Mulock,9059671045
below is my code
import java.io.*;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class demos {
public static void main(String[] args){
FileInputStream Phonebook;
DataInputStream In;
int i = 0;
String fileInput;
try
{
Phonebook = new FileInputStream("phonebook.txt");
FileReader fr = new FileReader("phonebook.txt");
BufferedReader br = new BufferedReader(fr);
String buffer;
String fulltext="";
while ((buffer = br.readLine()) != null) {
fulltext += buffer;
// System.out.println(buffer);
String names = buffer;
char [] Y ;
Y = names.toCharArray();
System.out.println(Y);
}}
catch (FileNotFoundException e)
{
System.out.println("Error - this file does not exist");
}
catch (IOException e)
{
System.out.println("error=" + e.toString() );
}
For a full functionnal (rather than imperative) solution I propose you this one :
public static void main(String[] args) throws IOException {
Object[] names = Files.lines(new File("phonebook.txt").toPath()).map(l -> l.split(",")[0]).toArray();
Object[] numbers = Files.lines(new File("phonebook.txt").toPath()).map(l -> l.split(",")[1]).toArray();
System.out.println("names in the file are : ");
Arrays.stream(names).forEach(System.out::println);
System.out.println("numbers in the file are : ");
Arrays.stream(numbers).forEach(System.out::println);
}
output
names in the file are :
Bin Arry
Alex Cadel
Poh Caimon
Diego Amezquita
Tai Mai Shu
Yo Madow
Caup Sul
This Guy
Me And I
Justin Thyme
Hey Oh
Free Man
Peter Piper
William Mulock
numbers in the file are :
1110001111
8943257000
3247129843
1001010000
7776665555
1110002233
5252521551
7776663333
0009991221
1113332222
3939399339
4533819911
6480013966
9059671045
As you can see functionnal programming is short and smart …. and easy when you're accustomed
You could simplify it if you are using Java 8:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
public class Test {
static ArrayList<String> names = new ArrayList<String>();
static ArrayList<String> numbers = new ArrayList<String>();
/**
* For each line, split it on the comma and send to splitNameAndNum()
*/
public static void main(String[] args) throws IOException {
Files.lines(new File("L:\\phonebook.txt").toPath())
.forEach(l -> splitNameAndNum(l.split(",")));
}
/**
* Accept an array of length 2 and put in the proper ArrayList
*/
public static void splitNameAndNum(String[] arr) {
names.add(arr[0]);
numbers.add(arr[1]);
}
}
And in Java 7:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Test {
static ArrayList<String> names = new ArrayList<String>();
static ArrayList<String> numbers = new ArrayList<String>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("L:\\phonebook.txt")));
String line;
while ((line = br.readLine()) != null) {
splitNameAndNum(line.split(","));
}
}
/**
* Accept an array of length 2 and put in the proper ArrayList
*/
public static void splitNameAndNum(String[] arr) {
names.add(arr[0]);
numbers.add(arr[1]);
}
}

My HTML fetcher program in java returns incomplete results

My java code is:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class celebGrepper {
static class CelebData {
URL link;
String name;
CelebData(URL link, String name) {
this.link=link;
this.name=name;
}
}
public static String grepper(String url) {
URL source;
String data = null;
try {
source = new URL(url);
HttpURLConnection connection = (HttpURLConnection) source.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
/**
* Attempting to fetch an entire line at a time instead of just a character each time!
*/
StringBuilder str = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while((data = br.readLine()) != null)
str.append(data);
data=str.toString();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public static ArrayList<CelebData> parser(String html) throws MalformedURLException {
ArrayList<CelebData> list = new ArrayList<CelebData>();
Pattern p = Pattern.compile("<td class=\"image\".*<img src=\"(.*?)\"[\\s\\S]*<td class=\"name\"><a.*?>([\\w\\s]+)<\\/a>");
Matcher m = p.matcher(html);
while(m.find()) {
CelebData current = new CelebData(new URL(m.group(1)),m.group(2));
list.add(current);
}
return list;
}
public static void main(String... args) throws MalformedURLException {
String html = grepper("https://www.forbes.com/celebrities/list/");
System.out.println("RAW Input: "+html);
System.out.println("Start Grepping...");
ArrayList<CelebData> celebList = parser(html);
for(CelebData item: celebList) {
System.out.println("Name:\t\t "+item.name);
System.out.println("Image URL:\t "+item.link+"\n");
}
System.out.println("Grepping Done!");
}
}
It's supposed to fetch the entire HTML content of https://www.forbes.com/celebrities/list/. However, when I compare the actual result below to the original page, I find the entire table that I need is missing! Is it because the page isn't completely loaded when I start getting the bytes from the page via the input stream? Please help me understand.
The Output of the page:
https://jsfiddle.net/e0771aLz/
What can I do to just extract the Image link and the names of the celebs?
I know it's an extremely bad practice to try to parse HTML using regex and is the stuff of nightmares, but on a certain video training course for android, that's exactly what the guy did, and I just wanna follow along since it's just in this one lesson.

Displaying text retrieved from a Java class

I'm a beginner in Java. I have a 2 Java file that will passed the text retrieved from one Java file to the main Java file. But it doesnt seems to be working.
Main.java
import java.io.IOException;
public class LSAalgo extends Preprocessing {
public static void main(String[] args) throws IOException {
Preprocessing x = new Preprocessing(?);
}
}
Retrieve.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Preprocessing {
public void preprocessing(String text) throws IOException
{
BufferedReader in = new BufferedReader(new FileReader("input7.txt"));
String line;
while((line = in.readLine()) != null)
{
System.out.println(line);
}
in.close();
}
}
Please help. Thanks.
You are just printing the text in console only. If you want to return complete text from one method to other just change your method return type to String (Since you are returning text) from void. Next change your code to
public String preprocessing() throws IOException
{
BufferedReader in = new BufferedReader(new FileReader("input7.txt"));
String line = "";
while((line = in.readLine()) != null)
{
System.out.println(line);
line += line;//appending complete text
}
in.close();
return line;//returning text
}
In main(-) change code to call preprocessing() method of Preprocessing class.
Preprocessing x = new Preprocessing();
String text = x.preprocessing();//getting text from Preprocessing class

Grabbing text from websites

i have this small chunk of code that will grab the html code from a website. Im interested in parsing a certain section of the code though, several times. More specifically, im making a pokedex, and would like to parse certain descriptions from say a bulbapedia page, http://bulbapedia.bulbagarden.net/wiki/Bulbasaur_(Pok%C3%A9mon) for example. How would I make this parser take just the description of bulbasaur? How would I create any boundary to stop and start?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class WebCrawler{
public static void main(String[] args) {
try {
URL google = new URL("http://pokemondb.net/pokedex/bulbasaur");
URLConnection yc = google.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
You can use Jsoup, with this code you can get the description of Bulbasaur:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class Test {
public static void main(String[] args) throws IOException {
Document doc = Jsoup
.connect(
"http://bulbapedia.bulbagarden.net/wiki/Bulbasaur_(Pok%C3%A9mon)")
.get();
Elements newsHeadlines = doc.select("#mw-content-text p");
for (Object o : newsHeadlines) {
System.out.println(o.toString());
}
}
}
Where mw-content is the main div.
Try with Jsoup
Syntax is JQuery selectors liked.

Parsing Json witth Gson in Java

I'm trying to parse a local json file and the output is not what it's supposed to show.
I have little experience with Json (and Gson) so I'm unclear as to what the problem is.
Here is the tweet class:
public class tweet {
String from_user;
String from_user_name;
String profile_image_url;
String text;
public tweet(){
//empty constructor
}
}
This is the class where Gson is used:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.Gson;
public class tweetfeedreader {
public static void main(String args[]) throws FileNotFoundException {
Gson gson = new Gson();
BufferedReader bufferedReader = new BufferedReader(new FileReader(
"C:/Users/LILITH/Desktop/jsonfile.json"));
tweet J_tweet = gson.fromJson(bufferedReader, tweet.class);
System.out.println(J_tweet);
}
}
Lastly, the .json file which i have saved onto a local directory:
http://search.twitter.com/search.json?q=%40android
there are no errors, but the output is:
tweet#3030d5aa
I'm uncertain as to what might be going wrong, so thanks for your guidance!
[Edit: I forgot to add that I have searched SO before and read the related posts. They may be similar but I am not having much luck in piecing the pieces together.]
Strip the results array out of that json, leaving nothing outside the []s.
Then this was just about the least I could modify the code to get it working:
import java.lang.reflect.*;
import java.io.*;
import java.util.*;
import com.google.gson.*;
import com.google.gson.reflect.*;
public class tweetfeedreader {
public static void main(String args[]) throws IOException {
Gson gson = new Gson();
BufferedReader bufferedReader = new BufferedReader(new FileReader(
"jsonfile.json"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) sb.append(line);
Type tweetCollection = new TypeToken<Collection<tweet>>(){}.getType();
Collection<tweet> tweets = gson.fromJson(line, tweetCollection);
for (final tweet t : tweets) System.out.println(t.text);
}
}
System.out.println(J_tweet);
log in console reference on object J_tweet (tweet#3030d5aa)
Add method toString() to your tweet class
for example
#Override
public String toString()
{
return "from_user: " + from_user + "; from_user_name : " + from_user_name;
}

Categories