Reputation: 14189
I have to read from a file, so made:
cod_order (int)
cod_cliente (int)
cod_pizza (int)
num_pizza (int)
cod_pizza
num_pizza
...
$FINE
here is an example:
1
1107
02
1
01
5
03
2
$FINE
I created this method to read from this text file:
private void loadOrdini(String fname){
try{
BufferedReader reader = new BufferedReader(new FileReader(fname));
String cod_ordine = reader.readLine();
while(cod_ordine!=null){
String cod_cliente=reader.readLine();
Cliente cl=listaclienti.get(Integer.parseInt(cod_cliente));
String cod_pizza=reader.readLine();
while(!cod_pizza.equals("$FINE")){
String n_pizze=reader.readLine();
Ordine ord = new Ordine(Integer.parseInt(cod_pizza),Integer.parseInt(n_pizze));
cl.getListaOrdini().put(Integer.parseInt(cod_ordine), ord);
cod_pizza=reader.readLine();
}
cod_ordine=reader.readLine();
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
the problem is the method load into the HashMap listaordini only the numbers 03
and 2
while the numbers before aren't considerer.why?
import java.util.*;
public class Cliente{
private String name;
private String address;
private Map<Integer,Ordine> listaordini;
public Cliente(String n,String addr){
name=n;
address=addr;
listaordini=new HashMap<Integer,Ordine>();
}
public String getName(){
return name;
}
public String getAddr(){
return address;
}
public Map<Integer,Ordine> getListaOrdini(){
return listaordini;
}
public String toString(){
String temp="";
temp+="Nome Cliente: "+name;
temp+="\nIndirizzo: "+address;
return temp;
}
}
even after this suggets the problem remain the same
Upvotes: 1
Views: 164
Reputation: 14791
I think the other answers have missed the point: you want a mapping from order IDs to pizzas.
The problem is, a map (such as a HashMap) must have a unique mapping for each key. What that means is, you can only map a single Ordine
to the order ID, a map doesn't support having multiple Ordine
s for the same order ID.
There's a couple of ways around this. You could make a new object that contains a list of Orders
and put that into the map, or you could change your map to map an order ID to a List
of orders.
For example, doing it the second way you would have:
// To create your map:
Map<Integer, List<Ordine>> listaordini = new HashMap<Integer, List<Ordine>>( );
// To add an order to it:
Ordine ord = new Ordine(Integer.parseInt(cod_pizza),Integer.parseInt(n_pizze));
List<Ordine> ordineList = cl.getListaOrdini( ).get(Integer.parseInt(cod_ordine));
if (ordineList == null)
{
ordineList = new ArrayList<Ordine>( );
cl.getListaOrdini( ).put(Integer.parseInt(cod_ordine), ordineList);
}
ordineList.add(ord);
Upvotes: 2
Reputation: 9451
cl.getListaOrdini().put(Integer.parseInt(cod_ordine), ord);
because all the value you put into this map use the same key, which is cod_ordine
, so every time you call this method, you replace the old ordine
with new one, that's why you only have last record.
Upvotes: 0
Reputation: 104050
You've called readLine()
too many times:
String cod_ordine = reader.readLine();
while(cod_ordine!=null){
String cod_cliente=reader.readLine();
Cliente cl=listaclienti.get(Integer.parseInt(reader.readLine()));
You populate cod_ordine
with the first readLine()
call.
You populate cod_cliente
with the second readLine()
call.
You create the client object with the third readLine()
call -- using what should have been a cod_pizza
value as a client value.
Try this:
String cod_ordine = reader.readLine();
while(cod_ordine!=null){
String cod_cliente=reader.readLine();
Cliente cl=listaclienti.get(Integer.parseInt(cod_cliente));
Upvotes: 0
Reputation: 8819
Every time you call reader.readLine(), you are moving to the next line in the file.
Thus, when you call
Cliente cl=listaclienti.get(Integer.parseInt(reader.readLine()));
You are actually moving to the next line and getting 02 when you meant to parse 1107.
Upvotes: 3