如何使用indexOf?请帮助初学者

时间:2013-12-24 07:52:00

标签: java methods char java.util.scanner

所以我想知道indexOf()的作用。因为我想在我的程序中使用它,它会找出用户输入的单词中有多少个元音。

public static boolean methodCheck(char a){
       return "AEIOUaeiou".indexOf(a) != -1;
}

但这似乎根本不起作用哈哈。因为我不知道indexOf()实际上做了什么。无论如何这里是我的程序到目前为止(对不起,如果它不好,我真的是新的)。我也留下了5个问题,这对我很有帮助!拜托,谢谢你的帮助:D

import java.util.Scanner;



public class vowelCounter {


private static String input = methodInput(); //1. is there any other way to make a global Scanner?


public static void main(String[] args){



    System.out.println("Enter word");
    System.out.println(input);
    System.out.println("This word has" + methodCheck('a')); //2. what should i put in my parameters? 


}

public static boolean methodCheck(char a){

return "AEIOUaeiou".indexOf(a) != -1; //3. what does this line do?


}

public static String methodInput(){


    Scanner keyboard = new Scanner(System.in);
    String input = keyboard.nextLine();

    return input;

        //4. the output is 'hastrue' why is that?
        //5. how can i make this program better?

}

}

6 个答案:

答案 0 :(得分:1)

如果您不知道方法的作用,那么解决方案就是去了解它的作用。例如,java documentation会告诉您

  

public int indexOf(int ch)

     

返回第一次出现的指定字符

的字符串中的索引      

在任何一种情况下,如果此字符串中没有出现此类字符,则返回-1。

如果找不到该字符,考虑该方法如何返回-1,如何使用它并不一定是错误的。但是如果你想检查用户输入的单词中有多少个元音,那么检查他们输入的单词是否在元音串中是不正确的。

答案 1 :(得分:0)

所有标准的Java库,类和方法都有Javadoc来描述它们的作用。

您需要做的就是查找Javadoc并描述它。

在这种情况下,Javadoc位于:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)

您对此类问题的第一步应始终是文档,如果不起作用,请尝试进行网络搜索以查找示例。例如谷歌5秒内放入“java indexOf example”发现我: http://www.tutorialspoint.com/java/java_string_indexof.htm

如果这不起作用,你可以尝试在这里提问。

答案 2 :(得分:0)

如果在方法名称前面有单词boolean,则表示该方法将返回值true或值false。这是您的程序打印出来的truefalse值,与“此词有”在同一行。

如果传递给它的字符是元音,则此特定方法将返回true,否则返回false。方法indexOf告诉您String的哪个字符是第一个与您传入方法的值相等的字符。它为第一个字符返回0,为第二个字符返回1,依此类推。如果没有任何字符匹配,则返回-1。在这种情况下,您只是检查indexOf返回的值是否为-1 - 换句话说,该字符是否在String "AEIOUaeiou"中。

答案 3 :(得分:0)

indexOf(String str)返回指定子字符串第一次出现的字符串中的索引。如果不存在str的此类值,则返回-1。

例如:

  

int num1 =" AEIOUaeiou" .indexOf(" a"); //它给出5

     

int num2 =" AEIOUaeiou" .indexOf(" A"); //它给0

     

int num3 =" AEIOUaeiou" .indexOf(" z"); //它给-1

答案 4 :(得分:0)

1 不要这样做!在main中创建一个扫描程序,用它读取输入,然后调用你的方法。

2 countVowels(input)怎么样?您需要编写static int countVowels(String input)方法。

3 自传入'a'后返回true。

4 请参阅编号 3

5 查看数字 2 ,然后添加static boolean isVowel(char a)

答案 5 :(得分:0)

这是indexOf方法的作用

string.indexOf(searchvalue,start)

<强>参数

searchvalue : Required. The string to search for 
start       : Optional. Default 0. At which position to start the search

返回值

Number : The position where the specified searchvalue occurs for the first time, or -1 if it never occurs

简单来说,方法索引检查从起始位置(如果指定)传递给它的值的第一次出现,并返回在字符串中第一次遇到该值的位置。

例如。

String s = "AEIOUaeiou";
s.indexOf("a");     //This would get a value of 5.
s.indexOf("v");     //This would get a value of -1, since it doesn't have the character v

回答你的问题,

  1. 您可以直接将扫描程序声明为私有,并在中使用它 整个计划

    `private static Scanner input = new Scanner(System.in);`
    
  2. 您可以编写一个接收用户输入的String的方法 然后检查String是否包含任何元音。您可以 使用indexOf或包含检查每个元音的方法 indexOf方法。

  3. 已经在上面描述过了。

  4. 更好的方法如下:

  5. public class vowelCounter {

        public static void main (String[] args) {
          Scanner keyboard = new Scanner (System.in); // No need to declare it as global. You use it only once.
          System.out.println ("Enter word : ");   //Prompt the user to enter a word    
          String input = keyboard.nextLine ();  //Fetch the word that the user enters into a String 
          System.out.println ("This word has" + countVowel (input)); // Pass the string to the method to check if it has vowels.
        }
    
       private static int countVowel (String a) {
         int count = 0;
         String s =  a.toLowerCase ();    // convert the string to lower case so that you only have to check for the lower case characters
        // Here you would need to check the number of times each vowel exists in the String and incremenet the count everytime.
        return count;
       }  
    }
    
相关问题