简体   繁体   English

如何在 java 中使用 nextLine 进行扫描仪

[英]How to use nextLine for scanner in java

I used correct data type but I cant find my errors.我使用了正确的数据类型,但我找不到我的错误。 Below is my code;以下是我的代码;

Scanner input = new Scanner (System.in);
    
    //Read 
    System.out.printf ("Enter the station: ");
    String s  = input.nextLine();
    
    System.out.printf ("Enter quantity in liter: ");
    double q  = input.nextDouble();

    
    System.out.printf ("Enter type of petrol: ");
    String t  = input.nextLine();
    
    System.out.printf ("Enter price of petrol: ");
    double p  = input.nextDouble();
    
    System.out.printf ("Enter discount: ");
    int d  = input.nextInt();

when I run my program, It does not go to next line to input values.当我运行我的程序时,它不会 go 到下一行输入值。 For petrol type, I want to input something like "Super 99" so I need to use input.nextLine() but its not working.对于汽油类型,我想输入“Super 99”之类的内容,所以我需要使用 input.nextLine() 但它不起作用。

在此处输入图像描述

Try the below code.试试下面的代码。

Use another input.nextLine() right after the double q = input.nextDouble();在 double q = input.nextDouble(); 之后使用另一个 input.nextLine() line.线。

public static void main(String[] args) {
        Scanner input = new Scanner (System.in);

        //Read
        System.out.printf ("Enter the station: ");
        String s  = input.nextLine();

        System.out.printf ("Enter quantity in liter: ");
        double q  = input.nextDouble();
        input.nextLine();

        System.out.printf ("Enter type of petrol: ");
        String t  = input.nextLine();

        System.out.printf ("Enter price of petrol: ");
        double p  = input.nextDouble();

        System.out.printf ("Enter discount: ");
        int d  = input.nextInt();
    }

/* *****This will work for you dear */ /* *****这对你有用,亲爱的 */

import java.util.*;导入 java.util.*; class Tester { class 测试仪 {

    public static void main(String[] args)
  {
    Scanner input = new Scanner (System.in);
    System.out.printf ("Enter the station: ");
    String s  = input.nextLine();
    
    System.out.printf ("Enter quantity in liter: ");
    double q  = input.nextDouble();

    input.nextLine();
    System.out.printf ("Enter type of petrol: ");
    String t  = input.nextLine();
    
    System.out.printf ("Enter price of petrol: ");
    double p  = input.nextDouble();
    
    System.out.printf ("Enter discount: ");
    int d  = input.nextInt();
   }

} }

You just need to use:你只需要使用:

System.out.println("Enter the type of petrol");

It will work like this:它将像这样工作:

Enter the type of petrol
Super99     //its what you will enter as per your choice here the cursor will automatically take you to the next line

To print a line including a newline:要打印包含换行符的行:

System.out.println("Enter price of petrol: ");
// -OR-
System.out.printf("Enter price of petrol: \n");

To obtain a single token (a single word, number, integer, etc):要获取单个令牌(单个单词、数字、integer 等):

scanner.nextInt(); // integer
scanner.next(); // word

To obtain an entire line:获取整行:

scanner.useDelimiter("\r?\n");
// now the scanner is in 'entire line' mode
scanner.nextInt(); // still works
scanner.next(); // gets one line's worth

To go back to 'one token per whitespace' mode, where entering eg "abc" (enter) will cause 3 .next() calls of data to be available:到 go 回到“每个空格一个令牌”模式,输入例如“abc”(回车)将导致 3 .next()数据调用可用:

scanner.reset();
// -OR-
scanner.useDelimiter("\s+");

TIP: Don't mix nextLine() and any other next method.提示:不要混合nextLine()和任何其他next方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM