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();
        }
    }
}
7
  • 1
    Please read How do I compare strings in Java? (Hint equals rather than ==) Commented Oct 22, 2014 at 7:44
  • tretja_beseda =="S**ei" :___( Commented Oct 22, 2014 at 7:44
  • 1
    Why the hell was this closed as duplicate? It's not about string comparison, it's about regex matching a string. Commented Oct 22, 2014 at 7:46
  • 1
    @Pimgd You're right, I reopened it. Commented Oct 22, 2014 at 7:46
  • 1
    Indeed. The string comparison issue was just a minor problem. To Rok Ivartnik: Regular expressions tutorial Commented Oct 22, 2014 at 7:47

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help:) Good luck in improving your skills:)
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();

1 Comment

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".
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.

5 Comments

č is not a problem since there are no č,š,ž in treja_beseda
and how can mine if be both true at the same time since i am reading 1 line and only Pei or S*ei can accour once in every line
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(); }
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}*"))
@RokIvartnik Yes, that would match all letters after ei (0..∞ times).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.