0

Array doesn't change after running this code. What is the reason of it? Thank you

 Scanner s = new Scanner(System.in);
 String [] h = new String[100];
 int hlds = 0;
 while (true) {
 System.out.print("Enter: ");
 if(s.hasNextLine()) {
 String str = s.nextLine();
 if (Pattern.matches("[abc]", str)) {
 h[hlds++] = str;
 }
 for( int i = 0; i < h.length ; i++){
 System.out.println(h[i]);
 }
 break;
 }
Alan Moore
75.6k13 gold badges110 silver badges161 bronze badges
asked Sep 15, 2013 at 11:16
5
  • 2
    What are you trying to achieve with this code? Commented Sep 15, 2013 at 11:19
  • I need to write down strings in array , which contain letters abc Commented Sep 15, 2013 at 11:24
  • 1
    Probably because if (Pattern.matches("[abc]", str)) is never met. Commented Sep 15, 2013 at 11:24
  • So, how i need to change it? Commented Sep 15, 2013 at 11:27
  • I have updated my answer, so you want strings which contains letter a, b, or c in the input line. Commented Sep 15, 2013 at 19:16

4 Answers 4

1
Pattern.matches("[abc]", str)

Evaluates to true only if you enter either a or b or c

because of the regex you have used [abc], see the docs about regular expressions

If you enter ab then it would not be accepted.

If you want your input to contain any of the char then you could change your regex to [abc]+.

answered Sep 15, 2013 at 11:27
Sign up to request clarification or add additional context in comments.

Comments

1

Your regex [abc] means "a single character either a, b or c".

Change your regex to [abc]+, meaning "one or more characters either a, b or c"

answered Sep 15, 2013 at 11:27

Comments

0

Extra Info:
This would also work:

str.matches("[abc]+");

It calls Pattern.matches(regex,this); internally. (where regex is the regular expression used)

answered Sep 15, 2013 at 11:34

Comments

0

(updated after reading all comments...)

Okey, so if I understand it right: You want to store into the array from input line those who contains a, b or c letters.

apple, ball, catch, table, tictac... will be stored. Right?

I would use the String contains, or indexof to fins a, b, and c letters. This is more efficient that regex.

Scanner s = new Scanner(System.in);
String [] h = new String[10];
Pattern p = Pattern.compile("(a|b|c)");
for(int hlds=0; hlds<h.length;hlds++ ) {
 System.out.print("Enter: ");
 String str = s.nextLine();
 /* with regex
 if( p.matcher(str).find() ) {
 h[hlds] = str;
 }
 */
 /* with contains */
 if( str.contains("a") || str.contains("b") || str.contains("c") ) {
 h[hlds] = str;
 }
}
System.out.println(Arrays.toString(h));
answered Sep 15, 2013 at 11:29

2 Comments

The loop is ok, but OP wants strings that match a pattern, not that all are equal to abc and ending with an array of 100 abcs.
No. I'm just trying to find strings, which contain a, b or c and write them down to an array.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.