Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2004, 04:44 PM
Registered User
 
Join Date: Jun 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transfering elements from a vector to another

hi all,
I am trying to make a program called "Duplicated" which reads thousand of strings(barcodes), then it checks whether a barcode is written more than once or not,
if yes the programm should print out the list of all barcodes which occurences it is more than once.

Tecnicaly, since I don't know exactly how many are the barcodes I use a vector(it's called "primo") instead of an array, then I start to a nested cycle to see the
occurences of a single barcode if it is more than once it will store it in another vector called "secondo". At the end the programm should print out all the elements of
the vector "secondo".

Well, the strange thing, it is that the program reads the barcodes, then store them into "primo" but it's not able to store into "secondo".
Why???




Thanks in advance

P.S. I used also a file already made to help me for semplify the syntax of the streaming code(it is called Text.java). Please before running the file Duplicated.class, create a file called barcode.txt in which just made up some barcodes such as: "FOD 4533" and so forth.

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

public class Duplicated{
public static void main(String[]args) throws IOException{


Vector primo=new Vector();
Vector secondo=new Vector();


BufferedReader fin=Text.open("barcode.txt");


try {
for( ; ; ){
String number=(Text.readString(fin));
  primo.add(number);

}
}

catch(EOFException e){




System.out.println("Ok strings imported !");


for(int i=0; i<primo.size(); i++)
System.out.println((String)primo.get(i));
}


// **compare whether the elements of primo are duplicated if yes store it into secondo

// from the instruction below I can see that the initial size as expected is zero
System.out.println(" the size of the vector secondo is: "+secondo.size());

     for(int i=0; i<primo.size(); i++){
      int a=0;
      for(int j=0; j<primo.size(); j++){
      if ((String)primo.get(i)==(String)primo.get(j))
           {a++; }}
      if (a>1)
          {secondo.add((String)primo.get(i));}
        }

// now print out all the duplicated barcodes

//I find out that the size of the vector secondo is still zero
System.out.println(" the size of the vector secondo is still : "+secondo.size());
for(int i=0; i<secondo.size(); i++)
  System.out.println((String)secondo.get(i));
//**
  }
}

----------------------------------------------------------------------


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

public class Text {

  public Text () {};

  /* The All New Famous Text class by J M Bishop Aug 1996
   * revised for Java 1.1 by Alwyn Moolman Aug 1997
   * revised for efficiency by J M Bishop Dec 1997
   *
   * Provides simple input from the keyboard and files.
   * Now also has simple output formatting methods
   * and file opening facilities.
   *
   * public static void prompt (String s)
   * public static int readInt (BufferedReader in)
   * public static double readDouble (BufferedReader in)
   * public static String readString (BufferedReader in)
   * public static char readChar (BufferedReader in)
   * public static String writeInt (int number, int align)
   * public static String writeDouble
                   (double number, int align, int frac)
   * public static BufferedReader open (InputStream in)
   * public static BufferedReader open (String filename)
   * public static PrintWriter create (String filename)
   *
   * January 2001
   * Now local to the javagently package as it has been
   * superceded for program development by the Stream class.
   * It is still needed by Graph and Display.
   */

  private static StringTokenizer T;
  private static String S;

  public static BufferedReader open (InputStream in) {
    return new BufferedReader(new InputStreamReader(in));
  }

  public static BufferedReader open (String filename)
    throws FileNotFoundException {
    return new BufferedReader (new FileReader (filename));
  }

  public static PrintWriter create
     (String filename) throws IOException {
    return new PrintWriter (new FileWriter (filename));
  }

  public static void prompt (String s) {
    System.out.print(s + " ");
    System.out.flush();
  }

  public static int readInt (BufferedReader in) throws IOException {
      if (T==null) refresh(in);
      while (true) {
        try {
          return Integer.parseInt(T.nextToken());
        }
        catch (NoSuchElementException e1) {
          refresh (in);
        }
        catch (NumberFormatException e2) {
          System.out.println("Error in number, try again.");
        }
      }
   }

 public static char readChar (BufferedReader in) throws IOException {
      if (T==null) refresh(in);
      while (true) {
        try {
          return T.nextToken().trim().charAt(0);
        }
        catch (NoSuchElementException e1) {
          refresh (in);
        }
      }
   }

 public static double readDouble (BufferedReader in) throws IOException {
      if (T==null) refresh(in);
      while (true) {
        try {
          String item = T.nextToken();
          return Double.valueOf(item.trim()).doubleValue();
        }
        catch (NoSuchElementException e1) {
          refresh (in);
        }
        catch (NumberFormatException e2) {
          System.out.println("Error in number, try again.");
        }
      }
   }

  public static String readString (BufferedReader in) throws IOException {
    if (T==null) refresh (in);
    while (true) {
      try {
        return T.nextToken();
      }
      catch (NoSuchElementException e1) {
        refresh (in);
      }
    }
  }

  private static void refresh (BufferedReader in) throws IOException {
    S = in.readLine ();
    if (S==null) throw new EOFException();
    T = new StringTokenizer (S);
  }

  // Write methods
  // -------------

  private static DecimalFormat N = new DecimalFormat();
  private static final String spaces = " ";

  public static String writeDouble (double number, int align, int frac) {
    N.setGroupingUsed(false);
    N.setMaximumFractionDigits(frac);
    N.setMinimumFractionDigits(frac);
    String num = N.format(number);
    if (num.length() < align)
      num = spaces.substring(0,align-num.length()) + num;
    return num;
  }

  public static String writeInt (int number, int align) {
    N.setGroupingUsed(false);
    N.setMaximumFractionDigits(0);
    String num = N.format(number);
    if (num.length() < align)
      num = spaces.substring(0,align-num.length()) + num;
    return num;

  }
}







Similar Threads
Thread Thread Starter Forum Replies Last Post
Transfering enteries in the same form scandalous Access 3 February 22nd, 2007 11:15 AM
Transfering an entry scandalous Access 1 February 13th, 2007 08:36 AM
Transfering report Hudson40 Access VBA 1 March 11th, 2005 11:00 AM
Transfering data from one machine to another..... nikosdra SQL Server 2000 0 September 6th, 2003 05:08 AM
Transfering Import Specs Morris Access 3 August 15th, 2003 08:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.