如何在Java中返回子字符串的第二个实例?

如何在Java中返回子字符串的第二个实例?,java,Java,我正在创建一个函数,返回用户输入的第二个字符。问题是它没有打印任何东西。我记得听说过一些关于使用嵌套for循环的事情,但我不知道。这是程序;我遇到问题的函数是最后一个 import javax.swing.JOptionPane; public class IntroToWHILELoops { public static void main(String[] args) { //Thiis section tests the first me

我正在创建一个函数,返回用户输入的第二个字符。问题是它没有打印任何东西。我记得听说过一些关于使用嵌套for循环的事情,但我不知道。这是程序;我遇到问题的函数是最后一个

import javax.swing.JOptionPane;

public class IntroToWHILELoops
{
    public static void main(String[] args)
    {
        
        //Thiis section tests the first method
        String myString = GetAndStoreInput();
        System.out.println(myString);
        
        //This section tests the second method
        System.out.println("Enter a character to search for --> ");
        String searchChar = System.console().readLine();
        int position = SecondCharPosition(myString,searchChar);
        System.out.println("The second position of " +searchChar +" is " +position);
                
    }
    
    //This method keeps getting input from the user until a "X" or "x" is entered
    //It then stores the input in a string.
    public static String GetAndStoreInput()
    {
      String input;
      String sentence = "";
      
      input = JOptionPane.showInputDialog("Enter something (press x to stop)");

        while(!input.equals("x")  && !input.equals("X") ){
        System.out.println(input);
        
        sentence = sentence + input;
        input = JOptionPane.showInputDialog("Enter something else (press x to stop)");
      }
      return sentence;
        
    }
    
    
    //Given a string, this method returns the position of the second occurrence of a given character.
    //If the character occurs less than 2 times it returns -1.
    public static int SecondCharPosition(String str, String Charr)
    {
      String sentence = str;
      int count = 1;
      int position = 0;
      int i = 0;
      while(count < 10){
          if(sentence.substring(i,i+1).equals(Charr)){
          count++;
        }
          if(count == 2){
          position = i;
        }
      }
      return position;
    }

}
import javax.swing.JOptionPane;
公共课程介绍Hillelops
{
公共静态void main(字符串[]args)
{
//本节测试第一种方法
字符串myString=GetAndStoreInput();
System.out.println(myString);
//本节测试第二种方法
System.out.println(“输入要搜索的字符-->”;
字符串searchChar=System.console().readLine();
int position=SecondCharPosition(myString,searchChar);
System.out.println(“第二个“+searchChar+”位置是“+position”);
}
//此方法一直从用户获取输入,直到输入“X”或“X”
//然后将输入存储在字符串中。
公共静态字符串GetAndStoreInput()
{
字符串输入;
字符串句子=”;
input=JOptionPane.showInputDialog(“输入内容(按x键停止)”);
而(!input.equals(“x”)和&!input.equals(“x”)){
系统输出打印项次(输入);
句子=句子+输入;
input=JOptionPane.showInputDialog(“输入其他内容(按x键停止)”);
}
返回判决;
}
//给定一个字符串,此方法返回给定字符第二次出现的位置。
//如果字符出现少于2次,则返回-1。
公共静态int SecondCharPosition(字符串str、字符串Charr)
{
字符串句子=str;
整数计数=1;
int位置=0;
int i=0;
同时(计数<10){
if(句子.子串(i,i+1).等于(Charr)){
计数++;
}
如果(计数=2){
位置=i;
}
}
返回位置;
}
}

@Tyler-希望这对你有所帮助

import java.util.Scanner;

import javax.swing.*;

class Scratch {
    public static void main(String[] args) {

        // Thiis section tests the first method
        String myString = GetAndStoreInput();
        System.out.println(myString);

        // This section tests the second method
        System.out.println("Enter a character to search for --> ");
        final Scanner in = new Scanner(System.in);
        String searchChar = in.next();
        int position = SecondCharPosition(myString, searchChar);
        System.out.println("The second position of " + searchChar + " is " + position);

    }

    // This method keeps getting input from the user until a "X" or "x" is entered
    // It then stores the input in a string.
    public static String GetAndStoreInput() {
        String input;
        String sentence = "";

        input = JOptionPane.showInputDialog("Enter something (press x to stop)");

        while (!input.equals("x") && !input.equals("X")) {
            System.out.println(input);

            sentence = sentence + input;
            input = JOptionPane.showInputDialog("Enter something else (press x to stop)");
        }
        return sentence;

    }

    // Given a string, this method returns the position of the second occurrence of a given character.
    // If the character occurs less than 2 times it returns -1.
    public static int SecondCharPosition(String str, String Charr) {
        int count = 0;
        int position = 0;
        for (int i = 0; i < str.length(); i++) {
            if (String.valueOf(str.charAt(i)).equals(Charr))
                count++;

            if (count == 2) {
                position = i;
                break;
            }
        }
        return position;
    }
}

import java.util.Scanner;
导入javax.swing.*;
课堂擦伤{
公共静态void main(字符串[]args){
//本节测试第一种方法
字符串myString=GetAndStoreInput();
System.out.println(myString);
//本节测试第二种方法
System.out.println(“输入要搜索的字符-->”;
最终扫描仪输入=新扫描仪(系统输入);
字符串searchChar=in.next();
int position=SecondCharPosition(myString,searchChar);
System.out.println(“第二个“+searchChar+”位置是“+position”);
}
//此方法一直从用户获取输入,直到输入“X”或“X”
//然后将输入存储在字符串中。
公共静态字符串GetAndStoreInput(){
字符串输入;
字符串句子=”;
input=JOptionPane.showInputDialog(“输入内容(按x键停止)”);
而(!input.equals(“x”)和&!input.equals(“x”)){
系统输出打印项次(输入);
句子=句子+输入;
input=JOptionPane.showInputDialog(“输入其他内容(按x键停止)”);
}
返回判决;
}
//给定一个字符串,此方法返回给定字符第二次出现的位置。
//如果字符出现少于2次,则返回-1。
公共静态int SecondCharPosition(字符串str、字符串Charr){
整数计数=0;
int位置=0;
对于(int i=0;i
@Tyler-希望这对你有所帮助

import java.util.Scanner;

import javax.swing.*;

class Scratch {
    public static void main(String[] args) {

        // Thiis section tests the first method
        String myString = GetAndStoreInput();
        System.out.println(myString);

        // This section tests the second method
        System.out.println("Enter a character to search for --> ");
        final Scanner in = new Scanner(System.in);
        String searchChar = in.next();
        int position = SecondCharPosition(myString, searchChar);
        System.out.println("The second position of " + searchChar + " is " + position);

    }

    // This method keeps getting input from the user until a "X" or "x" is entered
    // It then stores the input in a string.
    public static String GetAndStoreInput() {
        String input;
        String sentence = "";

        input = JOptionPane.showInputDialog("Enter something (press x to stop)");

        while (!input.equals("x") && !input.equals("X")) {
            System.out.println(input);

            sentence = sentence + input;
            input = JOptionPane.showInputDialog("Enter something else (press x to stop)");
        }
        return sentence;

    }

    // Given a string, this method returns the position of the second occurrence of a given character.
    // If the character occurs less than 2 times it returns -1.
    public static int SecondCharPosition(String str, String Charr) {
        int count = 0;
        int position = 0;
        for (int i = 0; i < str.length(); i++) {
            if (String.valueOf(str.charAt(i)).equals(Charr))
                count++;

            if (count == 2) {
                position = i;
                break;
            }
        }
        return position;
    }
}

import java.util.Scanner;
导入javax.swing.*;
课堂擦伤{
公共静态void main(字符串[]args){
//本节测试第一种方法
字符串myString=GetAndStoreInput();
System.out.println(myString);
//本节测试第二种方法
System.out.println(“输入要搜索的字符-->”;
最终扫描仪输入=新扫描仪(系统输入);
字符串searchChar=in.next();
int position=SecondCharPosition(myString,searchChar);
System.out.println(“第二个“+searchChar+”位置是“+position”);
}
//此方法一直从用户获取输入,直到输入“X”或“X”
//然后将输入存储在字符串中。
公共静态字符串GetAndStoreInput(){
字符串输入;
字符串句子=”;
input=JOptionPane.showInputDialog(“输入内容(按x键停止)”);
而(!input.equals(“x”)和&!input.equals(“x”)){
系统输出打印项次(输入);
句子=句子+输入;
input=JOptionPane.showInputDialog(“输入其他内容(按x键停止)”);
}
返回判决;
}
//给定一个字符串,此方法返回给定字符第二次出现的位置。
//如果字符出现少于2次,则返回-1。
公共静态int SecondCharPosition(字符串str、字符串Charr){
整数计数=0;
int位置=0;
对于(int i=0;i
您需要递增
i
来循环字符。如果您的计数器从1开始,那么在只找到一个匹配项后,它将是2。@shawn递增i只是在执行i++,对吗?我应该把它放在哪里?Java命名约定让类以大写字母开头;方法