我也主要使用Scanner,也想尝试使用缓冲读卡器:到目前为止我所拥有的
import java.util.*;
import java.io.*;
public class IceCreamCone
{
// variables
String flavour;
int numScoops;
Scanner flavourIceCream = new Scanner(System.in);
// constructor
public IceCreamCone()
{
}
// methods
public String getFlavour() throws IOexception
{
try{
BufferedReader keyboardInput;
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" please enter your flavour ice cream");
flavour = keyboardInput.readLine();
return keyboardInput.readLine();
}
catch (IOexception e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我相当肯定会得到一个你可以说的int
Integer.parseInt(keyboardInput.readLine());
Run Code Online (Sandbox Code Playgroud)
但如果我想要一个字符串怎么办?
我正在尝试为大学gpa制作一个计算器.如果陈述只说每个字母等级是什么,我就把所有这些都剪掉了.我修复了我的第一个程序,让任何人再次看到这个.该程序现在可以工作,但无论我在gpa中键入的字母,它返回的是2.0.如果有人看到任何错误,将再次非常感谢.谢谢
import java.util.Scanner;
public class universityGPA {
public static void main(String args[]){
int classes = 4;
int units[] = {3, 2, 4, 4};
double[] grade = new double[4];
double[] value= new double[4];
int counter = 0;
double total = 0;
double gpa;
String letter;
while(classes > counter){
Scanner gradeObject = new Scanner(System.in);
letter = gradeObject.next();
if(letter.equalsIgnoreCase("A+") || letter.equalsIgnoreCase("A")){
grade[counter] = 4;
}
if(letter.equalsIgnoreCase("F")){
grade[counter] = 0;
}
value[counter] = grade[counter] * units[counter];
counter++;
}
for(int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我正在使用Scanner逐行读取文件.所以我的问题是,当我们继续前进到下一行时,我如何能够并排存储前一行.
while (scanner.hasNextLine()) {
line = scanner.nextLine();//this will get the current line
if (line.contains("path=/select")){
// So whenever I enter in this if loop or this loop is true, I also want to
have the previous line in any String variable. How can we achieve this?
}
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激.
我从来没有对正则表达式很好,我似乎无法得到这个......
我试图匹配这些行的语句(这些是我正在阅读的文本文件中的两行)
Lname Fname 12.35 1
Jones Bananaman 7.1 3
Run Code Online (Sandbox Code Playgroud)
目前我正在使用这个语句
reader.hasNext("\\w+ \\w+ \\d*\\.\\d{1,2} [0-5]")
Run Code Online (Sandbox Code Playgroud)
但它没有进入while语句.当我删除while时,程序读取文本文件就好了.代码段是这样的:
private void initializeFileData(){
try {
Scanner reader = new Scanner(openedPath);
while(reader.hasNext("\\w+ \\w+ \\d*\\.\\d{1,2} [0-5]")){
employeeInfo.add(new EmployeeFile(reader.next(), reader.next(), reader.nextDouble(), reader.nextInt(), new employeeRemove()));
}
for(EmployeeFile element: employeeInfo){
output.add(element);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 这里的基本问题..我将首先要求你不要回复任何代码,因为这可能只会让我更加困惑(编程菜单).我正在寻找关于如何解决这个问题的明确解释.
我有一个扫描仪,可以读取用户的输入.提示用户输入1到150之间的int值(仅限整数).我获得如下值:
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
Run Code Online (Sandbox Code Playgroud)
继续我的程序,一切正常.
不幸的是,代码并不完全是防弹的,因为任何非整数的输入都可以破坏它(字母,符号等).
如何使代码更加健壮,哪里可以验证只输入了一个int?
这些是我希望的结果:
让我们说输入是:
23 -> valid
fx -> display an error message, ask the user for input again (a while loop would do..)
7w -> error, again
3.7 -> error
$$ -> error
etc
Run Code Online (Sandbox Code Playgroud) 对于我的编程类,我被告知要创建一个使用递归的程序.我很困惑,去看我已经在课堂上的朋友,他向我展示了这个节目.我认为递归必须使用像r1(x-1)等的东西.它实际上是递归的吗?如果不是,你如何使它递归?
import java.util.*;
import java.io.*;
class ReverseFile
{
private static Scanner infile;
public static void main(String[] args) throws IOException
{
infile= new Scanner(new File("hw_1.txt"));
r1();
}
public static void r1()
{
String s;
if (infile.hasNextLine())
{
s = infile.nextLine();
r1();
System.out.println(s);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图将一段文本分成基于标点符号的单独句子,即[.?!]但是,即使我已经指定了特定的模式,扫描程序也会在每个新行的末尾分割行.我该如何解决这个问题?谢谢!
this is a text file. yes the
deliminator works
no it does not. why not?
Scanner scanner = new Scanner(fileInputStream);
scanner.useDelimiter("[.?!]");
while (scanner.hasNext()) {
line = scanner.next();
System.out.println(line);
}
Run Code Online (Sandbox Code Playgroud) 当使用Scanner对象从文本文件中读取时,我希望它跳过文件的第一行.我怎么做到这一点?
在使用扫描仪时,如下所示:
Scanner s = new Scanner(System.in);
String response = s.next();
Boolean approved = (response.contains("Y") || response.contains("y")) ? true : false;
if (approved){
Do Stuff
}
s.close();
Run Code Online (Sandbox Code Playgroud)
我没有这样的Element异常异常:
线程“主”中的异常java.util.Scanner.throwFor(java.util.Scanner.next(未知源)处的java.util.NoSuchElementException ****
我多次致电(Scanner),第二次致电时发生运行时错误。这是由于关闭扫描仪,然后可能再次使用。我的问题是,每次使用Scanner时都会创建一个新实例,为什么会出现RunTime错误?
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner a = new Scanner(System.in);
System.out.println("Enter a number: ");
try{
float b = a.nextFloat();
System.out.println("You wrote " + b);
}catch(Exception n){
System.out.println("That wasn't a number, yo!");
}
a.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我想扫描用户输入的号码,从而确定它是否是数字.当我输入诸如3.1415之类的十进制数时会出现问题,因为它将其检测为非数值.