public class namePrinting
{
//each name in a series of names
public String[] names = {"Hello", "My", "Name", "Is", "Jose"};
//the method for putting names in
public static void printNames(String [] namesArray)
{
//go through each String in the array and then print each
for(int i = 0; i <= namesArray.length; i++)
{
System.out.println(namesArray[i]);
}
}
//print the names in this specific array
printNames(names[]);
}
I'm pretty new to Java and was wondering what I was doing wrong? I just want to go through each string in the array and print each. I think the incorrect portion is at the bottom... is there a specific variable I am forgetting to put inside printNames?
Your code contains multiple errors:
names
array in a static method, but the names themselves aren't static. Either remove the static from the method, or add static to the field.printNames(names[]);
is on class-level, which isn't possible. Method calls should be in a method or constructor.names[]
as parameter input isn't valid either. This should have been printName(names)
instead.<=
should be <
. Because the length is 5 for your array, but the indices are [0-4].Try something like this (without static):
public class NamePrinter
{
//each name in a series of names
public String[] names = {"Hello", "My", "Name", "Is", "Jose"};
//the method for putting names in
public void printNames(String [] namesArray)
{
//go through each String in the array and then print each
for(int i = 0; i < namesArray.length; i++)
{
System.out.println(namesArray[i]);
}
}
public static void main(String[] a){
NamePrinter namePrinter = new NamePrinter();
//print the names in this specific array
namePrinter.printNames(namePrinter.names);
}
}
Or alternatively with static:
public class NamePrinter
{
//each name in a series of names
public static String[] names = {"Hello", "My", "Name", "Is", "Jose"};
//the method for putting names in
public static void printNames(String [] namesArray)
{
//go through each String in the array and then print each
for(int i = 0; i < namesArray.length; i++)
{
System.out.println(namesArray[i]);
}
}
public static void main(String[] a){
//print the names in this specific array
printNames(names);
}
}
Change:
for(int i = 0; i <= namesArray.length; i++)
to:
for(int i = 0; i < namesArray.length; i++)
,
because last iteration causes ArrayIndexOutOfBoundsException
(length of names
array is equal to 5, but last index is equal to 4 - indices of an array start from 0).
Change printNames(names[])
to printNames(names)
, because syntax that you have used is incorrect.
If you want to use names
array in the static
method, this array should be declared as static
too, on the other hand, you can delete static
keyword from printNames()
method.
You can't call printNames(names)
in the body of class as you did it. It should be put in some method or in the initialization block (I'm pretty sure that you should look to put it inside public static void main(String args[])
method.
To correct your code you have to :
printNames
method from a main
methodi <= namesArray.length
to i < namesArray.length
Your code gonna look like this :
public class namePrinting
{
public static void printNames(String [] namesArray)
{
for(int i = 0; i < namesArray.length; i++)
{
System.out.println(namesArray[i]);
}
}
public static void main(String[] args) {
String[] names = {"Hello", "My", "Name", "Is", "Jose"};
printNames(names);
}
}
But to simplify your code you can use foreach instead of for loop :
public static void printNames(String [] namesArray)
{
for (String name: namesArray) {
System.out.println(name);
}
}
And if you use Java 8 or +, you can use Stream Api like this :
public static void printNames(String [] namesArray)
{
Arrays.stream(namesArray).forEach(System.out::println);
}
Have fun ;)
1 Change printNames(names[]) to printNames(names)
2 Use public static String[] names = {"Hello", "My", "Name", "Is", "Jose"};
instead of public String[] names = {"Hello", "My", "Name", "Is", "Jose"};
3 Use for(int i = 0; i <= namesArray.length -1 ; i++)
instead of for(int i = 0; i <= namesArray.length; i++)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.