java - 需要一些帮助从并行数组/输出语句检索数据

标签 java

package statescapitalquizz;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* @author Steve
*/
public class Statescapitalquizz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String fileName = "Capitals.txt";
        boolean found = false;
        Scanner kb = new Scanner(System.in); 

        String target; 
        int n = 50;

        String[] states = new String[n];
        String[] capitals  = new String[n];

        try (Scanner inputStream = new Scanner(new FileInputStream(fileName))) {

            for (int i = 0; i < n; i++) {
                states[i] = inputStream.nextLine();
                capitals[i]   = inputStream.nextLine();
            }

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }


        System.out.println("Please enter the name of a state: "); 
        target = kb.nextLine();


        // the loop coninues to the end of the array if the city is not found  
        for (n=0; (!found) && (n < states.length) ; n++)  {
            if (states[n].matches(target)) {
                //print found message and set found to true
                System.out.println("The capital of " + target + " is"); //Where I need help
                found = true;
            } // end if

        } // for loop


        // after the loop – if not(found) print not found message
        if (!found)
            System.out.println(target + "is not a state in the United States");

        }
    }

好吧,我需要帮助的部分是如何检索与用户输入的“状态”相匹配的“大写字母”。我的输出语句基本上是

//print found message and set found to true
      System.out.println("The capital of " + target + " is" + ); //Where I need help
      found = true;
  } // end if

 } // for loop

而且我不知道要在输出语句中放入什么代码,因此它交叉引用我的第二个数组并从用户输入的 State 中放入匹配的 Capital 。

最佳答案

System.out.println("The capital of " + target + " is " + capitals[n]);

正如我的问题的评论中提到的,这就是答案。

关于java - 需要一些帮助从并行数组/输出语句检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33470816/

相关文章:

java - Tomcat 托管,通过 github 的 openshift,隐藏特定页面?

java - 使用 hibernate 对有序数据进行分页

java - 如何在 java 中使用 selenium webdriver 来查找输入字段是否为只读

javascript - Java servlet 无法读取 html select 标记名称

java - 防止 Glassfish 使用自己的库版本而不是项目中的库

java - 如何获取在 android(Java) 的 TextView 中添加的 SpannableStrings 列表?

java - jni4net - 如何设置 jni4net.j-0.7.1.0.jar 的绝对路径

java - 更改 Google Place Picker Appbar 文本颜色

java - 正则表达式匹配空格后跟一个或多个字符

java - 在 doStartTag 函数中获取 JSP TLIB 的自定义标记名称