5 Replies - 454 Views - Last Post: 04 May 2013 - 06:54 AM Rate Topic: -----

#1 dzeko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-May 13

Problem with adding user input created objects to ArrayList

Posted 04 May 2013 - 01:27 AM

Hi everyone.

I have problem with adding objects to ArrayList. I'm creating objects like SportShoes and Socks through method written like

	public void novSportniCevelj() throws Exception {
		BufferedReader vhod = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Vnesi podatke sportnega cevlja!");
		System.out.print("Znamka: ");
		znamka = vhod.readLine();
		System.out.print("Model: ");
		model = vhod.readLine();
		System.out.print("Velikost: ");
		velikost = Double.parseDouble(vhod.readLine());
		System.out.print("Barva: ");
		barva = vhod.readLine();
		System.out.print("Spol: ");
		Spol spol = Spol.valueOf(vhod.readLine());
		System.out.print("Namen: ");
		Namen namen = Namen.valueOf(vhod.readLine());
		System.out.print("Cena: ");
		cena = Double.parseDouble(vhod.readLine());
		System.out.print("Popust: ");
		popust = Integer.parseInt(vhod.readLine());
		System.out.print("Zaloga: ");
		zaloga = Integer.parseInt(vhod.readLine());
		System.out.println("Vnos uspesen!");
	}



Now I'd like user created object through this "user friendly" input to be stored in ArrayList. Where and how do I create this ArrayList of SportShoes objects?


Whole SportShoes (SportniCevelj.java) is this -

import java.io.*;

public class SportniCevelj extends Cevelj {

	private Namen namen;
	
	public SportniCevelj() {
		super();
		this.namen = Namen.TEK;
	}
	
	public SportniCevelj(String z, String m, double v, String b, double c, int p, int za, Spol s, Namen n) {
		super(z,m,v,b,c,p,za,s);
		this.namen = n;
	}
	
	public void nastaviNamen(Namen n) {
		namen = n;
	}
	
	public Namen vrniNamen() {
		return namen;
	}
	
	public String toString() {
		return "Sportni cevelj " + znamka + " " + model + " je velikost " + velikost + " stane " + cena + " EUR in je namenjen za " + vrniNamen() + ".";
	}
	
	public static void vnosIzDatoteke() throws IOException {
		//
	}
	
	public static void izpisVDatoteko() throws IOException {
		//
	}
	
	public static void iskanje() {
		//
	}

	public void novSportniCevelj() throws Exception {
		SportniCevelj sc1 = new SportniCevelj();
		BufferedReader vhod = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Vnesi podatke sportnega cevlja!");
		System.out.print("Znamka: ");
		znamka = vhod.readLine();
		System.out.print("Model: ");
		model = vhod.readLine();
		System.out.print("Velikost: ");
		velikost = Double.parseDouble(vhod.readLine());
		System.out.print("Barva: ");
		barva = vhod.readLine();
		System.out.print("Spol: ");
		Spol spol = Spol.valueOf(vhod.readLine());
		System.out.print("Namen: ");
		Namen namen = Namen.valueOf(vhod.readLine());
		System.out.print("Cena: ");
		cena = Double.parseDouble(vhod.readLine());
		System.out.print("Popust: ");
		popust = Integer.parseInt(vhod.readLine());
		System.out.print("Zaloga: ");
		zaloga = Integer.parseInt(vhod.readLine());
		System.out.println("Vnos uspesen!");
	}
	
}



So far I was thinking of creating it in my main program like this -

import java.io.*;
import java.util.ArrayList;

public class GlavniProgram {
	
	public static ArrayList<SportniCevelj> seznamSportnihCevljev = new ArrayList<SportniCevelj>();
	public static ArrayList<PohodniskiCevelj> seznamPohodniskihCevljev = new ArrayList<PohodniskiCevelj>();
	public static ArrayList<Skorenj> seznamSkornjev = new ArrayList<Skorenj>();
	public static ArrayList<Nogavice> seznamNogavic = new ArrayList<Nogavice>();
	public static ArrayList<Vezalke> seznamVezalk = new ArrayList<Vezalke>();
	public static ArrayList<Krtaca> seznamKrtac = new ArrayList<Krtaca>();
	
	public static void main(String[] args) throws Exception {
		
		//
		
	}
	
}



Thank you all for help in advance.


Dejan

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with adding user input created objects to ArrayList

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem with adding user input created objects to ArrayList

Posted 04 May 2013 - 01:49 AM

Yes, you should create your ArrayLists in your driver class, so that they will be useful to you later when you try to make use of them.
//collect the user input, create a new object from that, and add it to the ArrayList
seznamSportnihCevljev.add(new SportniCevelj(z, m, v, b, c, p, za, s, n));



Further Reading:
- JPF > How to use an ArrayList and what is its advantage over array?
Was This Post Helpful? 1
  • +
  • -

#3 dzeko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-May 13

Re: Problem with adding user input created objects to ArrayList

Posted 04 May 2013 - 02:09 AM

If I put ArrayList in my main program and write your code it doesn't work. I get error: cannot find symbol, symbol: variable z (all variables). Any idea?

But it works if I write

import java.io.*;
import java.util.ArrayList;

public class GlavniProgram {
	
	public static ArrayList<SportniCevelj> seznamSportnihCevljev = new ArrayList<SportniCevelj>();
	public static ArrayList<PohodniskiCevelj> seznamPohodniskihCevljev = new ArrayList<PohodniskiCevelj>();
	public static ArrayList<Skorenj> seznamSkornjev = new ArrayList<Skorenj>();
	public static ArrayList<Nogavice> seznamNogavic = new ArrayList<Nogavice>();
	public static ArrayList<Vezalke> seznamVezalk = new ArrayList<Vezalke>();
	public static ArrayList<Krtaca> seznamKrtac = new ArrayList<Krtaca>();
	
	public static void main(String[] args) throws Exception {
		
		
		for(int i = 0; i < 2; i++) {
			SportniCevelj sc = new SportniCevelj();
			sc.novSportniCevelj();
			seznamSportnihCevljev.add(sc);
		}
		
		//seznamSportnihCevljev.add(new SportniCevelj(z,m,v,b,c,p,za,s,n));
		
		
		System.out.println(seznamSportnihCevljev.get(0));
		System.out.println(seznamSportnihCevljev.get(1));
		
	}
	
}

Was This Post Helpful? 0
  • +
  • -

#4 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem with adding user input created objects to ArrayList

Posted 04 May 2013 - 02:26 AM

Quote

I get error: cannot find symbol, symbol: variable z (all variables). Any idea?

I don't expect you to plug in variables that don't exist, those are just the parameter identifiers that you've chosen for your constructor. Use values that do exist.

are znamka, model, velikost, barva, cena, popust, and zaloga declared in your parent class?
Was This Post Helpful? 3
  • +
  • -

#5 dzeko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-May 13

Re: Problem with adding user input created objects to ArrayList

Posted 04 May 2013 - 02:32 AM

This class Cevelj is parent to SportniCevelj -

import java.io.*;

abstract class Cevelj {
	
	String znamka;
	String model;
	double velikost;
	String barva;
	double cena;
	int popust;
	int zaloga;
	Spol spol;
	
	public Cevelj() {
		znamka = "";
		model = "";
		velikost = 0.0;
		barva = "";
		cena = 0.0;
		popust = 0;
		zaloga = 0;
		spol = null;
	}
	
	public Cevelj(String z, String m, double v, String b, double c, int p, int za, Spol s) {
		znamka = z;
		model = m;
		velikost = v;
		barva = b;
		cena = c;
		popust = p;
		zaloga = za;
		spol = s;
	}
	
	public void nastaviZnamko(String z) {
		znamka = z;
	}
	public void nastaviModel(String m) {
		model = m;
	}
	public void nastaviVelikost(double v) {
		velikost = v;
	}
	public void nastaviBarvo(String B)/>/> {
		barva = b;
	}
	public void nastaviCeno(double c) {
		cena = c;
	}
	public void nastaviPopust(int p) {
		popust = p;
	}
	public void nastaviZalogo(int z) {
		zaloga = z;
	}
	public void nastaviSpol(Spol s) {
		spol = s;
	}
	
	public String vrniZnamko() {
		return znamka;
	}
	public String vrniModel() {
		return model;
	}
	public double vrniVelikost() {
		return velikost;
	}
	public String vrniBarvo() {
		return barva;
	}
	public double vrniCeno() {
		return cena;
	}
	public int vrniPopust() {
		return popust;
	}
	public int vrniZalogo() {
		return zaloga;
	}
	public Spol vrniSpol() {
		return spol;
	}
	
	public String toString() {
		return "Cevelj " + znamka + " " + model + " je velikosti " + velikost + " in stane " + cena + " EUR.";
	}
	
	public double cenaSPopustom() {
		return cena - (cena/popust);
	}
	
	public static void preberiIzDatoteke(BufferedReader dat) throws IOException {
		//
	}
		
	public void novCevelj() throws Exception {
		BufferedReader vhod = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Vnesi podatke cevlja!");
		System.out.print("Znamka: ");
		znamka = vhod.readLine();
		System.out.print("Model: ");
		model = vhod.readLine();
		System.out.print("Velikost: ");
		velikost = Double.parseDouble(vhod.readLine());
		System.out.print("Barva: ");
		barva = vhod.readLine();
		System.out.print("Spol: ");
		Spol spol = Spol.valueOf(vhod.readLine());
		System.out.print("Cena: ");
		cena = Double.parseDouble(vhod.readLine());
		System.out.print("Popust: ");
		popust = Integer.parseInt(vhod.readLine());
		System.out.print("Zaloga: ");
		zaloga = Integer.parseInt(vhod.readLine());
		System.out.println("Vnos uspesen!");
	}
}



Then I have class SportniCevelj that extends class Cevelj -

import java.io.*;
import java.util.ArrayList;

public class SportniCevelj extends Cevelj {

	private Namen namen;
	
	public SportniCevelj() {
		super();
		this.namen = Namen.TEK;
	}
	
	public SportniCevelj(String z, String m, double v, String b, double c, int p, int za, Spol s, Namen n) {
		super(z,m,v,b,c,p,za,s);
		this.namen = n;
	}
	
	public void nastaviNamen(Namen n) {
		namen = n;
	}
	
	public Namen vrniNamen() {
		return namen;
	}
	
	public String toString() {
		return "Sportni cevelj " + znamka + " " + model + " je velikost " + velikost + " stane " + cena + " EUR in je namenjen za " + vrniNamen() + ".";
	}
	
	public void novSportniCevelj() throws Exception {
		BufferedReader vhod = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Vnesi podatke sportnega cevlja!");
		System.out.print("Znamka: ");
		znamka = vhod.readLine();
		System.out.print("Model: ");
		model = vhod.readLine();
		System.out.print("Velikost: ");
		velikost = Double.parseDouble(vhod.readLine());
		System.out.print("Barva: ");
		barva = vhod.readLine();
		System.out.print("Spol: ");
		Spol spol = Spol.valueOf(vhod.readLine());
		System.out.print("Namen: ");
		Namen namen = Namen.valueOf(vhod.readLine());
		System.out.print("Cena: ");
		cena = Double.parseDouble(vhod.readLine());
		System.out.print("Popust: ");
		popust = Integer.parseInt(vhod.readLine());
		System.out.print("Zaloga: ");
		zaloga = Integer.parseInt(vhod.readLine());
		System.out.println("Vnos uspesen!");
	}
	
}



Where do I now declare (and how) ArrayList for SportniCevelj type objects? I'd like user to input znamka, model, ... and then put that object in ArrayList seznamSportnihCevljev.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with adding user input created objects to ArrayList

Posted 04 May 2013 - 06:54 AM

ArrayList<SportniCevelj> arList = new ArrayList<SportniCevelj>();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1