• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to print the Output

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below shown are the 3 classes, I developed. When I run ElementTest.java, I cannot see the output. I am unable to put it in debug also. I am using JBuilder tool for editing, compling & running the code. I am expecting it to print the error message
"Trying to add duplicate Element" which was coded in file Element.java
----------------- File Element.java --------------------------
package testexamples;
public class Element {
private String name;
public Element(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
----------------- File Set.java --------------------------
package testexamples;
import java.util.Vector;
public class Set {
private String name;
private Vector elements;
public Set(String name) {
this.name = name;
elements = new Vector();
}
public void addElement(Element el) {
if(! elements.contains( el )) {
elements.addElement(el);
}
else {
System.out.println(" Trying to add Duplicate Element:"+ el);
}
}
}
----------------- File ElementTest.java ----------------------
package testexamples;
import java.util.*;
import java.io.*;
public class ElementTest{
public ElementTest() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("log.txt");
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
PrintStream ps = new PrintStream(fos);
Element e1 = new Element("e1");
Element e2= new Element("e2");
Element e3= new Element("e3");
Element e4= new Element("e1");
Element e5= new Element("e2");
Set s1= new Set("s1");
Set s2= new Set("s2");
s1.addElement(e1);
s1.addElement(e2);
s1.addElement(e3);
s1.addElement(e4);
s1.addElement(e1);
s2.addElement(e1);
// s2.removeElement(e1);
s2.addElement(e1); // etc ...
// list of the elements in the set
ps.println("Here is the list of the elements");
ps.close();
}

public void main(String[] args) {
ElementTest elementTest = new ElementTest();
elementTest.invokedStandalone = true;
while(true);
}
private boolean invokedStandalone = false;
}
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhu
I've got your code to run using Sun's jdk though I had to make a few changes.
My general approach was to identify and resolve errors, removing layers of complexity where necessary. There were several errors and I'm not sure whether your IDE made it hard for you to see them clearly.
I suggest you build up your programs gradually, running and testing as you go. If find it best to prove to myself that a program is working at a simple level before I start adding another level of complexity. That way you don't compound errors and you see immediately if you go off track.
Enough advice. Here's my results. I hope they help you.
Compile errors

  • Set was being confused with java.util.Set in compilation, so I changed your Set to be Sett.
  • The compiler couldn't resolve Element and so, not being a great expert in packages I simply removed your package statements, which made all the classes part of the same default package.

  • Runtime errors

    • Added "static" to the declaration of main in ElementTest.java so java could run it.
    • Removed your infinite loop: while(true);

    • That's all I remember changing.
      Here's the output.
      log.txt contains:

      Here is the list of the elements


      Console output:

      Trying to add Duplicate Element:Element@4672d0
      Trying to add Duplicate Element:Element@4672d0


      Here's my modifed version of your source code.
      Element.java
      ------------

      Sett.java
      --------

      ElementTest.java
      ----------------

      Greg
 
Madhu Jannu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u Greg. I will consider u'r suggestions.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic