mardi 22 novembre 2016

How to fix this non-compiling method?

Doing some practice exam questions, and it says to "assume the statement "String pattern = getPattern();" Explain if any flow in the method getPattern(). How would you fix it?"

Here's the code:

public static String getPattern() {
  Scanner inPattern = new Scanner(System.in);
  String pattern = " ";
  boolean valid = false;
  int i = 0;
  while(!valid){
    System.out.println("please enter a valid pattern with X or x");
    pattern = inPattern.next();
 if ( !  (pattern.charAt(i) == 'X' || pattern.charAt(i) == 'x'
    || pattern.charAt(i) == 'r'))
    System.out.println("You have entered an invalid pattern");
else if ((i + 1) == pattern.length()) valid = true;
};
inPattern.close();
return pattern;
}

I'm not really sure how I would fix this... obviously this is a smaller part of a bigger code because this doesn't include a main method, personally making it a bit more difficult to see what's wrong. I'm really not sure exactly what to change here. I know this may seem like a dumb question but I've been up for 7+ hours watching youtube videos and attempting to understand this stuff or to do this question but I really cannot figure it out :( would anyone be able to provide a good example?

Aucun commentaire:

Enregistrer un commentaire