簡體   English   中英

使用Java中的掃描器類輸入

[英]Input using scanner class in java

我正在編寫一個程序以將給定的整數減少到最簡單的比率,但是在程序的子方法中通過Scanner類獲取輸入時發生錯誤,代碼如下:

package CodeMania;

import java.util.Scanner;

public class Question5 
{
public static void main(String args[])
{
    Scanner sc=new Scanner(System.in);
    int T=sc.nextInt();// number of test cases
    sc.close();
    if(T<1)
    {
        System.out.println("Out of range");
        System.exit(0);
    }
    for(int i=0;i<T;i++)
    {
    ratio();//line 19
    }

}
static void ratio()
{
    Scanner sc1=new Scanner(System.in);
    int N=sc1.nextInt();//line 26
    if((N>500)||(N<1))
    {
        System.out.println("Out of range");
        System.exit(0);
    }
    int a[]=new int[N];
    for(int i=0;i<N;i++)
    {
        a[i]=sc1.nextInt();
    }
    int result = a[0];
   for(int i = 1; i < a.length; i++)
        {
    result = gcd(result, a[i]);
    }
    for(int i=0;i<N;i++)
    {
        System.out.print((a[i]/result)+" ");
    }
    sc1.close();
}
static int gcd(int a, int b)
{
    while (b > 0)
    {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
}

錯誤是-

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at CodeMania.Question5.ratio(Question5.java:26)
    at CodeMania.Question5.main(Question5.java:19)

在這里,我在主要功能中使用了2個單獨的掃描儀對象sc,在比率功能中使用了sc1從控制台獲取輸入。 但是,如果我在類范圍內聲明了一個公共靜態類型Scanner對象,然后在整個程序中僅使用一個Scanner對象來獲取輸入,那么程序將按要求正常工作而不會出錯。

為什么會這樣...?

發生此錯誤的原因是,在掃描器上調用.close()也會關閉inputStream System.in ,但實例化新的掃描器不會重新打開它。

您需要在方法參數中傳遞單個Scanner,或者使其成為靜態全局變量。

由於您的main()ratio()方法正在使用掃描儀,因此它們會引發異常,因此,當發生異常時,程序的正常流程將被中斷,並且程序/應用程序將異常終止,因此不建議這樣做,因此應處理這些異常。 異常可能由於多種原因而發生,下面給出了發生異常的一些情況。

A user has entered invalid data.

A file that needs to be opened cannot be found.

A network connection has been lost in the middle of communications or the JVM has run out of memory.

您可以使用Try / Catch塊來處理這些異常,也可以通過在方法的定義后使用throws來處理它們,在這種情況下,這兩種方法將是這樣的:

使用try / catch

  public static void main()
  {
    try{
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();// number of test cases
        sc.close();
       }
    catch(NoSuchElementException e){
    System.out.print("Exception handled" + e);
    //rest of method
    }
    static void ratio(){
    try{
    Scanner sc1=new Scanner(System.in);
    int N=sc1.nextInt();}
    catch(NoSuchElementException e){
    System.out.print("Exception handled" + e);}
    //rest of method
    }

與“拋出”:

  public static void main()throws Exception{

       //rest of method
     }
   static void ratio()throws Exception
   { 
    //rest of method
   }

試試這個。 您可以將掃描儀作為參數傳遞

package stack.examples;

import java.util.Scanner;

public class Question5 {
public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();// number of test cases
    if (T < 1) {
        System.out.println("Out of range");
        System.exit(0);
    }
    for (int i = 0; i < T; i++) {
        ratio(sc);// line 19
    }
    sc.close();
}

static void ratio(Scanner sc1) {
    int N = sc1.nextInt();// line 26
    //Your Logic
}

static int gcd(int a, int b) {
    while (b > 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

}

import java.util.*;
public class Understanding_Scanner
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Please enter your name");
String name=sc.next();
System.out.println("Your name is:"+name);
}
}

現在解釋一下這一點,因此我們必須從Java Utility包中導入一個掃描器類,以便可以通過第一行中的代碼來實現,第二行創建一個類。 注意 (該類的名稱不必以大寫字母開頭)現在進入主要主題Scanner類,因此,我們必須在程序中使用第四行給出的代碼創建一個掃描器類...在此語句中,“ sc”是一個對象,用於存儲掃描器類的值因此,如果您想在掃描儀類中進行任何操作,都可以通過對象'sc'* NOTE(您可以將ur對象命名為任何東西,例如:poop,bla等)...那么我們就有了一個有趣的命令,它說System.in現在允許用戶在運行時通過鍵盤或任何此類輸入設備編寫任何語句。...字符串名稱= sc.next()此行可幫助我們編寫在運行期間要編寫的任何字符串時間,它將b存儲在名稱變量中

就是這樣,這是您的掃描器類。 希望它易於理解。

干杯!! 繼續編碼:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM