File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help With With HeadFirst Java Chap.6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help With With HeadFirst Java Chap.6" Watch "Help With With HeadFirst Java Chap.6" New topic
Author

Help With With HeadFirst Java Chap.6

Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
I have read over the other forums, and nothing points to what i need help with. In chap.6 page 139 (new and improved DotCom class). I have made all the changes to the class. So when i try to compile the files.

DotCom.java
DotComTestDrive.java
GameHelper.java

I get an error,
------------------------------------------------------------------------------------------------

C:\MyJava\Chapter 6>javac DotComTestDrive.java
DotComTestDrive.java:18: setLocationCells<java.util.ArrayList<java.lang.String>>
in DotCom cannot be applied to <int[]>
theDotCom.setLocationCells<locations>;
^
1 error
-------------------------------------------------------------------------------------------------
Thanks for the help. Just starting out with java
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Hi Adam

Welcome to the ranch

If you post the code you compiled, people here can answer your question in a jiffy...

Can you please post the code of DotComTestDrive.java
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
Here is the code for DotComTestDrive.java
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

From the error listing I conclude that the setLocationCells method takes an ArrayList of String and not an int array. Please modify your code appropriately.

Kindly enclose your code within "" tags.


S.D. MADHAN
Not many get the right opportunity !
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
I don't understand what you are saying. Do i change the <String> to <array>?
Im a total noob, at java
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

In your code listing, I only see that the setLocationCells method takes an int array as argument, you need to pass an ArrayList<String> as argument.

What you are going to pass as ArrayList<String> is known only to you!
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
There are 3 .java files.
this is the DotCom.java file



Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
This is the DotComTestDrive.java file


and the other is a gamehelper.java file
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Adam Burda wrote:
DotComTestDrive.java:18: setLocationCells<java.util.ArrayList<java.lang.String>>
in DotCom cannot be applied to <int[]>
theDotCom.setLocationCells<locations>;
^
1 error

The error message means that you are trying to call the method setLocationCells of class DotCom by passing an int[] (array of ints) to it, while it expects an ArrayList<String>.

Look in your code, lines 16 - 18 of DotComTestDrive.java (note that the error message tells you that the error is in line 18 of DotComTestDrive.java):

You see that locations is an int[], and in line 18 you're calling setLocationCells with this int[].

If you now look at line 9 of DotCom.java:

You see that the method expects an ArrayList<String>, not an int[].

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

My comment still holds good.

In my opinion, you may be a newbee to Java, however, I believe you do understand the problem you are trying to solve. If not, you cannot debug any program in any language.

You need to figure out what ArrayList<String> is going to contain and call the setLocationCells method appropriately.
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
ok this is a dumb question.
How do i fix it?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Either by calling the method with the correct type of arguments, which means that you must make locations an ArrayList<String> (ArrayList of strings) instead of an int[] (int array) in DotComTestDrive.java, or by changing the arguments to the method from ArrayList<String> to int[] (which means you will have to change other parts of DotCom.java as well).

Ofcourse, if you do the first, then you must change line 16 of DotComTestDrive.java, but note that you can't just change the type of locations, you'll have to initialize it differently as well.
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
Please forgive me, Im still not understanding.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

What exactly are you not understanding?

Do you know how variables work (variables have a type, etc.)? If not, look at: Variables

Do you know how methods work (you can pass arguments to a method, they can return values, etc.)? If not, look at Defining Methods

Do you understand what an array is, such as the int[] that you have in your code? If not, look at Arrays

Do you understand what an ArrayList is? It's one of the collection classes.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Adam Burda wrote:Please forgive me, Im still not understanding.

If you need to cut paper, you need scissors.
If you need to drive screw, you need screwdriver.

Here, DotCom.setLocationCells need List of String.
But you have passed Array of int.

DotCom class is for checking the guess of String.
DotComTestDrive is for making guess of int.
You need to change either one to have single data type for guessing.

Moreover, the random numbers you generated not really 3 random numbers.
They are consecutive numbers.
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
Ok i know what an int, string, and an array is. And i understand that i need to change one to the other. But how?

Do I change line 16 in DotComTestDrive from int[] locations = {randomNum, randomNum+1, randomNum+2}; to <String> locations = {randomNum, randomNum+1, randomNum+2};

or do I change line 9 in DotCom Class from public void setLocationCells(ArrayList<String> loc) to public void setLocationCells(ArrayList int[] loc) ?
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
Okay, I think you are having trouble with type safety. Jump to page 540 of Head First Java and read that page.

The page 137 starts the hint though.
4. ArrayLists in Java 5.0 are parameterized.
We just said that unlike arrays, ArrayLists have no special syntax. But they do use something special that was added in Java 5.0 Tiger -- parameterized types.


The <String> in angle brackets is a "type parameter". ArrayList<String> means simply "a list of Strings", as opposed to ArrayList<Dog> which means, "a list of Dogs".


Also, Page 139 from the Errata for Head First Java:
In the 2nd comment where says: "Change the String array to an ArrayList that holds
Strings" should say: "Change the int array to an ArrayList that holds Strings"
(You are changing from a int[] to a ArrayList<String>.)


Head First Java doesn't give you the complete code for remaking the application with ArrayList<String>. My guess is that this section was chopped into the book after Java 5 came out, and they were just giving you an example of how type safety works but not expecting you to think too much about it, or compile it, until later chapters.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2835
    
  11

You could change either place, as long as you get a match between the two. However, choices aren't really friends to beginners. I know when I'm learning something new, I just want to be told one way to do something, and not "You could do this, or you could do that," even though well-intentioned,

So:

1. Create an array of strings. You can use Integer.toString(randomNumber) to convert your integers to Strings. Yes, you'll have to call that three times, once for each int. Your int array will have to be String array instead.

2. Use Arrays.toList(yourArray) to convert this array to a list.

3. Use this list as the parameter to setLocationCells().

4. Change your locationCells variable to be List<String> instead of ArrayList<String>, and change the setLocationCells() method too. (In general, use List, an interface, instead of ArrayList, a class, unless you're actually instantiating an object.)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37985
    
  22
Never use == true or == false. They are poor style, and error prone if you mistakenly write = for ==.
Not if (b == true) ... but if (b) ...
Not if (b == false) ... but if (!b) ...
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
Ok first let me say thank you to all of you that have tryed to help me. I have read your posts, I have tryed to change the code im working on with no success.
Can some body please show me the correct lines in the code that i need to change and to what they need to be. Out of all the noob's im the bigest. Or maybe just
a real dumb noob!

And again I do thank you for all your help.
hugo zheng
Greenhorn

Joined: Nov 01, 2011
Posts: 2
Adam Burda wrote:Ok first let me say thank you to all of you that have tryed to help me. I have read your posts, I have tryed to change the code im working on with no success.
Can some body please show me the correct lines in the code that i need to change and to what they need to be. Out of all the noob's im the bigest. Or maybe just
a real dumb noob!

And again I do thank you for all your help.


I give you some code just for test below,and hope it will help...

...........................................................................................................................................
int randomNum = (int)(Math.random()*5);

String str1 = Integer.toString(randomNum);
String str2 = Integer.toString(randomNum+1);
String str3 = Integer.toString(randomNum+2);

String[] locations_string ={str1,str2,str3};
ArrayList<String> locations = new ArrayList<String>(Arrays.asList(locations_string));
theDotCom.setLocationCells(locations);
............................................................................................................................................
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37985
    
  22
hugo zheng welcome to the Ranch
Jacob Varghese
Greenhorn

Joined: Nov 28, 2011
Posts: 1
Walter Gabrielsen Iii wrote:Okay, I think you are having trouble with type safety. Jump to page 540 of Head First Java and read that page.

The page 137 starts the hint though.
4. ArrayLists in Java 5.0 are parameterized.
We just said that unlike arrays, ArrayLists have no special syntax. But they do use something special that was added in Java 5.0 Tiger -- parameterized types.


The <String> in angle brackets is a "type parameter". ArrayList<String> means simply "a list of Strings", as opposed to ArrayList<Dog> which means, "a list of Dogs".


Also, Page 139 from the Errata for Head First Java:
In the 2nd comment where says: "Change the String array to an ArrayList that holds
Strings" should say: "Change the int array to an ArrayList that holds Strings"
(You are changing from a int[] to a ArrayList<String>.)


Head First Java doesn't give you the complete code for remaking the application with ArrayList<String>. My guess is that this section was chopped into the book after Java 5 came out, and they were just giving you an example of how type safety works but not expecting you to think too much about it, or compile it, until later chapters.


!!! THIS IS THE BEST EXPLANATION !!!

Since you are here, I believe that you may be trying to run the project from HeadFirstJava Second Edition [ DotCom Project (Simple version at page 139) ]

I suggest you move on to the next page. Don't waste time trying to solve this problem here. There is more fun ahead

Cheers !!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37985
    
  22
Welcome to the Ranch Jacob Varghese.

Please avoid writing in CAPITAL LETTERS.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help With With HeadFirst Java Chap.6
 
Similar Threads
Head First Java Programs
Head First Java P148 - DotComBust - compile errors
Head First Java DotCom, DotComBust, GameHelper
symbol cannot be found
Help with ArrayList