Java - search engine

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 613 Views - Last Post: 05 February 2018 - 11:37 AM Rate Topic: -----

#1 sureme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 17

Java - search engine

Posted 31 January 2018 - 06:46 AM

Hi guys, basically, for my project I have to design a simple search engine that will have a text field for users to write in, and when the "search" button is clicked the results/links for that phase entered in the textfield should appear in a new window.

I have done it so that users can enter the phase in the console, and the results will be shown in console like so:

package com.journaldev.jsoup;
import java.io.IOException;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Page {

	public static final String GOOGLE_SEARCH_URL = "https://www.google.com/search";
	public static void main(String[] args) throws IOException {
		
		//Taking search term input from console
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter the search term.");
		String searchTerm = scanner.nextLine();
		System.out.println("Please enter the number of results. Example: 5 10 20");
		int num = scanner.nextInt();
		scanner.close();
		
		String searchURL = GOOGLE_SEARCH_URL + "?q="+searchTerm+"&num="+num;
		//without proper User-Agent, we will get 403 error
		Document doc = Jsoup.connect(searchURL).userAgent("Mozilla/5.0").get();
		
		//below will print HTML data, save it to a file and open in browser to compare
		//System.out.println(doc.html());
		
		//If google search results HTML change the <h3 class="r" to <h3 class="r1"
		//we need to change below accordingly
		Elements results = doc.select("h3.r > a");

		for (Element result : results) {
			String linkHref = result.attr("href");
			String linkText = result.text();
			System.out.println("Text::" + linkText + ", URL::" + linkHref.substring(6, linkHref.indexOf("&")));
		}
	}

} 




But i want this same concept with a text field/ search button and not in console.

this is my code for the GUI:

 
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;

import com.journaldev.jsoup.Searchwindow;

import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class Frame {

	private JFrame frame;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Frame window = new Frame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Frame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(Color.WHITE);
		frame.setBounds(100, 100, 777, 502);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton btnNewButton = new JButton("Search");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				//open a new window when search is clicked
				Frame.dispose();
				Searchwindow srchWindow=new Searchwindow();
				srchwindow.setVisible(true);;
				
					
			}
			
				
		});
		btnNewButton.setBounds(550, 206, 149, 51);
		frame.getContentPane().add(btnNewButton);
		
		textField = new JTextField();
		textField.setBounds(78, 206, 460, 51);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(this.getClass().getResource("Actions-edit-find-icon.png")));
		label.setBounds(209, 259, 266, 163);
		frame.getContentPane().add(label);
		
		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon("C:\\Users\\Taylan\\OneDrive\\Desktop\\Imgs\\coollogo_com-40141685.png"));
		label_1.setBounds(78, 64, 627, 129);
		frame.getContentPane().add(label_1);
	}

	protected static void dispose() {
		// TODO Auto-generated method stub
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java - search engine

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Java - search engine

Posted 31 January 2018 - 06:51 AM

Do you have any specific java programming questions?

Note: Frame is the name of an existing java class. You should use your own unique name for your classes.

This post has been edited by NormR: 31 January 2018 - 06:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,135
  • Joined: 20-September 08

Re: Java - search engine

Posted 31 January 2018 - 06:54 AM

So - what's the problem?
Was This Post Helpful? 0
  • +
  • -

#4 sureme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 17

Re: Java - search engine

Posted 31 January 2018 - 06:57 AM

The problem is How can i make the program so that once something is written in the textfield e.g. "java", and the search button is clicked - I want the program to collect that search results from e.g. google and show it in another frame/window
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,135
  • Joined: 20-September 08

Re: Java - search engine

Posted 31 January 2018 - 07:01 AM

The easiest way would probably be to use an html-enabled control. I've an idea that the bast one would probably be in JavaFx
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Java - search engine

Posted 31 January 2018 - 07:02 AM

Quote

collect that search results from e.g. google
and show it

Does that mean
sending a request to a website
parsing what is returned
posting the parsed response to a component in the GUI?
Was This Post Helpful? 0
  • +
  • -

#7 sureme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 17

Re: Java - search engine

Posted 31 January 2018 - 07:04 AM

Yes exactly. Because with my code everything is done in the console which I don't want. So yes I want it to be in my GUI

View Postg00se, on 31 January 2018 - 07:01 AM, said:

The easiest way would probably be to use an html-enabled control. I've an idea that the bast one would probably be in JavaFx


can you expand on that please ? I am new to java and just learning it very recent

This post has been edited by ndc85430: 31 January 2018 - 07:08 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#8 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Java - search engine

Posted 31 January 2018 - 07:19 AM

Quote

everything is done in the console

Does that mean you have the code for accessing a website, retrieving the contents and parsing it?

The GUI part would be simply to have an input text field and a button.
The button listener code would take care of getting the data from the website
Then the results could be displayed in a component like a text area in the GUI.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 14036
  • View blog
  • Posts: 56,156
  • Joined: 12-June 08

Re: Java - search engine

Posted 31 January 2018 - 07:43 AM

Don't forget - google has an actual API for folk to use.
https://developers.g...is-explorer/#p/
Was This Post Helpful? 0
  • +
  • -

#10 sureme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 17

Re: Java - search engine

Posted 31 January 2018 - 08:14 AM

No i do not have the code, that is what i am struggling with.
My code retrives the contents and shows it on the console (eclipse) I have build the GUI with text fied and button. But I need to get the code for the button to get the data from the website

This post has been edited by ndc85430: 31 January 2018 - 08:18 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#11 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 798
  • View blog
  • Posts: 3,262
  • Joined: 13-June 14

Re: Java - search engine

Posted 31 January 2018 - 08:20 AM

Have you learnt how event handling works? If not, then you need to go and do that, as it's a central concept for GUIs.

Also, please stop quoting the previous post in its entirety and just use the "Reply" button instead.
Was This Post Helpful? 0
  • +
  • -

#12 sureme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 17

Re: Java - search engine

Posted 31 January 2018 - 08:21 AM

Okay I will look into event handling. And apologies.
Was This Post Helpful? 0
  • +
  • -

#13 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 798
  • View blog
  • Posts: 3,262
  • Joined: 13-June 14

Re: Java - search engine

Posted 31 January 2018 - 08:27 AM

I'm quite surprised that you haven't come across it already, though, if you're learning Swing.
Was This Post Helpful? 0
  • +
  • -

#14 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Java - search engine

Posted 31 January 2018 - 08:28 AM

I suggest that you work on the project one step at a time.
First build a GUI with the text input field and a button.
Compile and execute that.
When that works,
add a listener to the button and have the listener retrieve the contents of the text input field and print it on the console.
Compile and execute that.
When that works,
add the existing code that accesses the website and retrieves and parses the data.
Print the parsed data on the console.
Compile and execute that.

When that works move to designing how and where you want the data to be displayed in the GUI.
When you get to that stage, post your code and design and we'll work on the next step.
Was This Post Helpful? 1
  • +
  • -

#15 sureme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 17

Re: Java - search engine

Posted 05 February 2018 - 11:24 AM

Hi, basically I got it all ready now.
The program asks user for a search term. Once user inputs a phase and clicks the search button the program searchs google for that phase and displays results in the console.

But can you now help me display them results in a separate Frame other than the console?
thanks.


Here is the code:

package com.journaldev.jsoup;
import java.io.IOException;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javax.swing.JOptionPane;



public class Page {
	public static void main(String[] args) throws IOException{
	final String GOOGLE_SEARCH_URL = "https://www.google.com/search";

		{
			String title=JOptionPane.showInputDialog(null,"Enter Search Phase");
			
			System.out.println("Search Phase is: " + title);
		
		
		String searchURL = GOOGLE_SEARCH_URL + "?q="+title;
		//without proper User-Agent, we will get 403 error
		Document doc = Jsoup.connect(searchURL).userAgent("Mozilla/5.0").get();
		
		//below will print HTML data, save it to a file and open in browser to compare
		//System.out.println(doc.html());
		
		//If google search results HTML change the <h3 class="r" to <h3 class="r1"
		//we need to change below accordingly
		Elements results = doc.select("h3.r > a");

		for (Element result : results) {
			String linkHref = result.attr("href");
			String linkText = result.text();
			System.out.println("Text::" + linkText + ", URL::" + linkHref.substring(6, linkHref.indexOf("&")));
		}
	}

} }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2