字符和字符串输入在Java中

huangapple go评论80阅读模式
英文:

Character and String input in Java

问题

以下是翻译好的代码部分:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        char nextChar() {
            char c = ' ';
            try {
                c = (char)br.read();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return c;
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        FastReader in = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        int n = in.nextInt();
        char c = in.nextChar();
        String s = in.nextLine();
        out.println(n);
        out.println(c);
        out.println(s);
        out.close();
    }
}
英文:

I am new to Java and wrote a program for basic I/O operations of different data types. I want the program to input 1 a abcd and output them in in three different lines respectively. But when I input 1 a the program terminates and outputs 1 , a and an empty line in three different lines. I am not able to take a character input properly, which I think is the root of the problem.
Can someone please guide me as to where I got it wrong ?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException  e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
char c = ' ';
try {
c = (char)br.read();
}
catch (IOException e) {
e.printStackTrace();
}
return c;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
char c = in.nextChar();
String s = in.nextLine();
out.println(n);
out.println(c);
out.println(s);
out.close();
}
}

答案1

得分: 0

将代码重构如下。

char c = in.nextChar(); // 保持这一行不变
in.nextLine(); //(仅在上一行之后添加这一行)将这一行放在其他位置,否则无法插入字符串abcd


输出:

1
a
abcd


<details>
<summary>英文:</summary>
Refactor the code like below.

char c = in.nextChar();//Keep this line as same
in.nextLine();//(only add this line after the above line)Place this line otherwise your String abcd can't insert

output:

1
a
abcd


</details>
# 答案2
**得分**: 0
你的意思是像下面这样:
```plaintext
输入:1 a abcd
输出:
1
a
abcd

你引用了这段代码:

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String inputLine = bufferedReader.readLine();
        if (inputLine != null && inputLine.length() > 0) {
            String[] splitInputLine = inputLine.split(" ");
            for (String outLine : splitInputLine) {
                System.out.println(outLine);
            }
        }
        bufferedReader.close();
    }
英文:

you mean like follow:

input: 1 a abcd
out:
1
a
abcd

you reference this code:

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String inputLine = bufferedReader.readLine();
        if (inputLine!=null &amp;&amp; inputLine.length()&gt;0) {
            String[] splitInputLine = inputLine.split(&quot;\\ &quot;);
            for (String outLine : splitInputLine) {
                System.out.println(outLine);
            }
        }
        bufferedReader.close();
    }

huangapple
  • 本文由 发表于 2020年10月22日 15:53:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64477676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定