Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (2014) Coding the Fun Facts Using an Array

James Ward
James Ward
1,787 Points

Stuck with conversion of String to Int

I think I know what the problem is, but not how to fix it, My result is a string and the object (?) is an int.

Heres the error;

JavaTester.java:42: error: cannot find symbol int numberOfSports = bestSport.nextInt(sports.length); ^ symbol: method nextInt(int) location: variable bestSport of type String 1 error

I tried a few things, but the challenges are quite finicky for exact syntax so googling and using parseInt etc obviously doesnt work :)

Thanks team appreciate the help

Array.java
String[] sports = {
  "Basketball",
  "Baseball",
  "Tennis"};

String bestSport = sports[0];

int numberOfSports = bestSport.nextInt(sports.length);

2 Answers

Axel McCode
Axel McCode
13,869 Points
//My guess is that you are initializing the variable numberOfSports
// to the number of sports in your sports array, in this case you would
// not need to have bestSport.nextInt(Sports.length)


String[] sports = {
  "Basketball",
  "Baseball",
  "Tennis"};

String bestSport = sports[0];

int numberOfSports = sports.length;

Hope this helps :)

James Ward
James Ward
1,787 Points

Thanks Axel, that worked a treat

Axel McCode
Axel McCode
13,869 Points

You're very welcome, glad I could help out!