Java中打印字符串变量

发布于 2024-12-17 12:27:11 字数 751 浏览 0 评论 0原文

运行(看似简单)代码时,我得到一些奇怪的输出。这就是我所拥有的:

import java.util.Scanner;

public class TestApplication {
  public static void main(String[] args) {
    System.out.println("Enter a password: ");
    Scanner input = new Scanner(System.in);
    input.next();
    String s = input.toString();
    System.out.println(s);
  }
}

编译成功后得到的输出是:

Enter a password: 
hello
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=5][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

这有点奇怪。发生了什么以及如何打印 s 的值?

I'm getting some weird output when running (seemingly simple) code. Here's what I have:

import java.util.Scanner;

public class TestApplication {
  public static void main(String[] args) {
    System.out.println("Enter a password: ");
    Scanner input = new Scanner(System.in);
    input.next();
    String s = input.toString();
    System.out.println(s);
  }
}

And the output I get after compiling successfully is:

Enter a password: 
hello
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=5][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

Which is sort of weird. What's happening and how do I print the value of s?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

天气好吗我好吗 2024-12-24 12:27:12

您得到的是 Scanner 对象本身返回的 toString() 值,这不是您想要的,也不是您使用 Scanner 对象的方式。您想要的是 Scanner 对象获取的数据。例如,

Scanner input = new Scanner(System.in);
String data = input.nextLine();
System.out.println(data);

请阅读有关如何使用它的教程,因为它将解释所有内容。

编辑
请查看此处:扫描仪教程

另请查看 < a href="http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html" rel="noreferrer">Scanner API 它将解释一些细节Scanner 的方法和属性。

You're getting the toString() value returned by the Scanner object itself which is not what you want and not how you use a Scanner object. What you want instead is the data obtained by the Scanner object. For example,

Scanner input = new Scanner(System.in);
String data = input.nextLine();
System.out.println(data);

Please read the tutorial on how to use it as it will explain all.

Edit
Please look here: Scanner tutorial

Also have a look at the Scanner API which will explain some of the finer points of Scanner's methods and properties.

爱*していゐ 2024-12-24 12:27:12

您还可以使用 BufferedReader

import java.io.*;

public class TestApplication {
   public static void main (String[] args) {
      System.out.print("Enter a password: ");
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String password = null;
      try {
         password = br.readLine();
      } catch (IOException e) {
         System.out.println("IO error trying to read your password!");
         System.exit(1);
      }
      System.out.println("Successfully read your password.");
   }
}

You could also use BufferedReader:

import java.io.*;

public class TestApplication {
   public static void main (String[] args) {
      System.out.print("Enter a password: ");
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String password = null;
      try {
         password = br.readLine();
      } catch (IOException e) {
         System.out.println("IO error trying to read your password!");
         System.exit(1);
      }
      System.out.println("Successfully read your password.");
   }
}
叫嚣ゝ 2024-12-24 12:27:12
input.next();
String s = input.toString();

将其更改为

String s = input.next();

也许这就是您想要做的。

input.next();
String s = input.toString();

change it to

String s = input.next();

May be that's what you were trying to do.

独木成林 2024-12-24 12:27:12

这更有可能让你得到你想要的:

Scanner input = new Scanner(System.in);
String s = input.next();
System.out.println(s);

This is more likely to get you what you want:

Scanner input = new Scanner(System.in);
String s = input.next();
System.out.println(s);
小梨窩很甜 2024-12-24 12:27:12

您打印了错误的值。相反,如果您打印扫描仪对象的字符串。试试这个

Scanner input = new Scanner(System.in);
String s = input.next();
System.out.println(s);

You are printing the wrong value. Instead if the string you print the scanners object. Try this

Scanner input = new Scanner(System.in);
String s = input.next();
System.out.println(s);
固执像三岁 2024-12-24 12:27:12

如果您已尝试了所有其他答案,但仍然无效,您可以尝试跳过一行:

Scanner scan = new Scanner(System.in);
scan.nextLine();
String s = scan.nextLine();
System.out.println("String is " + s);

If you have tried all the other answers, and it still hasn't work, you can try skipping a line:

Scanner scan = new Scanner(System.in);
scan.nextLine();
String s = scan.nextLine();
System.out.println("String is " + s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文