Java スキャナ クラス
Java では、Scanner は、文字列および int、double などのさまざまなプリミティブ型の入力を取得するために使用されるクラスです。スキャナー クラスは、パッケージ java にあります。これは Object クラスを拡張し、Closeable インターフェイスと Iterator インターフェイスを実装します。入力は空白区切り文字を使用してクラスに分割されます。 Java の Scanner クラスの宣言、構文、動作、その他のいくつかの側面については、次のセクションで説明します。
無料ソフトウェア開発コースを始めましょう
Web 開発、プログラミング言語、ソフトウェア テスト、その他
Java スキャナー クラス宣言
Java Scanner クラスは以下のように宣言されています。
public final class Scanner extends Object implements Iterator<String>
構文:
Scanner クラスは、Java プログラムの以下の構文で使用できます。
Scanner sc = new Scanner(System.in);
ここで、sc はスキャナ オブジェクトであり、System.in は Java に入力がこれであることを伝えます。
Java スキャナ クラスはどのように機能しますか?
次に、Scanner クラスがどのように機能するかを見てみましょう。
- クラス java.util.Scanner またはパッケージ全体 java.util. をインポートします。
import java.util.*; import java.util.Scanner;
- 構文に示すようにスキャナ オブジェクトを作成します。
Scanner s = new Scanner(System.in);
- 型 int、char、または string を宣言します。
int n = sc.nextInt();
- 要件に基づいて入力に対して操作を実行します。
コンストラクター
Java のスキャナー クラスにはさまざまなコンストラクターがあります。それらは次のとおりです:
- Scanner(File src): 言及されたファイルから値を生成する新しいスキャナーが構築されます。
- Scanner(File src, String charsetName): 前述のファイルからスキャンされた値を生成する新しいスキャナーが構築されます。
- Scanner(InputStream src): 前述の入力ストリームから値を生成する新しいスキャナーが構築されます。
- Scanner(InputStream src, String charsetName): 前述の入力ストリームから値を生成する新しいスキャナーが構築されます。
- Scanner(Path src): 前述のファイルから値を生成する新しいスキャナーが構築されます。
- Scanner(Path src, String charsetName): 前述のファイルから値を生成する新しいスキャナーが構築されます。
- Scanner(Readable src): 言及されたソースから値を生成する新しいスキャナーが構築されます。
- Scanner(ReadableByteChannel src): 言及されたチャネルから値を生成する新しいスキャナーが構築されます。
- Scanner(ReadableByteChannel src, String charsetName): 言及されたチャネルから値を生成する新しいスキャナーが構築されます。
- Scanner(String src): 指定された文字列から値を生成する新しいスキャナーが構築されます。
Java スキャナ クラスのメソッド
次に、さまざまな機能を実行するメソッドを示します。
- close(): Scanner gets closed on calling this method.
- findInLine(Pattern p): Next occurrence of the mentioned pattern p will be attempted to find out, ignoring the delimiters.
- findInLine(String p): The next occurrence of the pattern that is made from the string will be attempted to find out, ignoring the delimiters.
- delimiter(): Pattern that is currently used by the scanner to match delimiters will be returned.
- findInLine(String p): The next occurrence of the pattern that is made from the string will be attempted to find out, ignoring the delimiters.
- findWithinHorizon(Pattern p, int horizon): Tries to identify the mentioned pattern’s next occurrence.
- hasNext(): If the scanner has another token in the input, true will be returned.
- findWithinHorizon(String p, int horizon): Tries to identify the next occurrence of the mentioned pattern made from the string p, ignoring delimiters.
- hasNext(Pattern p): If the next token matches the mentioned pattern p, true will be returned.
- hasNext(String p): If the next token matches the mentioned pattern p made from the string p, true will be returned.
- next(Pattern p): Next token will be returned, which matches the mentioned pattern p.
- next(String p): Next token will be returned, which matches the mentioned pattern p made from the string p.
- nextBigDecimal(): Input’s next token will be scanned as a BigDecimal.
- nextBigInteger(): Input’s next token will be scanned as a BigInteger.
- nextBigInteger(int rad): Input’s next token will be scanned as a BigInteger.
- nextBoolean(): Input’s next token will be scanned as a Boolean, and it will be returned.
- nextByte(): Input’s next token will be scanned as a byte.
- nextByte(int rad): Input’s next token will be scanned as a byte.
- nextDouble(): Input’s next token will be scanned as a double.
- nextFloat(): Input’s next token will be scanned as a float.
- nextInt(): Input’s next token will be scanned as an integer.
- nextInt(int rad): Input’s next token will be scanned as an integer.
- nextLong(int rad): Input’s next token will be scanned as a long.
- nextLong(): Input’s next token will be scanned as a long.
- nextShort(int rad): Input’s next token will be scanned as a short.
- nextShort(): Input’s next token will be scanned as a short.
- radix(): The default radix of the scanner will be returned.
- useDelimiter(Pattern p): Delimiting pattern of the scanner will be set to the mentioned pattern.
- useDelimiter(String p): Delimiting pattern of the scanner will be set to the mentioned pattern made from the string p.
- useRadix(int rad): The scanner’s default radix will be set to the mentioned radix.
- useLocale(Locale loc): The locale of the scanner will be set to the mentioned locale.
Examples of Java Scanner Class
Now, let us see some sample programs of the Scanner class in Java.
Example #1
Code:
import java.util.Scanner; class ScannerExample { public static void main(String[] args) { //create object of scanner Scanner sc = new Scanner(System.in); System.out.println("Enter the username"); String obj = sc.nextLine(); System.out.println("The name you entered is: " + obj); } }
Output:
On executing the program, the user will be asked to enter a name. As the input is a string, the nextLine() method will be used. Once it is entered, a line will be printed displaying the name user given as input.
Example #2
Code:
import java.util.Scanner; class ScannerExample { public static void main(String[] args) { //create object of scanner Scanner sc = new Scanner(System.in); System.out.println("Enter age"); int obj = sc.nextInt(); System.out.println("The age you entered is: " + obj); } }
Output:
On executing the program, the user will be asked to enter the age. As the input is a number, the nextInt() method will be used. Once it is entered, a line will be printed displaying the age user given as input.
以上がJava スキャナ クラスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。

PHPとPythonにはそれぞれ独自の利点があり、さまざまなシナリオに適しています。 1.PHPはWeb開発に適しており、組み込みのWebサーバーとRich Functionライブラリを提供します。 2。Pythonは、簡潔な構文と強力な標準ライブラリを備えたデータサイエンスと機械学習に適しています。選択するときは、プロジェクトの要件に基づいて決定する必要があります。

phphassiblasifly-impactedwebdevevermentandsbeyondit.1)itpowersmajorplatformslikewordpratsandexcelsindatabase interactions.2)php'sadaptableability allowsitale forlargeapplicationsusingframeworkslikelavel.3)

PHPが多くのWebサイトよりも優先テクノロジースタックである理由には、その使いやすさ、強力なコミュニティサポート、広範な使用が含まれます。 1)初心者に適した学習と使用が簡単です。 2)巨大な開発者コミュニティと豊富なリソースを持っています。 3)WordPress、Drupal、その他のプラットフォームで広く使用されています。 4)Webサーバーとしっかりと統合して、開発の展開を簡素化します。

PHPはWeb開発およびコンテンツ管理システムに適しており、Pythonはデータサイエンス、機械学習、自動化スクリプトに適しています。 1.PHPは、高速でスケーラブルなWebサイトとアプリケーションの構築においてうまく機能し、WordPressなどのCMSで一般的に使用されます。 2。Pythonは、NumpyやTensorflowなどの豊富なライブラリを使用して、データサイエンスと機械学習の分野で驚くほどパフォーマンスを発揮しています。
