ArrayList To File Using ObjectOutputStream
Apr 9, 2014
i have saved an arrayList to file using the ObjectOutputStream but now when i try to read the file i get problems.
saveOnFile:
public void saveOnFile() {
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("object.txt"));
output.writeObject(mediaList);
output.close();
} catch(Exception ex) {
ex.printStackTrace();
[code]....
View Replies
ADVERTISEMENT
Nov 28, 2014
My assignment is to create an ObjectOutputStream object mapped to a binary output file named "ItemRecord_binary.txt". So far I've created a ItemRecord class with a constructor with getters and setters and a override toString method. It compiled. This ItemRecordReport class does not compile because of a can't find symbol error. Here is the code:
import java.io.*;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class ItemRecordReport {
private ObjectOutputStream output;
[code]....
View Replies
View Related
Jul 10, 2014
I want to write formatted output on a notepad file using ObjectOutputStream but I am not getting it in human readable formatted form
Here is my person class
public class Person implements Serializable {
private String firstName;
private String lastName;
private int age;
public String getFirstName() {
return firstName;
[code]....
I want to know how to use printStream.print() like method to write formatted output.
View Replies
View Related
Jul 7, 2014
I am new on java. I have following xml file structure. What I need to do is that :
1) Read this xml file and convert to arraylist.
2) insert this arrayList into Mysql database.
XML File is
<?xml version="1.0"?>
<root>
<individualorders>
<individualorder>
<patientRole>
<id>8839</id>
[Code] ....
View Replies
View Related
Nov 18, 2014
I have to read this CSV file into an arraylist. Arraylist i call weight. The data in the csv file has 200 double values which is the weight of all the 200 bananas. Since the csv file is a string i guess I cannot create an double arraylist from scratch, but i have to parse the csv file afterwards, right? Well this does not work and ill show you the code in a bit, just need to state all my issues.
My second issue is how to be able to print out the the weight items (in this case bananas) next to the weight of the bananas taken from the array? This has to be done by constructor i assume? I just dont know how constructors work with arraylists.
Id like it to look something like this in the output window.
Banana 34,55
Banana 43,55
Banana 23,74
and so on (200 times)
so these are my issues, how to parse and how to print the arraylist weights and the item banana in the output window with constructor. Here is the code.
package Main;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.io.IOException;
import java.io.Serializable;
import javax.swing.JOptionPane;
[Code] ...
View Replies
View Related
May 25, 2014
i have to read a file and then store that file in an arraylist. i have 2 clasees, a pyramid and a cylinder class and the file contains unorganized shapes. i want to store all of them in the array but my code does not return anything
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
[Code].....
View Replies
View Related
Mar 1, 2015
Java Code:
package inventory;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class Inventory
{
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
ArrayList<Product> InventoryList = new ArrayList<>();
Inventory I = new Inventory();
int option = 0;
View Replies
View Related
Feb 6, 2015
I wanna learn to create a method where I use a scanner or any other way to input.The stuffs I input will be strings. The whole thing will be like a destination creator.So if I input the string USA, it will write this to a file:
1:USA
When I go to the method again the ID will be added so if I input UK, the file will contain:
1:USA
2:UK
Now if I terminate the program, next time when I run it again it will be able to read the ArrayList, so if I wanna go to the method again and write: Norway the file will contain:
1:USA
2:UK
3:Norway
Later I also want to be able to use println to show all of these lines from the file, as well I would like to be able to delete an ID, so if I delete ID 2 the file would contain:
1:USA
3:Norway
I'm quite new to Java, I'm in a Java course since soon 3 weeks back and I have sure learned a lot, all the loops and basic statements that exist in most languages, but now when it comes to this I just don't understand.
View Replies
View Related
Feb 15, 2015
The initial output of the texfile is this. NO ARRAYLIST OR COMPARATOR IS ALLOWED:
Steve Jobs 9 f 91
Bill Gates 6 m 90
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Steve Jobs 9 m 95
Dennis Ritchie 5 f 100
Jeff Dean 7 m 100
Bill Gates 6 f 96
Jeff Dean 7 f 100
Sergey Brin 27 f 97
Sergey Brin 22 m 98
The collateExams method collates/sorts exam objects starting with the first 'm' (midterm) of the first object and immediately followed by the same person's 'f'(final). Only a SINGLE loop construct is allowed. The output from collateExams() should be the one below but my code is not working, i.e. collateExams method is not working. The output from collateExams() should be
Bill Gates 6 m 90
Bill Gates 6 f 96
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Dennis Ritchie 5 f 100
Steve Jobs 9 m 95
Steve Jobs 9 f 91
Jeff Dean 7 m 100
Jeff Dean 7 f 100
Sergey Brin 22 m 98
Sergey Brin 27 f 97
I am getting [b]NullExceptions[/b] at
r[2*position[exams[i].getID()]+1] =
new Exam(r[i].getFirstName(), r[i].getLastName(),
r[i].getID(), r[i].getExamType(), r[i].getScore());
import java.io.*;
import java.util.*;
[Code] ....
View Replies
View Related
Dec 4, 2014
I have created this project and want to be able to add new members to my members arraylist, store the input in a .txt file and load the new members after closing and opening the program.
[URL] ....
View Replies
View Related
Apr 23, 2015
I am advised to use a while loop the Scanner method hasNextLine() and in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. what I have gotten so far:
Scanner input = new Scanner(new File(""));
while(input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
View Replies
View Related
Aug 25, 2014
I would like to create an ArrayList from data types stored in a text file.The ArrayList would be multidimensional with two data types; int, String.
Example text file could be:
4 cahiers grand format
3 stylots bic bleu
5 gommes
I find it very difficult for multiple data types i don't see how its possible.
View Replies
View Related
Apr 15, 2014
I have been trying to get this method to work for a few hours now, for some reason I get an IndexOutOfBounds exception every time. Basically I am trying to read a txt file and add the words to an ArrayList as strings separated by a space .
private ArrayList<String> readLinesFromFile(BufferedReader inputFile) throws IOException
{
String value = null;
ArrayList<String> result = new ArrayList<String>();
while((value = inputFile.readLine()) != null){
String[] values = value.split(" ");
for (int i = 0; i < values.length; i++){
result.add(values[i]);
}
}
return result;
}
View Replies
View Related
Sep 13, 2014
//compiler error I'm receiving
J:CS3Student Folder Review Lab #2APlusImage.java:41: error: cannot find symbol
xcor.add(read.nexInt());
[Code].....
View Replies
View Related
Feb 28, 2014
I'm trying to create my own arraylist using Collection. My program doesn't do anything. Obviously, I haven't a clue.
import java.util.Collection;
import java.util.Iterator;
public class MyArrayList<T> implements java.util.Collection<T> {
private int size = 4;
private T[] mArray;
public MyArrayList(String[] args) {
[Code] ....
View Replies
View Related
Jul 8, 2014
I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) ....
As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.
By default, the program is meant to have a starting inventory, which is why I have added them in the constructor
public class Inventory {
private List<Product> products;
public Inventory() {
addProduct(new Product("Frying pan", 15, 20));
addProduct(new PerishableProduct("Apple", 5.8, 30, 7));
addProduct(new ItemisedProduct("Cereal", 5.8, 0));
// this is where I am having problems. Wanting to add
// objects to the ItemisedProduct's ArrayList for cereal.
}
Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.
public class ItemisedProduct extends Product {
private ArrayList<String> serialNumbers = new ArrayList();
public ItemisedProduct(String name, double price, int qty) {
super(name, price, qty)
[Code] .....
My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
View Replies
View Related
Apr 23, 2015
I am trying to remove a line based on user input. myFile.txt looks like:
Matt
Brian
John
However when I enter "Brian" (to remove this line), It is deleted on the temp file (myTempFile.txt), but not renamed back to the original file (myFile).
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
[code]....
View Replies
View Related
Feb 26, 2015
I think it's a problem with my if then else statements but I'm not sure?
package Hw1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class ScrapWork {
public static void main(String[] args){
String[]Dalton={"Joe","William","Jack","Averell"};
[Code] .....
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of type
at Hw1.ScrapWork.main(ScrapWork.java:16)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
View Replies
View Related
Nov 14, 2014
I have an ArrayList with String, String, Double.
From another class I would like to print minimum and maximum including the first String name.
I have tried with import java.util.Collections; but that didn't work.
Java Code:
ArrayList<List>list = new ArrayList<List>();
list.add(new List("Abc","Cba",1));
list.add(new List("Bca","Bca",5));
list.add(new List("Cba","AcB",2)); mh_sh_highlight_all('java');
I would like the result to look like:
Minimum: Abc 1
Maximum: Bca 5
View Replies
View Related
Jun 2, 2014
Something like ArrayList<Object>[] = new ArrayList<Object>[]();
View Replies
View Related
Feb 14, 2015
I have an ArrayList of employee and ArrayLsit of bosses, and I want to keep those people in a temporary ArrayList , then ordain alphabetically by name, to sort I use the interface comparator.
The problem comes when I will order 2 ArrayList(workers and bosses), because every time I call I use these functions or not is ordered (sortByNameAlphabetical())
public class Employee {
private String name;
public String getName() {
return name;
}
@Override
public String toString() {
return name ;
[Code]...
View Replies
View Related
Apr 7, 2015
I'm creating a card game assignment... so i have an arraylist called cards that has 20 cards and every card has contains 2 objects, suit and the point Value.
I shuffled the deck, now i want to add half of it to player 1 and the rest of the cards goes to the bot or computer.
how can i add the cards to the player one arraylist and have all the information of the cards?
here is my Deck class code :-
public class Deck {
private ArrayList cards;
private int size;
private ArrayList player1;
private ArrayList bot;
[code]....
the problem i have is this one doesn't work
size = cards.size() / 2;
for (int i = 0; i < size - 1; i++) {
player1.add(cards.get(i));
}
for (int s = size; s < cards.size() - 1; s++) {
bot.add(cards.get(s));
}
View Replies
View Related
Sep 18, 2014
Right now, i'm trying to do is when a user clicks on my GUI button, it would display a text of information on the button they selected. I'm using an arraylist of objects and i'm trying to input the function inside the mouse click function. But I'm not sure how to properly display that information. Here is my GUI code. I'm working on the raven button. I have animal interface, with a parent class of Bird, and child class of Raven. How I could display this correctly...
public class AnimalJF extends JFrame {
private JPanel contentPane;
private JTextArea textArea = new JTextArea();
/**
* Launch the application.
*/
public static ArrayList<Bird> Birdlist = new ArrayList<Bird>();
public static void main(String[] args) {
Birdlist.add(new Eagle());
[Code] .....
View Replies
View Related
Nov 21, 2014
I am currently working on a program that will read from a .txt file to create an arraylist. I will be working with the objects in the list to perform computations and display the data. The .txt file is in the following format: lastName firstName hoursWorked hourlyRate
With no delimiters. I've messed with this thing way too much in the last 12 hours (only day off for the week ) so variable names etc have changed often. At present the error I am receiving is "error: constructor GrossPayService in class GrossPayService cannot be applied to given types".
import java.util.*;
import java.io.*;
import java.text.*;
class AryListObjects
{
public static void addEmployee(String firstName, String lastName, double hoursWorked, double hourlyRate)
{
ArrayList<GrossPayService> employees = new ArrayList<GrossPayService>();
[code]....
View Replies
View Related
Dec 7, 2014
I've this "program" that shall manage to register a dog, show a list of all registerd dogs and delete dogs from the list.. And I'm stuck at the latter one. So I've to classes, one for the dog and one for register/program. This is my main program
package hundRegister;
import java.util.Scanner;
import java.util.ArrayList;
public class HundProgram {
private static Scanner tangentbord = new Scanner (System.in);
private static ArrayList<Hund> hundlista = new ArrayList<>();
[Code] ......
So, when I enter a name on a dog that exist on my list, it just jumps down to } else { and write that dog can't be found even if I write the exact name on the dog.
I can't see what I'm doing wrong, been trying out different methods now.
View Replies
View Related
Apr 3, 2014
I'm making a program for my class's chapter on classes. I need to make a program that makes a grocery list using an array. Here's the assignment from the book.
Write a class named GroceryList that represents a list of items to buy from the market, and another class named
GroceryItemOrder that represents a request to purchase a particular item in a given quantity (example: four boxes
of cookies). The GroceryList class should use an array field to store the grocery items and to keep track of its size
(number of items in the list so far). Assume that a grocery list will have no more than 10 items. A GroceryList
object should have the following methods:
public GroceryList()
Constructs a new empty grocery list.
public void add(GroceryItemOrder item)
Adds the given item order to this list if the list has fewer than 10 items.
public double getTotalCost()
Returns the total sum cost of all grocery item orders in this list.
The GroceryItemOrder class should store an item quantity and a price per unit. A GroceryItemOrder object
should have the following methods:
public GroceryItemOrder(String name, int quantity, double pricePerUnit)
Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price
per unit.
public double getCost()
Returns the total cost of this item in its given quantity. For example, four boxes of cookies that cost 2.30 per unit
have a total cost of 9.20.
public void setQuantity(int quantity)
Sets this grocery item’s quantity to be the given value.
I have the assignment mostly done, but the JUnit test case our teacher gave us to test it tests add() an array instead of an array list. For this reason, I need to change my program to use an array. My code is below.
import java.util.*;
public class GroceryList {
private ArrayList<GroceryItemOrder> list;
int num;
public GroceryList() {
list = new ArrayList<GroceryItemOrder>(10);
[Code] .....
View Replies
View Related