Finish the logic of search on java [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
When the array argi has more than one partition, its only begins to find word from the second url of the array, but why? Correct it please in code. Here the part of it:
I make regular expresion for search by title in url:
private final Pattern TITLE = Pattern.compile("\\<title\\>(.*)\\<\\/title\\>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
And searh logic here:
public String search(String url, String someword) {
try {
InputStreamReader in = new InputStreamReader(new URL(url).openStream(),"UTF-8");
StringBuilder input = new StringBuilder();
int ch;
while ((ch = in.read()) != -1) {
input.append((char) ch);
}
if (Pattern.compile(someword, Pattern.CASE_INSENSITIVE).matcher(input).find()) {
Matcher title = TITLE.matcher(input);
if (title.find()) {
return title.group(1);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (PatternSyntaxException e) {
e.printStackTrace();
}
return null;
}
String[] argi = {"http://localhost:8080/site/dipnagradi","http://localhost:8080/site/contacts"};
for (int i = 0; i < argi.length; i++) {
String result = search(argi[i], word);
if (result != null) {
str = "Search phrase " + "<b>"+ word + "</b>" + " have found " + "" + result + ""+ "<p></p>";
}
else{
str="Search word not found!";
}
if (word == null||word=="") {
str = "Enter a search word!";
}
}
return null;
}

Use if (word == null || word.isEmpty()), (for beginners) never use == in Object comparison in java.
Also, for a more detailed answer, you really need to post your input and expected output. And also what`search() does.

Do not use '==' operator with String as String is an Object and is compared using equals method.. Instead use : if(!"".equals(word){}

Related

I am trying to read a file in a specific way but for some reason it returns random white spaces

Basically what i am trying to do is read a file that contains some questions. The file is organized like this:
//The category of the question
Questions
//The question
Possible Answer
//Answer #1
//Answer#2
//Answer#3
//Answer#4
Right answer
//The right answer
Image
//The image path if it exists
This is for only one question. There are multiple questions like this.
private boolean readQuestion() {
try {
String line = this.reader.readLine();
if (line == null) {
return false;
}
String category = "";
String question = "";
String[] possibleAnswers = {
"",
"",
"",
""
};
String rightAnswer = "";
String image = "";
boolean imageQuestion = false;
if (line.compareTo("Category") == 0) {} else {
//read the category
category = this.reader.readLine();
}
if (line.compareTo("Question") == 0) {
//read the question (String question = ....)
question = this.reader.readLine();
}
line = this.reader.readLine();
if (line.compareTo("Possible answers") == 0) {
//read four lines with the possible answers(String [] possibleAnswers = ...)
for (int i = 0; i < 4; i++) {
possibleAnswers[i] = this.reader.readLine();
}
}
line = this.reader.readLine();
if (line.compareTo("Right answer") == 0) {
//read the right answer(String rightAnswer = ...)
rightAnswer = this.reader.readLine();
}
line = this.reader.readLine();
if (line.compareTo("Image") == 0) {
//read the image name
image = this.reader.readLine();
imageQuestion = true;
}
BaseQuestion question1 = new BaseQuestion(question, possibleAnswers, rightAnswer, category);
if (imageQuestion) {
ImageQuestion imageQuestion_1 = new ImageQuestion(question1, image);
this.questions.add(imageQuestion_1);
} else {
this.questions.add(question1);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private void readFile() {
while (this.readQuestion());
}
i have made the readQuestion boolean so that it returns if there is another question following. The reader is also a BufferedReader object. The BaseQuestion is a class that has a category, a question, 4 possible answers and a right answer. The imageQuestion has all the attributes of the BaseQuestion but also an imagePath.
It's because you have a NullPointerException (during 3rd iteration of you loop) on your if stmt. because line = null
line = this.reader.readLine();
if (line.compareTo("Image") == 0)
java.lang.NullPointerException: Cannot invoke "String.compareTo(String)" because "line" is null
public static void main(String[] args) throws IOException {
Demo d = new Demo();
d.readFile();
System.out.println(d.questions.size());
for (Question q : d.questions) {
System.out.println(q.toString());
where Question is just a dummy interface
interface Question {}
class BaseQuestion implements Question { ... }
this is the output I'm able to get from your class:
2
BaseQuestion{question='', rightAnswer='', category='', possibleAnswers=[null, null, null, null]}
BaseQuestion{question='', rightAnswer='', category='//Answer #1', possibleAnswers=[null, null, null, null]}
to see output presented above add these methods to your BaseQuestion and ImageQuestion classes:
#Override
public String toString() {
return "ImageQuestion{" +
"image='" + image + '\'' +
", question1=" + question1 +
'}';
}
and
#Override
public String toString() {
return "BaseQuestion{" +
"question='" + question + '\'' +
", rightAnswer='" + rightAnswer + '\'' +
", category='" + category + '\'' +
", possibleAnswers=" + Arrays.toString(possibleAnswers) +
'}';
}
You are reading here same line twice:
if (line.compareTo("Category") == 0) {} else {
//read the category
category = this.reader.readLine();
}
if (line.compareTo("Question") == 0) {
//read the question (String question = ....)
question = this.reader.readLine();
}
there's a missing readLine() between Category and Questions that's why the current line doesn't match your desired input
if (line.compareTo("Category") == 0) {} else {
//read the category
category = this.reader.readLine();
}
line = this.reader.readLine();
if (line.compareTo("Question") == 0) {
//read the question (String question = ....)
question = this.reader.readLine();
}
... and what's the purpose of this piece of code? are you sure it works as expected?
if (line.compareTo("Category") == 0) {} else {
//read the category
category = this.reader.readLine();
}

Reading text between quotation marks

Here's a piece of text I'm trying to work with:
lat="52.336575" lon="6.381008">< time>2016-12-19T12:12:27Z< /time>< name>Foto 8 </name>< desc>Dag 4 E&F
Geb 1.4
Hakhoutstoof < /desc>< /wpt>
I'm trying to extract the coördinates between the "" and put the values between the "" into a string, but I can't get it to work...
Here's my code (so far):
public void openFile() {
Chooser = new JFileChooser("C:\\Users\\danie\\Desktop\\");
Chooser.setAcceptAllFileFilterUsed(false);
Chooser.setDialogTitle("Open file");
Chooser.addChoosableFileFilter(new FileNameExtensionFilter("*.gpx",
"gpx"));
int returnVal = Chooser.showOpenDialog(null);
try {
Dummy = new Scanner(Chooser.getSelectedFile());
} catch (FileNotFoundException E) {
System.out.println("Error: " + E);
}
}
public void createDummy() {
Dummy.useDelimiter("<wpt");
if (Dummy.hasNext()) {
String Meta = Dummy.next();
}
Dummy.useDelimiter("\\s[<wpt]\\s|\\s[</wpt>]\\s");
try {
while (Dummy.hasNext()) {
String Test = Dummy.next();
DummyFile = new File("Dummy.txt");
Output = new PrintWriter(DummyFile);
Output.print(Test);
Output.println();
Output.flush();
Output.close();
}
Reader = new FileReader(DummyFile);
Buffer = new BufferedReader(Reader);
TestFile = new File("C:\\Users\\danie\\Desktop\\Test.txt");
Writer = new PrintWriter(TestFile);
String Final;
while ((Final = Buffer.readLine()) != null) {
String WPTS[] = Final.split("<wpt");
for (String STD:WPTS) {
Writer.println(STD);
Writer.flush();
Writer.close();
}
}
} catch (IOException EXE) {
System.out.println("Error: " + EXE);
}
Dummy.close();
}
}
I'm really new to Java :(
I think the following code will do the trick ...
the "string" is only used to test the regex
final String string = "lat=\"52.336575\" lon=\"6.381008\">< time>2016-12-19T12:12:27Z< /time>< name>Foto 8 </name>< desc>Dag 4 E&F \nGeb 1.4 \n" + "Hakhoutstoof < /desc>< /wpt>";
final String latitudeRegex = "(?<=lat=\")[0-9]+\\.[0-9]*";
final Pattern latitudePattern = Pattern.compile(latitudeRegex);
final Matcher latitudeMatcher = latitudePattern.matcher(string);
//finds the next (in this case first) subsequence matching the given regex
latitudeMatcher.find();
String latitudeString = latitudeMatcher.group();
double lat = Double.parseDouble(latitudeString); //group returns the match matched by previous match
System.out.println("lat: " + lat);
to get the longitude, just replace lat by lon in the regex
this site is very useful for creating a regex
https://regex101.com/
you can even create the java code at this site

Nth occurrence of a character and if conditionals

Let's say we have two strings (ULRs):
https://stackoverflow.com/questions/ask
https://web.whatsapp.com/
I need to write expression such as:
if after 3rd slash (/) is nothing or 3rd slash does not exists do
{
some operation
} else {
another action
}
Please help.
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}
String[] stringArr = list.toArray(new String[0]);
//copying and removing https from the list
List<String> list2 = new ArrayList<String>();
Collections.addAll(list2, stringArr);
Iterator<String> iter = list2.iterator();
while(iter.hasNext()){
if(iter.next().contains(https))
// here you should copy https lines to another file.
iter.remove();
}
String[] stringArr2 = list2.toArray(new String[0]);
for (int i = 0; i<stringArr2.length; i++) {
//condition for pure domain names.
//else the action below
System.out.println(getDomainName(stringArr2[i]) + "," + stringArr2[i] + "," + "U" +"," + number_of_doc + "," + today);
}
}
public static String getDomainName(String url) throws URISyntaxException {
URI uri = new URI(url);
String domain = uri.getHost();
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
}
Why you don't split :
String link = "https://stackoverflow.com/questions/ask ";
if (link.split("/").length >= 3 ) {
System.out.println("CORRECT");
}else{
System.out.println("NOT CORRECT");
}
The idea is : Split your String with / if the result is great or equal then 3 then your condition is correct, else not correct.
Edit
Or like #biziclop mention in comment you can use Guava's Splitter for example :
Iterable<String> result =
Splitter.on(CharMatcher.anyOf("/")).
limit(4).
split("https://stackoverflow.com/questions/ask");
if (Lists.newArrayList(result).size() > 3) {
System.out.println(Lists.newArrayList(result).get(3));
}else{
System.out.println("NOTHING");
}
Input
https://stackoverflow.com/questions/ask
https://stackoverflow.com
Output
questions/ask
NOTHING
You can use simple regex:
String url = "https://web.whatsapp.com/";
if(url.matches("\\w+://(\\w+.\\w+)+(.\\w+)*/(.+)"))
System.out.println("Correct URL");
else
System.out.println("Incorrect URL");

apply extraction information with java

i trying to apply a dictionary (File of Words) on text(File of text):
we test if the word exists in a line of the text, if yes we will print it (the line). we test all word of dictionary for every line of text.
i used EXPREG pattern+matcher but the problem is the time. the operation take 5H.
The 2 File have 3330ko and 55ko
.
my question is is there another method to do this like UNITEX but in java
public class Tratemant_Dic extends Thread {
Tratemant_Dic() {
}
public void run() {
try {
BufferedReader file_corpus = new BufferedReader(
new InputStreamReader(new FileInputStream(
"corpus-medical.TXT"), "UTF-16LE"));
PrintWriter ecrire = new PrintWriter("sort.html");
String line;
String nom = null;
ecrire.write("<mot><span style=\"color:red\">startsss</span></mot></br><ligne>start\n");
while ((line = file_corpus.readLine()) != null) {
BufferedReader file_nom = new BufferedReader(
new InputStreamReader(new FileInputStream(
"Fichie_sorte.DIC"), "UTF-16LE"));
while ((nom = file_nom.readLine()) != null) {
nom = nom.substring(0, nom.length() - 3);
Pattern p = Pattern.compile("(.*)\\W+" + nom + "\\b.*",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line);
if (m.find()) {
System.out.println(nom + "==>" + line);
ecrire.write("<mot><span style=\"color:red\">" + nom
+ "</span></mot></br><ligne>" + line + "\n");
}
}
file_nom.close();
}
ecrire.close();
System.out.println("FIN");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
If I understand what you are trying to do correctly, I would not use regular expressions to do it. They're slow and you do not need them.
This is really a string matching problem. Your dictionary should probably be stored in a hash table, using the hashCode() method to get a key for the string. You then search in your dictionary for each word as you read it ( calculating the appropriate hash code as you read it ) from the text. Properly done that should be as fast as it gets.
Remember that hash codes are not guaranteed to be unique, so always make sure the actual strings match even if the hash code is found in the table.
I would start by attempting to time each of the "things" your application does than then target the slowest item (as mentioned in a comment by Jay, one issue in your case is the fact you are loading the dictionary every time) rather than base the improvements on a guess of what is wrong (the regex being slow).
You can use System.nanoTime() or one of the many stopwatches to do this. I normally use guava.
Why you not use instead of
Pattern p = Pattern.compile("(.*)\\W+" + nom + "\\b.*",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line);
if (m.find()) {
...
just
if(line.indexOf(nom) > -1) {
...
?
Update: if you need word boundary stuff use something:
String lineToLowerCase = line.toLowerCase(); // before second while
...
int index = lineToLowerCase.indexOf(nom.toLowerCase());
if(index > -1) {
if(index ==0 || Character.isWhitespace(lineToLowerCase.charAt(index-1))) {
int indexEnd = index + nom.length();
if (indexEnd >= lineToLowerCase.length() || !Character.isAlphabetic(lineToLowerCase.charAt(indexEnd))) {
...
for testing
public static void main(String[] s) {
check("skdc s dcd dsf", "dcd"); // print true
check("skdc sdcd dsf", "dcd"); // print false
check("dcd dsf", "dcd"); // print true
check("afasa dcd", "dcd"); // print true
check("afasa dCD11", "dcD"); // print true
check("skdc s dcda dsf", "dcd"); // print false
}
public static void check(String line, String nom) {
String lineToLowerCase = line.toLowerCase();
int index = lineToLowerCase.indexOf(nom.toLowerCase());
if(index > -1) {
if(index ==0 || Character.isWhitespace(lineToLowerCase.charAt(index-1))) {
int indexEnd = index + nom.length();
if (indexEnd >= lineToLowerCase.length() || !Character.isAlphabetic(lineToLowerCase.charAt(indexEnd))) {
System.out.println("true");
return;
}
}
}
System.out.println("false");
}

Java - NumberFormatException at linear search

I am having this issue with the NumberFormatException in my program. Basically, I am asked to read a .csv file separated by ; and it looks like this:
// Column Explanation (not in .csv file)
id; Summary; Number; Employee1; Employee2; ....... Employee7;
"1";"Sony";"1600";"Markos";"Nikos";"Antonis";"Nikolas";"Vaggelis";"Markos";"Thanasis";
"2";"HP";"1000";"Marios";"Dimitra";"Nikolia";"Spiros";"Thomas";"Kostas";"Manolis";
"3";"Dell";"1100";"Antonis";"Aggelos";"Baggelis";"Nikos";"Kuriakos";"Panagiotis";"Rafail";
"4";"Acer";"2000";"Marina";"Aggelos";"Spiros";"Marinos";"Xristos";"Antreas";"Basilis";
What I have already done is create a String 2-d array or the .csv file called temp_arr and I am asked to write a method that will run a linear search by id and return that company. So here is the thing.
At first, I thought I should convert the input key from int -> String since my temp_arr is a String and compares the strings (which at that time they would be int but read as Strings) using temp_arr[value][value2].equals(string_key). But I had a NullPointerException.
Then I thought I should better convert my Id's from the temp_arr from String -> Int and then compare with the integer key using == operand. This action returned me a NumberFormatException.
The process is this:
System.out.println("Enter id :");
Scanner input = new Scanner(System.in);
int item = input.nextInt(); // read the key which is an Integer
int id_int; // temp_arr is String and item is int, must convert ids from String -> int
for (int i = 0; i < temp_arr.length; i++)
{
id_int = Integer.parseInt(temp_arr[i][0]); // Convert from String to int
if (id_int == item) // If the Array's Id's are == item
{
System.out.println(item+" is present at location " + (i+1) );
break;
}
if (i == temp_arr.length)
System.out.println(item + " does not exist");
}
My error appears at line 7 and I do not know why.
Read File process:
String csvFile = "sam.csv"; // .csv file to be placed in the project file!
BufferedReader br = null; // ini
String line = "",cvsSplitBy = ";"; // columns asked to be split by ";"
String[] arr = null;
String[][] temp_arr = new String[1000][10];
int temp = 0;
try
{
br = new BufferedReader(new FileReader(csvFile)); //start reading the file
while ((line = br.readLine()) != null) // while the line has words
{
arr = line.split(cvsSplitBy); // creating the array
System.out.println(arr[0] + "\t" + arr[1] + "\t" + arr[2] + "\t" + arr[3] + "\t" + arr[4] + "\t" + arr[5] + "\t" + arr[6] + "\t" + arr[7] + "\t" + arr[8] + "\t" + arr[9] );
for (int i = 0; i<=9; i++)
{
temp_arr[temp][i] = arr[i]; // temp_arr represents (is a copy of) the .csv file
}
temp++;
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (br != null)
{
try
{
br.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
System.out.println("Done!\n");
Output (Image) :
Line 106 which is causing the issue is :
id_int = Integer.parseInt(temp_arr[i][0]); // Convert from String to int
Your issue is that your Integer.parseInt() is trying to parse a "2" WITH QUOTATION MARKS. That's the problem.
A quick solution would be to replace this line:
temp_arr[temp][i] = arr[i];
To this:
temp_arr[temp][i] = arr[i].replaceAll("\"", "");
Anyway, I'd like to suggest using a different data structure for your case, because I've done something like this before for a client. Have you ever heard of HashMaps? You can do something like a HashMap with an int key and String[] values to store your data in, and the key can be your id_int. Maybe you can try this implementation next time. It's a lot more elegant.
Hope I was able to help!
Cheers,
Justin
Would help if you also posted some of your data file and how you are reading it in.
But, my guess from what is presented is if you add System.out.println(temp_arr[i][0]) prior to the 7th line or run this code through a debugger you will see that temp_arr[i][0] is not an integer value as that is what the error is telling you.

Categories