jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Begginer problems with ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Begginer problems with ArrayList" Watch "Begginer problems with ArrayList" New topic

Begginer problems with ArrayList

Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Ok, so im making this little program to prove that im familliar with i/o streams.
What i want to make is a program that makes a catalog on my desktop, and allows you to make a shoppinglist,
where you add everything you want to buy and its cost's.
I've got no problems with creating files, my problem lays in putting the objects into an arrayList and returning it.
I'l attatch my codes, sorry for the swedish in the program.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Main {

static Scanner in = new Scanner(System.in);
public static void main (String[] args) throws IOException
File fil = new File("C:\\Users\\Shaggy\\Desktop\\Wallet");

System.out.println("Vad vill du att inkopslistan ska heta?");
String namn = in.next();
Filer.skapaListaFil(fil, namn);

System.out.println("Skapa en vara till inköpslistan:");
String vara = in.next();
System.out.println("Vad kostar varan?");
int pris = in.nextInt();

Varor v = new Varor(vara, pris);

//These prints works perfectly = the object has been created


//here it doesint print anything

The class Varor:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Varor {

String vara;
int pris;
static java.util.List varor = Collections.synchronizedList(new ArrayList());
static String[] listan = new String[varor.size()];

public Varor(String a, int b)
vara = a;
pris = b;

public static void addToList(Varor v)

I'l apologize for the lack of comments.

Edit: ok i think i should explain more specific.
There is no errors in the code when i compile it, but when im using the method System.out.println(Varor.varor) it doesint work.
I also tryed to do a toString method, then the print works but the list seams to be empty.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
In the future, please UseCodeTags. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.

Does it really print nothing? It should print something like "[Varor@3f3789]", which would be the String representation of the List object. If you want to show the individual List elements, then you should iterate over the List.

Also, please fix the umlaut in your last name; you can do that under the "My Profile" link.
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Yeah sorry, im new here. I will start to use the Code command from now on.

Your right,


is my output.

Thanks for the help, i'l read about iterating arraylist and then try it.
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Ok, i added this piece of code to the program in my Main class:

But the output i get is still:


What does the output tell me? That there is nothing in the ArrayList?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
No, to the contrary, it tells you that there is a single element in the list, and it also tells you that the Varor class doesn't have a toString() method. Add something like this to the Varor class:
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Thanks alot, works great

For some reason it doesint work to change rows when i write something to a file.

Wrote a code like this:

Paul Clapham

Joined: Oct 14, 2005
Posts: 19760

Doesn't work to change rows???

Let me guess, that means that when you display the output in Notepad, everything appears on one line, instead of each of the varor appearing on a separate line?

You could just avoid Notepad, it's the only text editor in the world which doesn't recognize \n by itself as an end-of-line character. Try Wordpad instead.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44621
Paul Clapham wrote: . . . You could just avoid Notepad, . . . Try Wordpad instead.
Windows users would do well to try NotePad++ NotePad2 and jEdit.
I agree. Here's the link: http://aspose.com/file-tools
subject: Begginer problems with ArrayList
It's not a secret anymore!