标签: java.util.scanner

Java - 如何使用Scanner获取最后一行?

        while( inStream.hasNextLine() )
        {
                ...

                lineList.add( inStream.nextLine() );
        }
        ...
Run Code Online (Sandbox Code Playgroud)

lineList是一个ArrayList.代码正在很好地读取所有内容,除非它不会抓住最后一行.文本文件中的最后两行如下所示:

"a sentence here..."
<a blank line here. the blank line is the last line>
Run Code Online (Sandbox Code Playgroud)

我假设它不会抓住它,因为hasNextLine()在此之后没有检测到另一行?

抓住最后一条线的方法是什么?我认为阅读直到它是EOF然后捕获异常可能会起作用,但似乎没有办法做到这一点.

编辑:更多信息

public void readLines()
{
    lineList = new ArrayList();

    try
    {
        inStream = new Scanner( new File( fileName ) );
    }
    catch( FileNotFoundException e )
    {
        System.out.println( "Error opening the file. Try again." );
    }


    if ( inStream != null )
    {
        while( inStream.hasNextLine() )
        {
            ++originalLines;

            lineList.add( inStream.nextLine() );
        }
        inStream.close();
    } …
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

2
推荐指数
1
解决办法
1万
查看次数

来自扫描仪的空白输入 - java

我想要做的是,如果用户点击回车键,程序应该抛出BadUserInputException.我的问题是,每当我按下回车键,它只是把我放在控制台的另一行,基本上什么都不做.

Scanner input = new Scanner(System.in);
    System.out.println("Enter Student ID:");

    String sID = null;
    if (input.hasNextInt()==false){
        System.out.println("Please re-check the student number inputted. Student number can only be digits.");
        throw new BadUserInputException("Student number can not contain non-digits.");
    }else if (input.next()==""){
        throw new BadUserInputException("Student number can not be empty");
    }
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

2
推荐指数
2
解决办法
4万
查看次数

停止扫描程序读取用户输入java?

我正在尝试编写这种方法,该方法不断向用户读取,直到输入"exit"字样.我尝试了休息和循环; 它不起作用.我正在尝试,但即使输入"退出"这个词,它也不会停止.任何想法如何解决这一问题?谢谢.

public void read(Scanner scanner){

while(3<4){
String in = scanner.nextLine();
Scanner scanner_2 = new Scanner(in);

String name = null;

if(scanner_2.hasNext()){
  //create and add the user to the user container class
  name = scanner_2.next();
  System.out.println(name);
}

if(name == exit)
//stop or break the while loop
}
}
Run Code Online (Sandbox Code Playgroud)

java input java.util.scanner

2
推荐指数
2
解决办法
1万
查看次数

字符串输出只返回一个单词

这个程序运行得很好,但出于某种原因,当我要求输入名称时,它只会保存响应中的第一个单词.例如,如果我在稍后使用它输入"Jon Snow"时输出它将只显示"Jon".

import java.util.Scanner;

public class help 
{
public static void main (String[] args)
{
    Scanner input = new Scanner(System.in); //object for user input

    //constant
    final double tax = 0.08; //Sales tax

    //variables
    int choice;                 //menu selection/case switch
    String name;                //customer's name   
    String address;             //customer's address
    String email;               //customer's email address
    String item;                //item purchased
    double itemsPurchased;      //number of items purchased
    double itemPrice;           //price of item purchased
    double total;               //total for purchase
    double grandTotal;          //total + tax
    double taxAmount;           //tax …
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

2
推荐指数
1
解决办法
4681
查看次数

Java扫描程序字符串的比较


我是Java编程的新手,我想在if语句中比较Java扫描程序字符串。我怎样才能做到这一点?

这就是我这样做的方式,但是没有给我我期望的输出:

Scanner oper = new Scanner(System.in); 
String op;   
System.out.println("Enter operator(+,-,*,/,%): ");
op = oper.nextLine();
if (op=="+")
{
   System.out.println("Answer is: "+fnum+snum);
}
Run Code Online (Sandbox Code Playgroud)

我还没有包括变量fnum和snum的代码。

java string comparison if-statement java.util.scanner

2
推荐指数
1
解决办法
1万
查看次数

Java - 如何将字符串扫描到数组?

我在将扫描信息放入Java中的数组时遇到了麻烦.

private void initializeFriends()
{
    //initialize the size of array:
    friends = new Friend[200];
    //initialize the first 5 objects(Friends):
    friends[0] = new Friend("Rodney Jessep", "rj2013", "Brisbane", "hi!");
    friends[1] = new Friend("Jaime Smiths", "abcd5432", "Sydney", "how's going");
    friends[2] = new Friend("William Arnold", "william1994", "Brisbane", "boom");
    friends[3] = new Friend("James Keating", "qwerty52", "Newcastle", "Hey");
    friends[4] = new Friend("Amy Richington", "IAmRichAmy", "Perth", "Yo");
}
Run Code Online (Sandbox Code Playgroud)

运行上述过程后,将运行一个调用的方法addFriends(),其中扫描程序将数据放入friend结构中.

将信息扫描到阵列中的最佳方法是什么?使用该Scanner in = new Scanner(System.in);功能是明智的,因为'Friend'中有很多不同的元素:( String name,String username,String city,String message)?

java arrays string java.util.scanner

2
推荐指数
1
解决办法
2万
查看次数

捕获异常时的不定式递归

这是我输入学号的代码:当用户以意想不到的格式输入数字时,我会要求他们通过递归重新输入.但它最终会产生一个不定式的递归.为什么?

private static int inputStudentNumber(){
    System.out.println("Enter the student number:");
    int studentNum;
    try {
        //Scanner in initialized before calling this method
        studentNum = in.nextInt();
        return studentNum;
    } catch(Exception e) {
        System.out.println("Invalid input, it can only be integer.");
        return inputStudentNumber();
    }
}
Run Code Online (Sandbox Code Playgroud)

java recursion exception java.util.scanner

2
推荐指数
1
解决办法
1458
查看次数

彩票号码发生器

我正在为课堂写一个程序.这是一个彩票游戏.这是给出的问题:

写一个java程序.程序应该有一个名为lotteryNumber()的方法,它应该接受两个整数,最大和最小数字,并且应该随机生成并返回这两个数字之间的数字(包括两者).

另外,提供一个名为checkWinner()的方法,该方法应该接受两个数组(一个用于抽奖号码,一个用于用户号码)并检查它们是否相同(相同的数字和序列).如果它们相同,则应返回true,否则返回false.请使用您自己的逻辑来使用相等运算符检查相等性.不允许使用像Arrays.equals()这样的预构建函数!

在main方法中,使用lotteryNumber()方法随机生成4个数字,介于0-10(包括两者)之间.然后要求用户从键盘输入0到10之间的数字.然后使用checkWinner()方法检查并显示用户是否为胜利者.

我的问题是使用l​​otteryNumber方法.我怎么应该只返回一个数字而不使用数组并仍然生成4个数字?请记住,我一般都是数组和java的新手,我需要将彩票号放在main方法的数组中,以便我可以将它们与用户输入进行比较.或者至少这就是我的问题

这就是我现在所拥有的.对不起,它是如此混乱,我一直在尝试不同的东西,希望弄明白

import java.util.Random;
import java.util.Scanner;

public class Assignment04 {
public static void main (String [] args){

    Scanner input = new Scanner (System.in); 

    Random r = new Random (); 

    int [] user= new int [4]; 
    //int [] rand= new int [4]; 

    int rand1= r.nextInt(10); 
    int rand2= r.nextInt(10); 
    int rand3 = lotteryNumber(rand1,rand2);

    /*for (int i=0; i<rand.length; i++){
        rand [i] = r.nextInt(11); 
    }

    for (int j=0; j<rand.length; j++){
        System.out.print(rand[j]+",");
    }*/

    System.out.println("");
    System.out.print("Enter number 1 between (1-10)= ");
    user[0] …
Run Code Online (Sandbox Code Playgroud)

java arrays random methods java.util.scanner

2
推荐指数
1
解决办法
4161
查看次数

使用扫描仪类在文本文件中书写

我遇到了很多关于如何使用ScannerJava 读取文本文件的程序.以下是使用Scanner以下方法在Java中读取文本文件的一些虚拟代码:

public static void main(String[] args) {

    File file = new File("10_Random");

    try {

        Scanner sc = new Scanner(file);

        while (sc.hasNextLine()) {
            int i = sc.nextInt();
            System.out.println(i);
        }
        sc.close();
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
 }
Run Code Online (Sandbox Code Playgroud)

但是,请任何人帮助我.txt使用Scannerjava 在文件中"编写"一些文本(即字符串或整数类型文本).我不知道如何编写该代码.

java file java.util.scanner

2
推荐指数
1
解决办法
3万
查看次数

我可以在公共课中申报扫描仪吗?

我是学习者(现在非常基础).当我找不到合法的答案时,我会问问题.

我可以在公共类中声明扫描程序,所以我不需要在我使用的每个函数中声明吗?

public class  name
{
   //can i declare scanner here? and how?
   public static int xxx2(int kk)
   {
     Scanner kb=new Scanner( System.in);
     //code
     return kk;
    }

    public static int xxx1(int kk)
    {
        Scanner kb=new Scanner(System.in);
        //code
    }
}
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

2
推荐指数
1
解决办法
58
查看次数