aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Begginer problems with ArrayList" Watch "Begginer problems with ArrayList" New topic
Author

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.

Main.java:
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");
fil.mkdir();

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);

System.out.println(v.vara);
System.out.println(v.pris);
//These prints works perfectly = the object has been created

Varor.addToList(v);

System.out.println(Varor.varor);
//here it doesint print anything
}
}

The class Varor:



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


@SuppressWarnings("unchecked")
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)
{
varor.add(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
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
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,

[Varor@99353f]

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:

[Varor@12b7eea]
Varor@12b7eea

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

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
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
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

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
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Paul Clapham wrote: . . . You could just avoid Notepad, . . . Try Wordpad instead.
Windows users would do well to try NotePad++ NotePad2 and jEdit.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Begginer problems with ArrayList
 
Similar Threads
Issue about ArrayList
Problems w/ a HashMap
Writing a sum to a file
ArrayList Problem