Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Begginer problems with ArrayList

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Marshal
Posts: 26534
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: . . . You could just avoid Notepad, . . . Try Wordpad instead.

Windows users would do well to try NotePad++ NotePad2 and jEdit.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic