11 Replies - 728 Views - Last Post: 08 October 2009 - 12:20 AM Rate Topic: -----

#1 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 22-January 09

rewriting a program to use a vector instead of an array

Post icon  Posted 07 October 2009 - 02:14 PM

This is my original code that i have to re-write to use a vector. I feel a little overwhelmed... i almost don't know what i'm doing.

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

public class CodeDetection_comment
{
	static final int maxCodeSize = 250;

	public static void main (String[] args) throws
										FileNotFoundException
	{
		int[] codeArray = new int[maxCodeSize];
		int	codeLength;


		Scanner codeFile = new Scanner
					  (new FileReader("SecretCode.txt"));

		PrintWriter outFile =
					new PrintWriter("SecretCodeOut.txt");

		codeLength = codeFile.nextInt();

		if (codeLength <= maxCodeSize)
		{
			readCode(codeFile, codeArray, codeLength);
			compareCode(codeFile, outFile, codeArray,
						codeLength);
		}
		else
		   System.out.println("Length of the secret code "
							+ "must be <= " + maxCodeSize);

		codeFile.close();
		outFile.close();
	}

	public static void readCode(Scanner inFile, int[] list,
								int length)
	{
		int count;

		for (count = 0; count < length; count++)
			list[count] = inFile.nextInt();
	}

	public static void compareCode(Scanner inFile,
								   PrintWriter outP,
								   int[] list, int length)
	{

		int length2;
		int digit;
		boolean codeOk;
		int count;

		codeOk = true;

		length2 = inFile.nextInt();

		if (length != length2)
		{
			System.out.println("The original code and "
 							 + "its copy are not of "
 							 + "the same length.");
	 		return;
		}

		outP.println("Code Digit   Code Digit "
				  +  "Copy");

		for (count = 0; count < length; count++)
		{
			digit = inFile.nextInt();

			outP.printf("%5d %15d",
						list[count], digit);

			if (digit != list[count])
			{
				outP.println("  corresponding code "
						   + "digits not the same");
				codeOk = false;
			}
			else
				outP.println();
		}

		if (codeOk)
			outP.println("Message transmitted OK.");
		else
			outP.println("Error in transmission. "
					  + "Retransmit!!");
	}
} 



And this is what i have so far(not much):


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

public class arrayhw
{
	static final int maxCodeSize = 250;

	public static void main (String[] args) throws
										FileNotFoundException
	{
		Vector<float> codeVector = new Vector<float>();
		int	codeLength;


		Scanner codeFile = new Scanner
					  (new FileReader("SecretCode.txt"));

		PrintWriter outFile =
					new PrintWriter("SecretCodeOut.txt");

		codeLength = codeFile.nextInt();

		if (codeLength <= maxCodeSize)
		{
			readCode(codeFile, codeVector, codeLength);
			compareCode(codeFile, outFile, codeArray,
						codeLength);
		}
		else
		   System.out.println("Length of the secret code "
							+ "must be <= " + maxCodeSize);

		codeFile.close();
		outFile.close();
	}

	public static void readCode(Scanner inFile, ,
								int length)
	{
		int count;

		for (count = 0; count < length; count++)
			list[count] = inFile.nextInt();
	}

	public static void compareCode(Scanner inFile,
								   PrintWriter outP,
								   int[] list, int length)
	{

		int length2;
		int digit;
		boolean codeOk;
		int count;

		codeOk = true;

		length2 = inFile.nextInt();

		if (length != length2)
		{
			System.out.println("The original code and "
 							 + "its copy are not of "
 							 + "the same length.");
	 		return;
		}

		outP.println("Code Digit   Code Digit "
				  +  "Copy");

		for (count = 0; count < length; count++)
		{
			digit = inFile.nextInt();

			outP.printf("%5d %15d",
						list[count], digit);

			if (digit != list[count])
			{
				outP.println("  corresponding code "
						   + "digits not the same");
				codeOk = false;
			}
			else
				outP.println();
		}

		if (codeOk)
			outP.println("Message transmitted OK.");
		else
			outP.println("Error in transmission. "
					  + "Retransmit!!");
	}
}




is this something that i can just change a few things around with? or am i going to have to rewrite a lot of this code?

the main things that are really hurting me are the method calls.... it uses some kind of array thing as parameters, and i'm just not really sure what that means or how i should replace it with a vector type thing...

sorry for posting so much code. any help would be immensely appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: rewriting a program to use a vector instead of an array

#2 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 22-January 09

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 02:45 PM

can anyone tell me at least if i'm on the right track?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

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

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 06:13 PM

View Postdeb4te, on 7 Oct, 2009 - 01:45 PM, said:

can anyone tell me at least if i'm on the right track?

if the array you want to replace is list[] you are on the wrong track
list is an array of int
and your codeVector is a Vector of float
Was This Post Helpful? 0
  • +
  • -

#4 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 22-January 09

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 09:14 PM

right, i haven't done anything to that area yet.. but i have to figure out what to replace as a parameter, one of them is left blank... i'm wondering if there's something i can use there as a parameter instead, or if i have to do something completely different?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

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

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 09:21 PM

View Postdeb4te, on 7 Oct, 2009 - 08:14 PM, said:

right, i haven't done anything to that area yet.. but i have to figure out what to replace as a parameter, one of them is left blank... i'm wondering if there's something i can use there as a parameter instead, or if i have to do something completely different?

just pass the Vector as parameter as you used to pass they array

public static void readCode(Scanner inFile, codeVector , int length)

by the way the lenght parameter might have been usefull for the array because the array was not full
fior the Vector no need for that the method can call vector.size() to know the nnumber of elements in the Vector
Was This Post Helpful? 1
  • +
  • -

#6 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 22-January 09

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 10:04 PM

this is my new code.... i think this is how i do it..


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

public class arrayhw
{
	static final int maxCodeSize = 250;

	public static void main (String[] args) throws
										FileNotFoundException
	{
		Vector<float> codeVector = new Vector<float>();
		int	codeLength;


		Scanner codeFile = new Scanner
					  (new FileReader("SecretCode.txt"));

		PrintWriter outFile =
					new PrintWriter("SecretCodeOut.txt");

		codeLength = codeFile.nextInt();

		if (codeLength <= maxCodeSize)
		{
			readCode(codeFile, codeVector, codeLength);
			compareCode(codeFile, outFile, codeVector,
						codeLength);
		}
		else
		   System.out.println("Length of the secret code "
							+ "must be <= " + maxCodeSize);

		codeFile.close();
		outFile.close();
	}

	public static void readCode(Scanner inFile, codeVector, int length)
	{
		int count;

		for (count = 0; count < length; count++)
			codeVector.elementAt(int count) = inFile.nextInt();
	}

	public static void compareCode(Scanner inFile,
								   PrintWriter outP,
								   codeVector, int length)
	{

		int length2;
		int digit;
		boolean codeOk;
		int count;

		codeOk = true;

		length2 = inFile.nextInt();

		if (length != length2)
		{
			System.out.println("The original code and "
 							 + "its copy are not of "
 							 + "the same length.");
	 		return;
		}

		outP.println("Code Digit   Code Digit "
				  +  "Copy");

		for (count = 0; count < length; count++)
		{
			digit = inFile.nextInt();

			outP.printf("%5d %15d",
						codeVector.elementAt(count), digit);

			if (digit != codeVector.elementAt(count))
			{
				outP.println("  corresponding code "
						   + "digits not the same");
				codeOk = false;
			}
			else
				outP.println();
		}

		if (codeOk)
			outP.println("Message transmitted OK.");
		else
			outP.println("Error in transmission. "
					  + "Retransmit!!");
	}
}
 


however i do get an <identifier> expected error, and it highlights this:
public static void readCode(Scanner inFile, codeVector, int length)


i'm not sure what i could be doing wrong?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 10:22 PM

You will have to identify the type of your parameter

public static void readCode(Scanner inFile, codeVector, int length)

public static void readCode(Scanner inFile, Vector<float> codeVector, int length)

but as I have already mentionned, you replaced an array of int by a Vector of float... not sure it is a good idea
should be a Vector of int
Was This Post Helpful? 1
  • +
  • -

#8 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 22-January 09

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 10:27 PM

unfortunately part of my assignment is to have it be of the float type... thank you though!!
Was This Post Helpful? 0
  • +
  • -

#9 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 22-January 09

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 11:11 PM

i have a feeling this isn't set up right...
i get a unexpected type error

public static void readCode(Scanner inFile, Vector<float> codeVector, int length)

This post has been edited by deb4te: 07 October 2009 - 11:26 PM

Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 11:43 PM

You need to use the Float class type, instead of the simple data type float. Vectors, like ArrayLists and other Collections in Java, can only hold Objects.
Was This Post Helpful? 1
  • +
  • -

#12 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 22-January 09

Re: rewriting a program to use a vector instead of an array

Posted 07 October 2009 - 11:49 PM

JUST got that as you posted... thank you so much... my main problem now is that i'm getting the same thing for this line

codeVector.elementAt(count) = inFile.nextInt();

Was This Post Helpful? 0
  • +
  • -

#13 deb4te  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 22-January 09

Re: rewriting a program to use a vector instead of an array

Posted 08 October 2009 - 12:20 AM

also... i changed the vector type to integer just to get this to work for now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1