在Java中读取字符串(Reading String in java)
我一直在为学校编写这个代码来创建课程,用户,学生和老师,但是我在创建课程时遇到了一些障碍。
我想要做的是(在终端中)读取输入流,例如“CSC 110 Into to java”,并将其全部输入到字符串中。 我正在使用扫描仪进行输入。 并在next()之后尝试了hasNext()和nextLine()。 我想写一个函数来使用char数组获取整个字符串,然后将char数组转换为字符串。
首先我想知道是否有更简单的解决方案?
编辑:对不起,我不清楚。 没有进入我的所有代码。
目前代码:
System.out.print("Enter the Course Title: "); title = keyboard.nextLine(); System.out.print("Enter a Brief Course Description: "); desc = keyboard.nextLine(); if (t != null) { do { System.out.println("\nPick a teacher\n"); for (int j = 0; j < t.size(); j++) { nTeacher = t.get(j); s += "\t(" + (j+1) + ") " + nTeacher + "\n"; } System.out.print(s + "\nEnter the Teacher ID: "); int tId = keyboard.nextInt(); // My error
当前错误:
Enter the Course Title: CSC 110 Into to java Enter a Breif Course Description: Pick a teacher Enter the Teacher ID: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:857) at java.util.Scanner.next(Scanner.java:1478) at java.util.Scanner.nextInt(Scanner.java:2108) at java.util.Scanner.nextInt(Scanner.java:2067) at Prototype.classCreator(Prototype.java:530) at Prototype.mainMenu(Prototype.java:181) at Prototype.login(Prototype.java:121) at Prototype.main(Prototype.java:48)
当我使用nextLine()
Create Class Enter the courseID: 27222 Enter the Course Title: Enter a Brief Course Description:
预期结果:
Create Class Enter the courseID: 27299 Enter the Course Title: CSC 110 Into to Java Enter a Brief Course Description: Introduction to programming Pick a teacher (1) ID: 1111 Name: Bob Enter the Teacher ID: 1 Class Created
我明白,当我有三个字符串“CSC 110 Intro”它得到nextInt()的输入,但我不明白为什么nextLine()正在写我的println是这样的。
I've been working on this code for school to create a course, users, students, and teachers but I ran into a snag while creating a course.
What I want to do is (in the terminal) read an input stream such as "CSC 110 Into to java" and enter all of it into a string. I am using Scanner for input. and have tried hasNext() and nextLine() besides next(). I thought about writing a function to use a char array get the entire string and then convert the char array into a string.
First I wanted to know if there was an easier solution?
Edit: Sorry I wasn't clear. Without going into all of my code.
current Code:
System.out.print("Enter the Course Title: "); title = keyboard.nextLine(); System.out.print("Enter a Brief Course Description: "); desc = keyboard.nextLine(); if (t != null) { do { System.out.println("\nPick a teacher\n"); for (int j = 0; j < t.size(); j++) { nTeacher = t.get(j); s += "\t(" + (j+1) + ") " + nTeacher + "\n"; } System.out.print(s + "\nEnter the Teacher ID: "); int tId = keyboard.nextInt(); // My error
Current error:
Enter the Course Title: CSC 110 Into to java Enter a Breif Course Description: Pick a teacher Enter the Teacher ID: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:857) at java.util.Scanner.next(Scanner.java:1478) at java.util.Scanner.nextInt(Scanner.java:2108) at java.util.Scanner.nextInt(Scanner.java:2067) at Prototype.classCreator(Prototype.java:530) at Prototype.mainMenu(Prototype.java:181) at Prototype.login(Prototype.java:121) at Prototype.main(Prototype.java:48)
When I use nextLine()
Create Class Enter the courseID: 27222 Enter the Course Title: Enter a Brief Course Description:
Intended result:
Create Class Enter the courseID: 27299 Enter the Course Title: CSC 110 Into to Java Enter a Brief Course Description: Introduction to programming Pick a teacher (1) ID: 1111 Name: Bob Enter the Teacher ID: 1 Class Created
I understand that when I have three strings "CSC 110 Intro" it get to the input for nextInt() but I don't understand why nextLine() is writing my println's like this.
原文:https://stackoverflow.com/questions/5534704
最满意答案
您可以使用Visual Studio Gallery中的 VSCommands Lite扩展(免费)
You can use VSCommands Lite extension (free) from Visual Studio Gallery
相关问答
更多-
右键(或控制)单击要更改的类型的文件: “获取信息” - >“打开方式:” - >(选择TextMate) - >“全部更改” Just right (or control) click a file of the type you want to change and: "Get Info" -> "Open with:" -> (Select TextMate) -> "Change All"
-
升格为默认编辑器(Sublime as default editor)[2022-01-06]
其实这不是我的答案,我刚刚google了它 : 打开regedit( Win + R ,键入“regedit”,选择OK)。 导航到HKEY_CLASSES_ROOT\Applications\sublime_text.exe\shell\open\command 验证路径是否准确,如果路径不正确。 退出regedit。 打开任务管理器通过Ctrl + Alt + Del (或Ctrl + Shift + Esc为更高版本的Windows),kill explorer.exe,去运行( Win + R )并 ... -
您可以使用Visual Studio Gallery中的 VSCommands Lite扩展(免费) You can use VSCommands Lite extension (free) from Visual Studio Gallery
-
如何使TinyMCE(富文本编辑器)成为默认的C5编辑器(How to make TinyMCE (Rich Text Editor) the default C5 editor)[2022-11-19]
我认为你使用的是HTML-Block而不是Content-Block 。 正如您在文档中看到的那样,有不同类型的块。 对于纯HTML和JavaScript, HTML-Block顾名思义。 Content-Block附加到Redactor Rich文本编辑器 。 I presume you're using a HTML-Block not a Content-Block. As you may see in the docs there are different type of blocks. The ... -
以下内容对您有用: git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe' -n -w" 您的别名起作用的原因是它被shell扩展: alias sublime="C:/Program\ Files/Sublime\ Text\ 2/sublime_text.exe" sublime -> C:/Program\ Files/Sublime\ Text\ 2/sublime_text.exe - ...
-
有2个环境变量可以使用: HOMEDRIVE和EXEPATH 。 HOMEDRIVE - 你跑过的驱动器(例如: C: HOMEDRIVE EXEPATH - git从哪里EXEPATH的完整路径(例如: "C:\\MyApps\\GitPortable" ) 所以你可以在你的gitconfig中使用这两个: editor = "$HOMEDRIVE/apps/Sublime-Text-3/sublime_text.exe" 您需要确保$HOMEDRIVE出现在gitconfig文件中(而不是由shell操 ...
-
这个问题看起来与我在我的机器上安装Visual Studio的方式有关 - 上周,我的电脑开始冻结并突然传出蓝屏 - 技术支持人员建议重新映像该机器,我不情愿同意因为那是我老化机器的万能药。 今天,支持工程师停下来,用我们现有的新硬盘替换掉了我们现有的硬盘 - 他们已经足够把我目前的应用推到新的镜像上。 交换完成后,支持人员进行了一些理智检查并竖起了大拇指,然后我再次安装了摩纳哥字体(来自与我原来相同的源代码),并应用了新字体,并打开了一个新项目 - 并且看,我现在看到我的代码在摩纳哥字体! 感觉很好,看到 ...
-
也许最好编写自己的自定义字段类型 ,从SPFieldMultiLineText继承它并覆盖使用TinyMCE的属性FieldRenderingControl和返回控件? Maybe it’s better to write your own Custom field type, inherited it from SPFieldMultiLineText and override property FieldRenderingControl and return control which use Tiny ...
-
SharePoint RTE只不过是一个内容丰富的ContentEditable div。 解决方案是将函数onpaste绑定到编辑器,然后直接操作html。 $(document).ready(function() { $(".ms-rtestate-write").bind('paste', function(e) { var text = $(this).html(); $(this).html(text.replace( /<\s*span.*?>/gi,"" ...
-
ghci命令都以:开头。 尝试:edit而不是。 https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html#ghci-cmd-:edit ghci command all begin with :. Try :edit instead. https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html#ghci-cmd-:edit