这是给我的一个朋友,他在学校里遇到了Java问题.
我知道一些编程,但不是Java.
Scanner kbReader = new Scanner(System.in);
System.out.print("Name of item: ");
String name = kbReader.next();
System.out.println("Original price of item: $");
double price = kbReader.nextDouble();
Run Code Online (Sandbox Code Playgroud)
输出:
Name of item: Coat
Original price of item: $
10
Run Code Online (Sandbox Code Playgroud)
为什么下一行的"物品原价:$"输入?我猜这是因为我去String了double,但想不出有不同的方法呢?
我正在尝试读取一个文本文件,然后使用Java中的nextInt()函数在循环中打印出整数.我的文本文件的格式如下:
a 2000 2
b 3000 1
c 4000 5
d 5000 6
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public static void main(String[] args) throws FileNotFoundException {
String fileSpecified = args[0] + ".txt";
FileReader fr = new FileReader(fileSpecified);
BufferedReader br = new BufferedReader (fr);
Scanner in = new Scanner (br);
while (in.hasNextLine()) {
System.out.println ("next int = " + in.nextInt());
}
}
Run Code Online (Sandbox Code Playgroud)
我总是得到的错误是:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
每次在任何程序中使用nextInt()时都会出现此错误.
Scanner Class 找不到我使用 NetBeansIDE 的文件,test.txt 在文件夹路径中:D:\netbeans projectworks\ReadFile\src\readfile\test.txt
在同一个文件夹中存在 readfile.java。代码如下。它生成未找到的文件。
package readfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) throws IOException , FileNotFoundException
{
Scanner scanner = new Scanner(new File("test.txt"));
while (scanner.hasNextLine())
System.out.println(scanner.nextLine());
}
}
Run Code Online (Sandbox Code Playgroud)
输出:-
run:
Exception in thread "main" java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.util.Scanner.<init>(Scanner.java:636)
at readfile.ReadFile.main(ReadFile.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Run Code Online (Sandbox Code Playgroud) 好的,所以我正在尝试转换一些东西。
所以我已经将我的 Scanner 转换为一个字符串,现在我想要做的是,获取他们输入的值并将其用作其他几个 if 语句的整数。它行不通!
到目前为止,这是我的代码:
import java.util.Scanner;
public class apples {
public static void main(String args[]) {
Scanner fname = new Scanner(System.in);
Scanner sname = new Scanner(System.in);
Scanner number = new Scanner(System.in);
tuna weight = new tuna();
System.out.println("Enter Your First Name: ");
String fname1 = fname.nextLine();
String fnames = fname1;
System.out.println("Enter Your Last Name: ");
String sname1 = sname.nextLine();
String snames = sname1;
System.out.println("Enter Your Weight (Lbs.) : ");
String num = number.nextLine();
String num1 = num;
System.out.println("Okay …Run Code Online (Sandbox Code Playgroud) 我一遍又一遍地提示用户输入,直到用户输入exit,在这种情况下数据对象将被设置为null并且它将离开循环.在大多数情况下,这段代码可以正常工作,但有时候我会得到一个NoSuchElementException : reader = new Scanner(System.in);. 这往往发生在几个循环后,而不是第一次通过.如果我在while循环的范围内声明读者变量,那么它将在第二个循环中失败.
int level = 1;
Scanner reader;
String selection = null;
while (true) {
if (data.done) {
level--;
}
data.done = false;
System.out.println(getSubmenu(level, data));
reader = new Scanner(System.in);
if (level <6) {
selection = reader.nextLine();
} else {
level = 4;
}
if (validSelection(selection)) {
level = getLevel(level, selection);
data = getData(level, data, selection);
} else {
System.out.println("Invalid entry");
}
if (data == null) {
System.out.println("Level "+ level + "selection " …Run Code Online (Sandbox Code Playgroud) 我有一个逗号分隔的文本文件,其中包含以下格式的信息:
firstname,lastname,meal1,meal2,meal3,meal4 ....每个新学生换一行。
我有以下学生对象。
public class Student {
private String first = null;
private String last = null;
public Student (String first, String last){
this.first = first;
this.last = last;
}
Run Code Online (Sandbox Code Playgroud)
我需要一个从另一个类中使用的方法来填充学生对象数组。
我不确定如何使用扫描仪执行此操作,因为我只需要每行中的前两个,任何帮助我指出正确方向的帮助将非常感谢。
~谢谢!
我希望这个程序要求用户输入,并创建一个名称等于用户输入的类实例。然后,createMember 类将创建一个文本文件,该文件将存储用户的所有数据。我该怎么做?
下面是主要方法:
public static void main(String[] args) {
String input = keyboard.nextLine();
input = new createMember(); // Error. can't do that for some reason?
}
}
Run Code Online (Sandbox Code Playgroud)
这是 createMember 类
public class createMember {
public void setMembership() {
Scanner keyboard = new Scanner(System.in);
out.println("Username: ");
String input = keyboard.nextLine();
try {
//Creates a text file with the same name as the username where data is stored.
Formatter x = new Formatter(input);
} catch (Exception e) {
out.println("Could not create username"); …Run Code Online (Sandbox Code Playgroud) 用户需要输入一定数量的整数.相反,他们一次输入一个整数,我想让它们可以在一行上输入多个整数,然后我希望这些整数在数组中转换.例如,如果用户输入:56 83 12 99那么我想要创建一个数组{56, 83, 12, 99}
在Python或Ruby等其他语言中,我会使用一种.split(" ")方法来实现这一目标.据我所知,Java中没有这样的东西存在.有关如何接受用户输入并基于此创建阵列的任何建议都在一条线上吗?
我有一个基本上从文本文件中读取的代码.输出时有一个前导加号,因为答案是循环打印的.如何摆脱那个奇异的领先加号?我所能想到的就是将整个过程转换为字符串,然后取出前三个索引,但这太复杂了.有没有一种简单的方法来重新排列逻辑并做到这一点?
我的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package importtextfiles;
/**
*
* @author Hana
*/
import java.io.*;
import java.util.*;
public class InputNumData {
public static void main(String args[]) throws IOException
{
Scanner sf = new Scanner(new File("C:\\Users\\Hana\\SkyDrive\\CompSci\\Programming\\importTextFiles\\meow.txt"));
int maxIndx = -1; //so the first index is 0
String text[] = new String[1000];
while(sf.hasNext()){ …Run Code Online (Sandbox Code Playgroud) I am trying to refactor my code, and adding methods where possible. When I read a file from a method and return the result of the computation, the code goes into extreme memory consumption, infinite loop.
My modification looks like this:
import java.util.Scanner;
public class NumberOfLines {
public static int compute () {
// read this file
String theFile = "numbers.txt";
Scanner fileRead = null;
if (NumberOfLines.class.getResourceAsStream(theFile) != null) {
fileRead = new Scanner(NumberOfLines.class.getResourceAsStream(theFile));
}
else {
System.out.print("The file " …Run Code Online (Sandbox Code Playgroud)