简体   繁体   English

在Java中使用charAt

[英]Using charAt in java

This is my assignment: 这是我的任务:

Write a program where the user enters a string, and the program echoes it to the monitor with one character per line: 在用户输入字符串的地方编写一个程序,该程序以每行一个字符的形式将其回显到监视器:

C:\>java LinePerChar
Enter a string:
Octopus

O
c
t
o
p
u
s

I have tried, but I'm getting some compilation errors. 我已经尝试过,但是遇到一些编译错误。 Here's my code: 这是我的代码:

import java.util.*;

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

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string :");

        try {
            String inputString = sc.nextLine();
            for(int i=0; i < sc.length(); i++) {
                char c = inputString.charAt(i);
                System.out.println("" + c);
            }
        } catch(IOException e) {

        }
    }
}

In your loop, you should be looping over the length of the String that you get from the Scanner.nextLine , not the scanner itself. 在循环中,应该循环遍历从Scanner.nextLine获得的String的长度,而不是扫描器本身。

for(int i=0; i<inputString.length(); i++){

If you want the input to be echoed with each character on the same line, use System.out.print instead of println . 如果希望在同一行上的每个字符都回显输入,请使用System.out.print而不是println

Two Issues: 两个问题:

Change for(int i=0; i<sc.length(); i++){ to for(int i=0; i<inputString.length(); i++){ for(int i=0; i<sc.length(); i++){更改for(int i=0; i<inputString.length(); i++){

You care comparing against the scanner and not the input string. 您需要比较扫描器而不是输入字符串。

Also, please try catching 另外,请尝试赶上

   java.util.NoSuchElementException
   java.lang.IllegalStateException

in place of IOException , as your statement sc.nextLine() with throws NoSuchElementException and IllegalStateException , not IOException . 代替IOException ,因为您的语句sc.nextLine()抛出NoSuchElementExceptionIllegalStateException ,而不是IOException

Make sure you add the related import statements. 确保添加相关的导入语句。

You need to import IOException . 您需要导入IOException Add this line to the top of your code, just after the package line if you have one: 如果您有以下代码,请将此行添加到代码顶部,紧接在package行之后:

import java.io.IOException;

Also, you're asking sc for a length instead of the string, so change your for to this: 另外,您要的是sc而不是字符串的长度,因此请更改for

for(int i = 0; i < inputString.length(); i++) {

Really though, you shouldn't be catching IOException . 确实,您不应捕获IOException In fact, your code will never throw an exception. 实际上,您的代码将永远不会引发异常。 This is really all you need: 这实际上就是您所需要的:

public static void main(String args []){

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter a string :");

    String inputString = sc.nextLine();
    for(int i=0; i < sc.length(); i++) {
        char c = inputString.charAt(i);
        System.out.println("" + c);
    }
}

Calling nextLine on a Scanner made with System.in will only throw an exception if System.in isn't accessible, and it won't even be an IOException , so don't worry about it. 如果无法访问System.in则在用System.in生成的Scanner上调用nextLine只会引发异常,甚至不会成为IOException ,因此不必担心。

One final observation, you don't need to do "" + c in your println . 最后的观察,您不需要在println执行"" + c System.out has a println method specifically for char , so you can just call: System.out具有专门用于charprintln方法,因此您可以调用:

System.out.println(c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM