The Artima Developer Community
Sponsored Link

Java Answers Forum
updating vectors using Strings/Int

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
JUn

Posts: 1
Nickname: gujunaid23
Registered: Jun, 2003

updating vectors using Strings/Int Posted: Jun 27, 2003 12:27 PM
Reply to this message Reply
Advertisement
I have the following code below. The input is from a text file called test.txt.
INPUT:
@0001 Variable1 3.
@0004 variable2 $4.
@0008 Variable3 mmddyy10.
@0015 Variable4 4.

Column1 Value of Current + Column3 Value of Previous gives you the next row's Column1 Value, and this must continue as each value can be updated within the vector.

I need to manipulate the output such that: The value of each subsequent gives you the next.(Looking at the input will make more sense). Each value in the vector is stored in a String. I am able to take the values out of the String, but unable to manipulate to get the next. This source code is able to add a value to the vector, but unable to update it.

import java.lang.*;
import java.io.*;
import java.util.*;

public class footest
{
String objectFile = "object.txt";

Vector lineData = new Vector();

String currVarSize = "";
String inputVarFnd ="";
String varName = "";
String inputVar;
String inputVarSz;
String temp1= "";
int currColSize;
int nextColSize;

boolean Found;

int index;




public footest()
{
readFile();
writeObject();
findAdd();
CalculateCols();
writeFile();
}

public void readFile()
{
System.out.println("");
System.out.println(">> Loading data from file into vector");
try
{
StringTokenizer words;
String line;
String word;

// prepare file for input
BufferedReader objIn =
new BufferedReader(new BufferedReader(new FileReader("test.txt")));


System.out.println("The input is : ");

// prepare to extract words from a line
// process lines until 'null' (no more input)
while ( (line = objIn.readLine()) != null)
{
//tokenizing line
StringTokenizer st = new StringTokenizer(line);

//Initiate size and name of input.
currColSize = (int)Double.parseDouble(st.nextToken().substring(1));
varName = st.nextToken();
currVarSize = st.nextToken();

//Add each value to a vector as a String
String fooValue = Integer.toString(currColSize);
lineData.add(fooValue);
lineData.add(varName);
l ineData.add(currVarSize);


//Conversion to integer values of each Variable Size
int temp;
//If Character
if (currVarSize.startsWith("$"))
{
temp = (int)Double.parseDouble(currVarSize.substring(1));
}
//If DateW (W is any integer)
else if (currVarSize.startsWith("mmddyy"))
{
temp = (int)Double.parseDouble(currVarSize.substring(6));
}
//If Integer
else
{
temp = (int)Double.parseDouble (currVarSize);
}
nextColSize = currColSize + temp;
currColSize = nextColSize;
System.out.println("@" +currColSize +" " +varName +" " +currVarSize);

}
objIn.close();

}

catch (IOException e)
{
System.out.println(">> error while accessing: test.txt");
}
}

public void writeObject()
{
System.out.println("");
System.out.println(">> Writing vector object to file ");
System.out.println(lineData.toString());
try
{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(objectFile));
os.writeObject(lineData);
os.close();
}
catch (IOException e)
{
System.out.println(">> Error while accessing test.txt");
}
}

public void findAdd()
{
System.out.println("Current Vector size " +lineData.size());
System.out.println("Input Variable Name to find :");
System.out.println("");
String DfltSize = "0";
try
{
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
inputVarFnd = stdIn.readLine();
if(lineData.contains(inputVarFnd))
{
Found = true;
index = lineData.indexOf(inputVarFnd);
System.out.println("Input located at: " +index);
System.out.println("Input Variable name to add :");
System.out.println("");
inputVar = stdIn.readLine();
lineData.add(index+3, inputVar);
System.out.println("Input Variable size to add :");
System.out.println("");
inputVarSz = stdIn.readLine();
lineData.add(index+4, inputVarSz);
lineData.add(index+5, DfltSize);
System.out.println("");
System.out.println("New size : " +lineData.size());
}
else
{
System.out.println("Input Not Found! Please input the correct Variable Name");
Found = false;
findAdd();
}

}
catch (IOException e)
{
System.out.println(">> Error upon entering input. Please try again.");
}
}

public void CalculateCols()
{
//Conversion to integer values of each Variable Size
int temp;
//If Character
if (currVarSize.startsWith("$"))
{
temp = (int)Double.parseDouble(currVarSize.substring(1));
}
//If DateW (W is any integer)
else if (currVarSize.startsWith("mmddyy"))
{
temp = (int)Double.parseDouble(currVarSize.substring(6));
}
//If Integer
else
{
temp = (int)Double.parseDouble (currVarSize);
}
System.out.println("@" +currColSize +" " +varName +" " +currVarSize);



// nextColSize = currColSize + temp;
// currColSize = nextColSize;
writeObject();
System.out.println("The next Column size should be : @" +currColSize);
}

public void writeFile()
{
System.out.println("");
System.out.println(">> Writing to File");

try
{
BufferedWriter bw = new BufferedWriter(new FileWriter("data.txt"));
for(int i = 3; i < lineData.size(); i = i+3)
{
bw.write("@" +lineData.elementAt(i) +" " +lineData.elementAt(i+1) +" " +lineData.elementAt(i+2));
bw.newLine();
}
bw.close();
System.out.println("DATA EDITING COMPLETE!!");
}
catch (IOException f)
{
System.out.println(">> Error while accessing "+ "data.txt");
}
}


public static void main(String[] args)
{
footest Junaid = new footest();
}

}

Topic: jtable and object data [][] Previous Topic   Next Topic Topic: plz help me

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use