aspose file tools*
The moose likes Beginning Java and the fly likes adding from scanner to ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding from scanner to ArrayList" Watch "adding from scanner to ArrayList" New topic
Author

adding from scanner to ArrayList

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Hello,
I'm getting an error when I try to add an element to an ArrayList from my Scanner and I think it has something to do with scanner s not being of type <Patient>. Am I right, how do I fix it? Error line 7.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I think I made progress but still no go with error line 7. Cannot find symbol variable next location variable s of type java.util.scanner.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
next is a method. So you should use like s.next().

Also you can change your ArrayList name from patient to patientsList or at least patients. Having the class name and variable name same really confuses.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8249
    
  23

Dustin Schreader wrote:I think I made progress but still no go with error line 7. Cannot find symbol variable next location variable s of type java.util.scanner.

I suspect you didn't read the advice in my post here properly.

You''re still bogged down in the bits and bytes of Java, and I can tell from your code that you're floundering badly.

My advice:
1. Turn your computer OFF.
2. Sit down with a pencil and paper and describe exactly what you want to do in English.
3. When (and only when) you understand ALL the steps that you want to perform, turn your computer back on and:
4. Read the API documentation for java.util.Scanner, or indeed the Java tutorials, very carefully, and make sure you understand them.
5. Start a new program that follows the steps you described in Step 2.

Right now, you're getting distracted by all sorts of stuff, like try...catch blocks (which you probably don't need; but you'll at least understand a bit better if you've read the tutorials) and how to use Scanner. DON'T. Write a simple, clear program that follows steps you have already described to yourself and understood; you'll find it's a lot easier.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
Also beware of methods which take lots of arguments. I saw a “powerpoint” of Joshua Bloch’s once which says you should not require more than three parameters for a method, because of the risk of confusing them. I would think you are better passing a Patient object (or more precisely the value of a reference to a Patient object) to that method, if possible.
As well as writing what you are doing with a Patient object, also write down the number of List objects you have, when they are created, when they have things added to them, and when they vanish.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Ok I've been doing some reading and found out a little more about generics and arraylists and scanners. Since patientList is an ArrayList of type <Patient> I should be able to do add(E e) or in my case patientList.add(Patient (s.next())); and since next() from the scanner returns a String I should be able to say that s.next() string is of type Patient correct? Well I guess it's not correct because I get the error cannot find symbol method Patient(java.lang.String) on line 7. I don't understand why it wants to see it as a method instead of making s.next() type Patient.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8249
    
  23

Dustin Schreader wrote:Ok I've been doing some reading and found out a little more about generics and arraylists and scanners.

Well I'm afraid you still haven't done enough because
patientList.add(Patient (s.next()));
is not only tortured logic, it is not valid Java.

Have you written down the steps you need to do in English, as I suggested? It seems to me that you're still trying to "code your way out of trouble", and that is likely to lead to a long and unrewarding experience with programming.

s.next() returns a String, and only a String; and since the class is final, the only way you will be able to get a Patient out of it is by some form of conversion (of which I see no sign).

I also see that you've now added a Logger before you've even got the rest of your code working, which smacks of existentialism to me. Jean-Paul Sartre may have been a great philosopher, but his ideas belong in books, not in programs.

Winston
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Netbeans threw the logger automatically since I needed a try catch so I haven't had time to mess around with that yet. I wrote down pseudo code of all the classes. I have created a person class, a physician class, a patient class, and a person interface all methods having no more than one variable except the constructor. My addPt method looks cleaner already I think. I have looked through oracles class arraylist, tutorials on generics. I didn't know oracle had tutorials on conversions I will look those up now. Thank you!
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Found a conversion example and slightly modified it. I don't understand why they would use interface but I got it to compile. Thanks for the help everyone!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8249
    
  23

Dustin Schreader wrote:Netbeans threw the logger automatically since I needed a try catch...

Blimey! Yet another reason (if any more were needed) to avoid using IDEs while you're learning the language.

BTW, I'm not at all convinced that you do need a try...catch block, but I am aware that the tutorials example uses one.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
Dustin Schreader wrote:Found a conversion example and slightly modified it.. . .
No, don’t go looking for somebody else’s code. Write your own Patient class. Test it by passing Strings to its constructor. Get that working (without using an IDE) and the consider adding it to a List. You will find these things much simpler if you break them down into tiny pieces.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I wrote my own patient class and I am able to pass strings to the constructor. I just couldn't figure out how to convert string to generic type.. which I still don't really understand but someone had a similar code for the conversion. I think it was a little to much for me to handle. I'm just trying to read a string from a text file and insert that into an arraylist<Patient> and I couldn't figure out what to do to convert it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
I hope I’m not too late, but why do you need to convert Strings to another generic type? You get all the details for the Patient from the String input, using that in the constructor to initialise all its fields.

Now you’ve done that, you have the information in the format you want it, i.e. a Patient. Now you use that Patient object.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
The only problem with that is when I want to create an ArrayList<Patient> patientList.... from the CSV file the scanner reads in the arraylist does not except the data from the CSV file because it's a string. I get an error saying cannot find symbol method Patient(java.lang.String).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8249
    
  23

Dustin Schreader wrote:The only problem with that is when I want to create an ArrayList<Patient> patientList.... from the CSV file the scanner reads in the arraylist does not except the data from the CSV file because it's a string. I get an error saying cannot find symbol method Patient(java.lang.String).

That means that it can't find a constructor with the signature Patient(String someString). Have you written one? I haven't seen any evidence so far. Perhaps you should show us your entire Patient class.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8249
    
  23

Double post. Apologies.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding from scanner to ArrayList