Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java The Challenge

Rafał Stasiak
Rafał Stasiak
3,763 Points

jshell error

Hello Team,

Please look on below code as I try to practice more arrays. When despite the challenge I try extra task to loop cows number and assing it to cow name I receive jshell error. Please take a look on my code and correct me where I am wrong.

import java.io.Console;

public class Arrays {

public static void main(String[] args) { Console console = System.console(); String[] names = {"Bella", "Stella", "Kasia", "Asia"}; Int[] cows = {1,2,3,4};

for(Int i = 0, i < names.length, i++){
System.out.printf("The name of cow #%d is %s %n", i+1, names.indexOf(i));
} 

} }

3 Answers

I made the following changes:

import java.io.Console;

public class Arrays {

  public static void main(String[] args) {
    Console console = System.console();
    String[] names = {"Bella", "Stella", "Kasia", "Asia"};

    for(int i = 0; i < names.length; i++){
      System.out.printf("The name of cow #%d is %s %n", i+1, names[i]);
    } 
  }
}

1) The statements in your for loop should be separated by semicolons.

2) With names.indexOf(i) i should be a string. Instead all you need is names[i].

3) Int[] cows = {1,2,3,4}; wasn't used

I think cause we've got class Cow.java, we should use it here.

import java.io.Console;

public class Arrays {

public static void main(String[] args) { Console console = System.console();

String[] cowNames = {"Bella", "Stella", "Kasia", "Asia"};
Cow[] cows = new Cow[cowNames.length];
for(int i = 0; i<cowNames.length; i++){
  cows[i] = new Cow(cowNames[i]);
  }
for(Cow cow : cows){
System.out.println(cow.getName());
}

} }

Rafał Stasiak
Rafał Stasiak
3,763 Points

Hello Kris,

Oh! So, thats Jshell is shout about he wants a separation by semicolons!

Please clarify starting Int = 0, by upper case is correct?

Reagarding names.indexOf(i) in my example if it would work correctly? I used array "names" which contains an array of strings?

Secodnly, I understend that exaple you showed "names[i]" will prevent errors if I will use int or double the premitive type of array?