Question:
array.length component in java?
Starlight*_*
2009-11-05 05:35:20 UTC
can someone correct this for me? just print array length to terminal?
.........................
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Capacity{
public static void main(String[] args) {
int arrayLength = array.length;
Scanner scanner =
new Scanner(System.in);
List list = new ArrayList();

for(int i= 0; i < 10; i++){
System.out.println("Enter " + "String # " + (i+1) + ":");
list.add(scanner.nextLine());
}

System.out.println("Now, enter" + " a new String:");
String search = scanner.nextLine();

System.out.format("The Java array length is %d", arrayLength);
}
}
Four answers:
Alberto
2009-11-05 05:46:20 UTC
The problem is on this line:



int arrayLength = array.length;



What is array? You haven't defined any variable called array, so it doesn't understand what that means.



You'll have to create an array variable that is called "array" in order to have this work.



Ex:



int[] array = new int[5];
?
2016-05-22 05:11:18 UTC
which one is harder is in the eye of the beholder, but i'd argue there's no point in learning VB unless you're forced to for work. VB is pretty much a legacy language just kept around for old-school people that never learned better languages like C++ or Java. So just learn Java, and don't worry about VB. Java's not hard to learn, you can get the basics in a few days.. Or check out C#, very similar to Java. You'll go a lot farther in the industry with C++/C#/Java than you ever would with VB.
Avaiz G
2009-11-05 06:17:43 UTC
The other two answers are correct, but basically the program doesnt make sense if you want to print the array length which you already know.



It MIGHT be helpful if you want the length of the ArrayList which contains the list of user input strings. For that you can just say print list.size() (but again you know there are going to be 10 elements from the loop terminating condition)
Mani S
2009-11-05 05:44:40 UTC
Where is the array?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...