1

I am new to Java so spare me. Below you can see my code. What it should do is read the 3th column from text file and if this column is S**ei or P***ei it returns the first word in that line. However my question is "How can I make * match any character from a to z"?. I heard of regular expressions but haven't really worked with them yet. Any help would be much appreciated. Thanks.

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class moja {
    public static void main(String[] args) {
        try {
            File file = new File("SloveneLexicon.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            StringBuffer stringBuffer = new StringBuffer();
            String vrstica;
            while ((vrstica = bufferedReader.readLine()) != null) {

                String s = vrstica;
                String[] dobi_besedo_v_vrstici = s.split("\\s+");
                String prva_beseda = dobi_besedo_v_vrstici[0];
                String tretja_beseda = dobi_besedo_v_vrstici[2];
                if (tretja_beseda =="S**ei"){
                    System.out.println(prva_beseda);
                    if (tretja_beseda =="P***ei")
                        System.out.println(prva_beseda);
                }

            }
            bufferedReader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Rok Ivartnik
  • 137
  • 5
  • 17
  • 1
    Please read [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) (Hint `equals` rather than `==`) – reto Oct 22 '14 at 07:44
  • `tretja_beseda =="S**ei"` :___( – Maroun Oct 22 '14 at 07:44
  • 1
    Why the hell was this closed as duplicate? It's not about string comparison, it's about regex matching a string. – Pimgd Oct 22 '14 at 07:46
  • Not sure what you mean by "read the 3th collum from text file and if this colum is S\*\*ei or P\*\*\*ei". Do you mean "If there is an asterisk '*' in the third column? – martin jakubik Oct 22 '14 at 07:46
  • 1
    @Pimgd You're right, I reopened it. – Maroun Oct 22 '14 at 07:46
  • 1
    Indeed. The string comparison issue was just a minor problem. To Rok Ivartnik: [Regular expressions tutorial](http://docs.oracle.com/javase/tutorial/essential/regex/) – RealSkeptic Oct 22 '14 at 07:47
  • no if there is a string lets say Saaei or Sedei or Padvei, however as you can se S**ei needs to occour other 2 marked as * can be any characters from a-z – Rok Ivartnik Oct 22 '14 at 07:49

3 Answers3

1

Take a look at regex pattern matcher :

manual : http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Example :

 Pattern p = Pattern.compile("a*b");
 Matcher m = p.matcher("aaaaab");
 boolean b = m.matches();
Margus
  • 19,694
  • 14
  • 55
  • 103
  • The original problem statement seems to be "HOW CAN I MAKE * MATCHING any character from a - z", so their `"S**ei"` would be matched with `"S[a-z]{2}ei"`. – Ken Y-N Oct 22 '14 at 07:52
1
    Pattern p = Pattern.compile("Pi[a-zA-z]{3}ei");
    if(p.matcher(input).matches()){
        This will work for 3 any letters (big or small)
    }
  • "Pi[a-zA-z]{3}ei" // 3 letters big or small
  • "Pi[a-zA-z]{1,3}ei" // 1-3 letters big or small
  • "Pi[a-zA-z]+ei" // at least one letter
  • "Pi[a-zA-z]*ei" // zero or more letters

Just remember to put your Pattern outside while loop, you should define it once and use many times

Beri
  • 11,470
  • 4
  • 35
  • 57
0

Use a pattern matcher (e. g. String.matches()):

    if (tretja_beseda.matches("S[a-zA-Z][a-zA-Z]ei")) {
        System.out.println(prva_beseda);
    }
    if (tretja_beseda == "P[a-zA-Z][a-zA-Z]ei") {
        System.out.println(prva_beseda);
    }

[a-zA-Z] matches any character from a-z (case insensitive). I'm reading SloveneLexicon.txt in your code so I guess you'll also have to deal with slovenian characters (like Č). I'd suggest you to use \\p{L} (matching one unicode letter) instead of [a-zA-Z]:

    if (tretja_beseda.matches("S\\p{L}\\p{L}ei")) {
        System.out.println(prva_beseda);
    }

Second, your if-logic can not work since you nested the second if inside the first, but both conditions can't be true at the same time (String starting with S and P).

Third, I'd suggest you to program in english so you don't mix languages in your code making it easier to read; but that's of course up to you.

steffen
  • 16,138
  • 4
  • 42
  • 81
  • č is not a problem since there are no č,š,ž in treja_beseda – Rok Ivartnik Oct 22 '14 at 08:04
  • and how can mine if be both true at the same time since i am reading 1 line and only P**ei or S***ei can accour once in every line – Rok Ivartnik Oct 22 '14 at 08:07
  • 1
    The second `if` is only evaluated if the first `if` was true. Your code: `if (cond1) { code1(); if (cond2) { code2; } }`. Compare this: `if (cond1) { code1(); } if (cond2) { code2(); }` – steffen Oct 22 '14 at 08:12
  • hello if you can help me if (tretja_beseda.matches("S\\p{L}\\p{L}ei")) what would i have to put behind ei to have 0 or infinite word possible icludint š,č. Is this ok? (tretja_beseda.matches("S\\p{L}\\p{L}ei\\p{L}*")) – Rok Ivartnik Oct 26 '14 at 20:20
  • @RokIvartnik Yes, that would match all letters after `ei` (0..∞ times). – steffen Oct 27 '14 at 08:00