Writing / Reading To And From Vector In JAVA

Nov 20, 2014

I have a project where I am required to read and write a vector of bank account objects and I am struggling with this concept. I was able to figure it out when the accounts were not stored in a vector but now that I am dealing with vectorsThis is my best attempt. Even though I know it's wrong, what I am trying to do.write/read methods in main:

public static void readTrans()
{
textArea.setText("");
chooseFile(1);
try
{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
for (int index=0; index != fileIndex; index++)

[code]....

View Replies


ADVERTISEMENT

Reading And Writing Files Into Java?

Apr 17, 2015

have to create a file named Lab13.txt. In the file I have 10 random numbers. I have to import the 10 numbers and have to Multiply all the numbers from Lab13.txt by 10 and save all the new numbers a new file named Lab13_scale.txt. so if the number 10 is in lab13.txt it prints 100 to Lab13_scale.txt. how do i get it to Multiply Here is what I have:

Java Code: import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

[Code].....

View Replies View Related

Reading And Writing Wav Files

May 17, 2014

I have copied a wav file as explained in [URL] ..... When trying to play my new file, it runs but no sound is hear. My reading and writing class is as explained in another post in the site:

This is the function that reads the file:

// read a wav file into this class
public boolean read(DataInputStream inFile) {
myData = null;
byte[] tmpInt = new byte[4];
byte[] tmpShort = new byte[2];

[Code] ....

I can't hear the copied wav file, but still the wav file is correctly copied...

View Replies View Related

File Reading / Writing

Sep 9, 2014

I've looked at multiple sources and everyone is saying different stuff. Which one should I be using? FileWriter/FileReader, other people was saying PrintWriter, and one even said : "Formatter" which is the one I'm doubting mostly. My purposes for writing files is for like saving maps, saving high scores, etc.

View Replies View Related

Reading And Writing To A File

Jan 10, 2014

I have accountsData.txt file in my default package, this is the file it should be writing to and reading from.

There is an ATM class, which has the main in it. From here the program is supposed to work like an atm, you type in your account number and can withdraw or deposit. It is supposed to then write the changes to the file.

//Read from a file.
public void loadAccounts(String inputFileName) throws IOException
{
Scanner fin = new Scanner(new File(inputFileName));
// Variables to store the values.[/color]
int account = 0;
double balance = 0.0;

[Code] .....

View Replies View Related

Reading And Writing Files

Nov 11, 2014

public class Tester
{
public static void main(String[] args)
{
Product p1 = new Product();
p1.loadFromFile("Monitor.pr");
System.out.println("Actual: " + p1.toString());
System.out.println("Expected: Product [id=12345, name=Monitor, description=A freakin great monitor!]");

p1.setId(11111);
p1.setName("another product");
p1.setDescription("Description of another product!");

[code]....

The expected output is what my program should be doing and the actual output is what it's doing instead. As you can see, my code works for the middle test. But I cannot understand why it won't load the first fileFor the second half, the id and the quantity are separated by a comma... I'm not really sure how to deal with that. As you can see, I tried using a delimiter, but it doesn't seem to be doing any good.

View Replies View Related

Reading / Sorting And Writing A File

Dec 4, 2014

I need to read lastname, firstname, and ssn from a text file and sort them based on oneof those three creiteria. Here is my person class:

public class Person implements Comparable {
private String lastName, firstName, ssn;
private static int sortBy = 1;
public static final int LASTNAME=1, FIRSTNAME=2, SSN=3;
public void setSortMethod(int method) {
if(method != LASTNAME && method != FIRSTNAME && method != SSN) {
throw new IllegalArgumentException();
} else {

[code]....

how to sort my info and store it into an array and then write that info into my new file.

View Replies View Related

I/O / Streams :: Reading From And Writing To A File Then Read Again Including Data Written

Oct 11, 2014

I'm having a bit of trouble with using the Scanner and the Printwriter. I start with a file like this (1 = amount of Houses in the file)

1
FOR SALE:
Emmalaan 23
3051JC Rotterdam
7 rooms
buyprice 300000
energylevel C

The user gets (let's say for simplicity) 3 options:

1. Add a House to the file,
2. Get all Houses which fullfil requirements (price, FOR SALE / SOLD etc.) and
3. Close the application.

This is how I start:

Scanner sc = new Scanner (System.in);
while (!endLoop) {
System.out.println("Make a choice);
System.out.println("1) Add House");
System.out.println("2) Show Houses");
System.out.println("3) Exit");
int choice = sc.nextInt();

Then I have a switch for all of the three cases. I keep the scanner open, so Java can get the user input (house = for sale or sold, price = ... etc). If the user chose option 1, and all information needed is inputted and scanned, the House will be written to the file (which looks like what I typed above).

For this, I use try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))). This works perfectly (at least so it seems.)

If the user chose option 1, and all requirements are inputted and scanned, the Houses will be read (scanner) from the file and outputted. For this I use the same Scanner sc. This also works perfectly (so it seems atleast).

My problem is as follows: If a House has been added, I can only read the House(s) which were already in the file. Let's say I have added 2 houses, and there were from the start 3 houses. If option 2 is chosen, the first 3 houses will be scanned perfectly. An exception will be caught for the remaining 2 (just added) Houses. How can I solve this? I tried to close the Scanner, and reopening it, but apparently Java doesn't agree with this

View Replies View Related

Vector Pair In Java

Mar 29, 2015

I want to convert this following piece of c++ code into java...but i cannot find suitable substitute for vector pair...what shall i do?

#include <cmath>
#include <cstdio>
#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>
using namespace std;
bool compare_polar (int x1, int y1, int x2, int y2) { // if p2 > p1 return true

[Code] .....

View Replies View Related

Writing Java From Pseudocode

Nov 26, 2014

for x = 0 to 9
set stars = "*"
set count = 0
while count < x
stars = stars + "*"
count = count + 1
endwhile
display stars
endfor

I need to change this pseudocode to Java, but I'm new, been trying for ages but can't get it right.

View Replies View Related

Writing A Java Program To PRINT?

Oct 19, 2014

how to print from bluej onto actual paper? how to do the formatting and configuration etc.

View Replies View Related

Writing Own Custom Comparator In Java

Jul 27, 2014

I have a java code that should sort an array of names based on the last name. e.g jane a, jane b, jane z, jane d should be jane a, jane b, jane d, jane z. I have the following code but for some reasons, the s1 in the comparator method is always null.
 
public class ShuffleName {
public static void sortNames(String[] names){
 Arrays.sort( names, new Comparator<String>() {
 public int compare( String s1, String s2 ) { 
String s1last = s1.split("s+")[1];
String s2last = s2.split("s+")[1];
return s1last.compareTo(s2last);

[code]....

View Replies View Related

Looping Through Objects Vector

Nov 11, 2014

I'm having some issues, trying to solve this problem in java. I want to print some election results, and i have to loop through a vector of objects and retrieve the partial sums of each party's seats for each constituency and the national results for each party. For now i can print the results per contituency, but i'm having problems in getting the national results. Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.

This is what i have.

Java Code:

while (i < h.geral.size()) {
show += "Constituency - "
+ ((Party) h.geral.elementAt(i)).getConstituency() + "

[Code] .....

View Replies View Related

Writing Print All Animals Method In Java

Aug 5, 2014

I am currently working on a java project, this involves me writing some code for a project, below are my attempts at coding so far:


/**
* Prints out details of all animal in the zoo.
*
*/
public void printAllAnimals() {
System.out.println("
Details for all animals in Zoo " + zooId);
System.out.println( "==================================");

[code]....

I currently cannot get the printallanimals() method to work as it should when executing the method printallanimals it just opens a filedialog box, when it is suppose to use the Collection object c,so that animals stored in the zoo can easily be checked.

View Replies View Related

File Handling In Java - Writing Objects In CSV

Dec 20, 2014

I'm doing this assignment in which i have to write some products in csv file...but as u can see in csv file two products are same "Cooking Oil"..so any method that can add two same product's quantity and their amount and write them in file

import java.util.*;
import java.io.*;
public class SalesbyTransactionMonth{
private static final String fileName = "Data.txt";
private static final String fileName1 = "sales_by_trans_month.csv";
String line = "";

[Code] ....

Attached image(s)

View Replies View Related

Writing Gauss Elimination In Java Code

May 2, 2014

I have a big problem with writing Gauss Elimination in Java code. This is my homework, it's true. But I want to learn this, and I don't know how to bite this. I must realise some points:

1) Create new class - Gauss Elimination with two attributes (table A (nxn) and table B with vector (n));

2) Add new method "eliminate" which transform matrix A and parametr vectors B ;

3) Add new method "solve" which calculate inverse substitution.

View Replies View Related

Swing/AWT/SWT :: Vector Is Used In ComboBox Model?

Sep 23, 2014

I understand how vectors work I'm currently using one to store my id's from my txt file but how do you put them in a defaultComboBoxModel?

DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel();
//declare a vector object that will hold a string (only thing that works with comboboxmodel
Vector<String> myVector=new Vector<String>();
//try statement
try{
FileReader fr = new FileReader(file);

[Code]...

Any example of how a vector is used with a defaultComboBoxModel so I can then use that to populate my JComboBox?

View Replies View Related

Accessing Vector Variables With Scanner?

Jan 26, 2015

When I input "the" or "and" I always receive "its not here" as the output. Why is this? I also tried printing the values of v.get(i).other and the system printed null twice.

public class Translator {
public String other;
public Translator(String x) {
x = other;

[Code].....

View Replies View Related

Converting From A Vector Containing Big Decimal To A Float

Mar 13, 2015

I'm having extreme difficulty in working with a Vector storing a column with a BigDecimal value, and converting that single value into a float. I'm not sure why the code was written this way, but basically, I'm working with something called a vector that has a single Big Decimal value/column (not sure what the correct terminology is), and I want to store that value in a float variable called "dp". However, I don't know how to convert from the Big Decimal to a float.

Code is below:

String s = "";
sql = "SELECT DiscountPercentRate FROM Attendees WHERE AttendeeId=" + attendeeId;
  Vector v2 = sqldb.getResults(sql); /*I know that sqldb.getResults(sql) returns a vector with a single BigDecimal column of 15.0, in the test example I'm using*/
if (!v2.isEmpty()) {
Vector data2 = (Vector)v2.elementAt(0);
if (!data2.isEmpty())

[Code] .....

In case you're wondering what the sqldb.getResults() method looks, like, here's a snippet of it - There's an else statement that triggers in my case, adding a BigDecimal column to a vector, and returns that vector.

Vector v = new Vector();
...
else {
BigDecimal bd = rs.getBigDecimal(i);
vCols.add(bd);
}
v.add(vCols);
...
return v;

How can I take the single result of my SQL query in that Vector/Big Decimal thing, and turn that result into a float value?

View Replies View Related

Difference Between ArrayList And Vector Class

Jun 26, 2014

What is the difference between ArrayList and Vector class

View Replies View Related

How To Create Insertion Sort Method For A Vector

Jun 9, 2014

im trying to create an insertion sort method for a vector. I know how to insertionsort for an array, but for a vector im having problems

Source code:
PHP Code: package test;
import java.util.*;
import java.io.*;
public class LinearSearch {
public static void main (String[] args) {
Vector myVector = new Vector();

[Code]...

I'm getting errors at lines 38 and 39 "Left-hand side of an assignment must be a variable". "Syntax-error(s) on token(s) misplaces contructor(s)". How can i fix them ??

View Replies View Related

Create Mathematical Vector Of D Dimension Initialized At 0

Nov 14, 2014

class GVector {
// TODO: declare a private array to save the vector coordinates 
// Creates a mathematical vector of d dimensions, initialized at 0
public GVector(int d) {
// TODO: implementation

[Code] ....

I'm confused with what type of array I need to use to save the vector coordinates and what to put in Gvector. Is it a constructor?

View Replies View Related

Calculate Time For Loan Payment - Writing Java From Pseudocode

Nov 26, 2014

calculate how long it would take to pay off a loan of 500 pounds if there was 10% interest monthly, and 100 pounds was paid each month. This code is in a pseudocode,

set months = 1
set balance = 500
set totalpaid = 0;
while balance > 100
balance = balance - 100;
set interest = balance * 0.1
balance = balance + interest
totalpaid = totalpaid + 100
months = months + 1
endwhile
totalpaid = totalpaid+balance
display "you paid "+totalpaid
display "It took you "+months+" months"

View Replies View Related

Random Color Ball And Move Bottom Bar Vector?

Feb 20, 2014

I created this simple vector java program where ball falling and when collide with the bar below, the ball stops.

I need to make the ball falling randomly from different direction and also need to make the bar move left right using keyboard arrow.

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
 //using timer to increase the counter

[code].....

View Replies View Related

Java Heap Memory Error While Writing Large Data To Excel

Mar 6, 2014

i have to write more than 100000 rows in a excel sheet (file size more than 20 MB) via java.

when i use XSSF, i am getting below Error.

java.lang.OutOfMemoryError: Java heap space
at org.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1592)
at org.apache.xmlbeans.impl.store.Saver$TextSaver.preEmit(Saver.java:1223)
at org.apache.xmlbeans.impl.store.Saver$TextSaver.emit(Saver.java:1144)

[Code]....

when i use HSSF , i am getting the below Error.
java.lang.OutOfMemoryError: Java heap space

I have tried increasing the java heap size , by giving upto -Xms1500m -Xmx2048m

View Replies View Related

Writing Output Of Two Different Java Class Files To One Txt File While Program Runs

Jul 20, 2013

So I am trying to write the output of two different java class files to one txt file while the program runs.  The file name is determined before the program is ran, through the command prompt as arguments.  How can I get the second class file to edit the same txt file without running into compile errors.
 
For right now I'm just going to send everything that the second file outputs to a message String variable, so that the Main class outputs to the the text file.  I still want to learn how to write to the same text file directly from the second class file.
 
import java.io.*;
public class Test{
    public static void main(String[] args) throws IOException{
        int x;
        //create a new file internally called doc.  But externally labelled the user input
        File doc = new File(args[0]);
        if (doc.exists()){

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved