如何读取字符串然后将其保存到java中的数组中

我想读取一个字符串,然后把它放在java中一个字符串数组的第一个位置


for (int i=1;i<100;i++) {


            System.out.print("Enter a string : ");


            Scanner scanner = new Scanner(System. in);


            String inputString = scanner.nextLine();

            System.out.println("String read from console is : \n"+inputString);

            inputString = thisIsAStringArray[];

}


呼唤远方
浏览 114回答 2
2回答

扬帆大鱼

读取一个字符串应该用 while 循环来完成,但你想做的是在这里完成的。此解决方案仅将字符串保存到第一个索引。public static void main(String [] args) {&nbsp; &nbsp; &nbsp; &nbsp;String [] thisIsAStringArray = new String[20]; //Only 20 string can be saved.&nbsp; &nbsp; &nbsp; &nbsp;Scanner in = new Scanner(System.in);&nbsp; &nbsp; &nbsp; for(int i = 0; i < 100;i++) {&nbsp; &nbsp; &nbsp; &nbsp; thisIsAStringArray[0] = in.nextLine(); ///Save element to first position.&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("String entered is " + thisIsAStringArray[0]); //Get the element from the first position.&nbsp; &nbsp; &nbsp; &nbsp;}}此解决方案不会保存到第一个位置,而是保存到数组的每个索引。public static void main(String [] args) {&nbsp; &nbsp; &nbsp; &nbsp; String [] thisIsAStringArray = new String[20]; //Only 20 string can be saved.&nbsp; &nbsp; &nbsp; &nbsp; Scanner in = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < thisIsAStringArray.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thisIsAStringArray[i] = in.nextLine(); ///Save element to first position.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("String entered is " + thisIsAStringArray[i]); //Get the element from the first position.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

智慧大石

因此,您创建了一个 String 列表并将 String 分配到 String 数组的头部。并且您使用辅助函数获得数组头部的索引。String[] thisIsAStringArray = new String[10];for (int i=1;1<100;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter a string : ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner scanner = new Scanner(System. in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String inputString = scanner.nextLine(); // READ IN THE STRING&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("String read from console is : \n"+inputString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pos= firstNonullPosition(thisIsAStringArray);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(pos!=-1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thisIsAStringArray[]=inputString;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Array is full!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The String at the position 0 of the String array thiIsAStringArray is:" +thisIsAStringArray[0]);}public static int firstNonullPosition(String[] a){&nbsp; &nbsp; int index=0;&nbsp; &nbsp; while(a[index]!= null)&nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (index > a.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return -1;&nbsp; &nbsp; }&nbsp; &nbsp; return index;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java