我正在尝试读取包含多个单词的字符串,即.洛杉矶或纽约市.对于"离开"和"到达"使用scanner.next()只读取第一个如果有两个单词并在变量之间拆分它们.nextLine()也没有太大的运气.这是我的代码:
System.out.print("\nEnter flight number: ");
int flightNumber = Integer.valueOf(scanner.nextLine());
System.out.print("\nEnter departing city: ");
String departingCity = scanner.nextLine();
System.out.print("\nEnter arrival city: ");
String arrivalCity = scanner.nextLine();
Run Code Online (Sandbox Code Playgroud)
我知道这很简单,但我还没弄清楚.
这是输入/输出w /上面的代码:
输入航班号:29
输入离境城市:(立即跳到下一行)
进入抵达城市:
----我真正想要的是什么----
输入航班号:29
进入离开城市:洛杉矶(能够输入多个单词而不跳过下一个输入)
进入抵达城市:堪萨斯城
我需要读取文本文件的前n行作为行(每行可能包含或不包含空格).文本文件的其余部分包含未知数量N的以空格分隔的标记(分隔符是空格,制表符和换行符的混合,所有这些都被视为与分隔符完全相同).
我知道如何使用BufferedReader读取行.我知道如何使用Scanner读取令牌.但是,如何以上述方式将这两种不同的阅读模式组合成单个文本文件?
我正在努力实现一个基本的词法分析器.我现在仍然坚持解析文件.
public ArrayList<Token> ParseFile () {
int lineIndex = 0;
Scanner scanner = new Scanner(this.fileName);
while (scanner.hasNextLine()) {
lineIndex++;
String line = scanner.nextLine();
if (line.equals(""))
continue;
String[] split = line.split("\\s");
for (String s : split) {
if (s.equals("") || s.equals("\\s*") || s.equals("\t"))
continue;
Token token = new Token(s, lineIndex);
parsedFile.add(token);
}
}
scanner.close();
return this.parsedFile;
}
Run Code Online (Sandbox Code Playgroud)
这是我的名字叫"p ++.ppp"
#include<iostream>
using namespace std ;
int a ;
int b ;
int main ( ) {
cin >> a ;
cin >> …Run Code Online (Sandbox Code Playgroud) 将来我会有这样的情况:异常可能通过的值赋给抛出的情况下input.nextLine(),以一个String与变量Scanner?
就像你说的那样
String foo = input.nextInt();
Run Code Online (Sandbox Code Playgroud)
你会得到一个InputMismatchException.所以我想知道的是,是否有任何可能的方法来获取异常:
String foo = input.nextLine();
Run Code Online (Sandbox Code Playgroud)
这可能是一个愚蠢的问题,但我需要绝对肯定.
提前致谢!
java user-input console-application type-safety java.util.scanner
我有这个方法,并从控制台(键盘)读取一个int数字序列的想法,并在ArrayList中添加所有这些,我使用类Scanner读取数字,但在for循环不起作用,它抛出" java.util.NoSuchElementException".
public static int mayorNumberSecuence(){
System.out.println("Give me a size ");
Scanner sn = new Scanner(System.in);
int n = sn.nextInt();
sn.close();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i=0; i<= n; ++i){
System.out.println("Give me a number ");
Scanner sn2 = new Scanner(System.in);
int in = sn2.nextInt();
list.add(in);
sn2.close();
}
Run Code Online (Sandbox Code Playgroud) 当我运行以下代码时,它显示错误,扫描仪无法解析键入.我检查了jre已安装且版本为1.7我还需要检查什么?请帮忙.
public class student {
String name;
int rollno;
public void get(String nm, int rno)
{ name=nm;
rollno=rno;
}
public void display()
{ System.out.println("Name of student is :" +name);
System.out.println("Roll no of student is :" +rollno);
}
public static void main(String args[])
{
int i ;
int r1;
String n1;
student obj[]= new student[10];
Scanner sc=new Scanner(System.in);
for(i=0;i<10;i++)
{
obj[i]= new student();
}
for(i=0;i<10; i++)
{ System.out.println("Enter name:");
n1=sc.next();
sc.nextLine();
System.out.println("Enter roll no :");
r1=sc.nextInt();
obj[i].get(n1,r1) ;
obj[i].display() ;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用java创建一个文本分析器,我正在尝试的是搜索文本文档,并计算有多少大写字母.
public static void main(String[] args) {
Scanner sc = new Scanner(CapitalCount.class.getResourceAsStream("test.txt"));
String s = sc.nextLine();
int upperCaseCount = 0;
int linecount = 0;
while (sc.hasNextLine()) {
sc.nextLine();
linecount++;
}
for (int i = 0; i < s.length(); i++) {
for (char c = 'A'; c <= 'Z'; c++) {
if (s.charAt(i) == c) {
upperCaseCount++;
}
}
}
System.out.println(upperCaseCount + "");
}
}
Run Code Online (Sandbox Code Playgroud)
我认为我必须要考虑有多少行,这就是为什么我在顶部添加了行数,虽然我不确定如何使用仅适用于第一行.
在程序中使用多个扫描仪对象的优点或用途是什么?
Scanner sc = new Scanner(System.in);
Run Code Online (Sandbox Code Playgroud)
而不是像
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
Run Code Online (Sandbox Code Playgroud)
在同一个程序中.
这是我第一次编码,所以我很抱歉,如果我让你面对面.
我想使用Scanner类来读取用户输入.但是,我收到一个错误.当我遇到这个问题时,我已停止编码,因此代码远未完成,但这就是我所拥有的:
package trigger;
import java.util.Scanner;
public class Trigger {
public static void main(String[] args) {
System.out.println("Please input known values");
Scanner input = new Scanner(System.in);
System.out.println("Angle A");
String Ain = input.next();
System.out.println("Angle B");
String Bin = input.next();
System.out.println("Angle C");
String Cin = input.next();
System.out.println("Side A");
String ain = input.next();
System.out.println("Side B");
String bin = input.next();
System.out.println("Side C");
String cin = input.next();
}
}
Run Code Online (Sandbox Code Playgroud)
这会返回如下错误:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class Scanner
location: class …Run Code Online (Sandbox Code Playgroud) 我们需要为netbeans的本地电影院完成一个简单的票务系统,而我为两个问题感到困惑。
问题1作为输出的一部分,一旦您选择了票证类型+数量,就需要有一个输出“您正在购买Y数量的X张票证”
问题2是长者票需要花费$ 32.50,我似乎无法找到一种允许使用小数进行计算的解决方法。我进行了调试,似乎将数字更改为整数,然后该整数将无法正确计算。救命!
package ticketingsystem;
import java.io.*;
public class ticketingsystem
{
public static void main(String []args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String order,again;
int quantity,price1=0,price2=0, price3=0,loop1=0,quantity1,quantity2=0;
System.out.println(" ");
System.out.println("Welcome to the cinemas!");
System.out.println(" ");
System.out.println("MAIN MENU");
System.out.println(" ");
System.out.println("The cinema has the following options");
System.out.println(" ");
System.out.println("1 = Child (4-5 yrs)");
System.out.println("2 = Adult (18+ yrs)");
System.out.println("3 = Senior (60+ yrs)");
do{
System.out.println(" ");
System.out.print("Enter your option: ");
order=br.readLine();
if (order.equalsIgnoreCase("1")) {
price1=18;
} else if (order.equalsIgnoreCase("2")) …Run Code Online (Sandbox Code Playgroud) java ×10
console ×1
eclipse ×1
for-loop ×1
netbeans ×1
netbeans-8.1 ×1
parsing ×1
type-safety ×1
user-input ×1