[英]Java - How to store values of a text file into an array using Scanner?
[英]How to store input values in an array by using Scanner in Java
嗨,我是Java的新手,我正在嘗試使用Scanner類。 我試圖找出一個小問題,我要輸入兩個輸入,例如:4 5 6和8 90。我想在一個數組中存儲4,5,6,在另一個數組中存儲8,9,0,然后打印這些數組。 但是我做不到。 我寫了以下代碼:
public class scanner {
public static void main(String[] args) {
int[] array = new int[3];
int[] array2 = new int[3];
Scanner scan = new Scanner(System.in);
int i = 0;
while(scan.hasNextInt()){
array[i] = scan.nextInt();
i++;
if(i == 3){
break;
}
}
i = 0;
while(scan.hasNextInt()){
array2[i] = scan.nextInt();
i++;
if(i == 3){
break;
}
}
for(int j = 0; j < array.length; j++){
System.out.println(array[j]);
}
for(int j = 0; j < array2.length; j++){
System.out.println(array2[j]);
}
}
}
但這不會將輸入4 5 6放在一行中。 我想在一行中輸入4 5 6,以便所有三個數字都存儲在數組中。 有人可以幫幫我嗎。 我認為我應該使用定界符刪除空白,但是我不確定如何去做。
您可以嘗試這樣,而不是2個while
循環來填充數組。
在這里,掃描器逐行讀取,並且每行在空間上拆分(如您在問題中所提到的),然后將每個拆分后的元素轉換為整數,然后填充數組。
String line1 = scan.nextLine(); // Read 1st line
String[] numbers1 = line1.split(" "); // Split based on space
for(int i=0;i<numbers1.length;i++){
array[i] = Integer.parseInt(numbers1[i]);
}
String line2 = scan.nextLine(); // Read 2nd line
String[] numbers2 = line2.split(" "); // Split based on space
for(int i=0;i<numbers2.length;i++){
array2[i] = Integer.parseInt(numbers2[i]);
}
樣本I / O:-
Input:
1 2 3
4 5 6
Output:
1
2
3
4
5
6
為此,您必須將String作為一個孔4 5 6
並使用split(“”)獲得一個數組:
String val="4 5 6";
String [] arr = val.split(" ");
然后像現在一樣繼續進行循環
這是你想要的?
public class InputScanner {
public static void main(String[] args) {
int[] array = new int[3];
int[] array2 = new int[3];
Scanner scan = new Scanner(System.in);
int i = 0;
while (scan.hasNextInt()) {
array[i] = scan.nextInt();
i++;
if (i == 3) {
break;
}
}
i = 0;
while (scan.hasNextInt()) {
array2[i] = scan.nextInt();
i++;
if (i == 3) {
break;
}
}
for (int j = 0; j < array.length; j++) {
System.out.print("" + array[j] + (j<(array.length-1)?" ":"\n"));
}
for (int j = 0; j < array2.length; j++) {
System.out.print("" + array2[j] + (j<(array2.length-1)?" ":"\n"));
}
}
}
您的代碼還可以。 根據javadoc,Scanner的默認定界符為空格。
輸入由定界符模式分為標記,默認情況下為空白
可能使您感到困惑的是,打印內容將每個整數放在了新行中。 要解決此問題,您可以簡單地編寫以下代碼:
for(int j = 0; j < array.length; j++){
System.out.print(array[j] + " ");
}
System.out.println();
for(int j = 0; j < array2.length; j++){
System.out.print(array2[j] + " ");
}
int[] array = new int[3];
int[] array2 = new int[3];
Scanner scan = new Scanner(System.in);
for(int i = 0 ; i < array.length ; i++){
array[i] = scan.nextInt();
}
for(int i = 0 ; i < array2.length ; i++){
array2[i] = scan.nextInt();
}
for(int j = 0; j < array.length; j++){
System.out.println(array[j]);
}
for(int j = 0; j < array2.length; j++){
System.out.println(array2[j]);
import java.util.*;
class Ab
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
String str=s.nextLine();
String [] arr=str.split(" ");
Integer [] a=new Integer[arr.length];
for(int i=0;i<arr.length;i++)
{
a[i]=Integer.parseInt(arr[i]);
}
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
您可以嘗試一下,並可以進行相應的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.