|
|
I think you should really start learning the basics. This is really really basic java programming. You pass the variable in the constructor but you only store it in a local variable in your constructor. Which means that it is not visible outside that method. You have to make it a member variable of your class in order for it to be accessible in other methods.
Please use the code block tag when posting code, because it is very difficult to read otherwise.
|
|
|
|
|
Thanks Cedric, that brought the final solution for me.
And yes, my basics seem to be thin.
I only wonder why I don't get such points when I read my books about java:
- Go To Java 2, 2. Auflage . Handbuch der Java-Programmierung von Guido Krüger von Addison-Wesley
- JAVA als erste Programmiersprache. - vom Einsteiger zum Profi von Joachim Goll, Cornelia Weiß, und Frank Müller von B.G. Teubner Verlag
Finding a usefull book is not such easy.
|
|
|
|
|
Hi! I created a page that has a "Back Button", it opens up as a pop up window. The Back Button doesn't work IE but id does in FireFox Mozilla.
I have tried with window.history.go and window.history.back but non of them is working. Any Suggestion please ?
|
|
|
|
|
Wrong forum. This is not a Java question. You will have more luck with this if you post it in the Web Development forum.
|
|
|
|
|
The fact thats it an AVL tree probably doesn't matter. I have a printTree method in my tree class and I'm trying to print the nodes in the form of the tree. I've successfully printed the nodes the correct way but now I'm trying to label the nodes by their relative height (i.e. equalheight, right hi, left hi).
The problem is my root, which is right hi, won't change to that. Everything else is working for every node except the root, but the counter I made that determines the label is the right number. I can't for the life of me figure out why it is staying at equal height.
The first thing is the label, then the node, and the end number is the count.
EDIT: I know you can't tell because the tabs got taken out but alstoto01 is the root, thus the first node to go through the methods.
This is what I'm outputting right now (the Wrong way):
EQUALHT:aaronha01: 0
EQUALHT:agganha01: 0
EQUALHT:alliega01: 0
EQUALHT:alstoto01: -1
EQUALHT:bollifr01: 0
EQUALHT:cronera01: 0
EQUALHT:grammal01: 0
EQUALHT:henlega01: 0
RIGHTHI:herrito01: -1
EQUALHT:jacobsp01: 0
The Correct output should be:
EQUALHT:aaronha01: 0
EQUALHT:agganha01: 0
EQUALHT:alliega01: 0
RIGHTHI:alstoto01: -1
EQUALHT:bollifr01: 0
EQUALHT:cronera01: 0
EQUALHT:grammal01: 0
EQUALHT:henlega01: 0
RIGHTHI:herrito01: -1
EQUALHT:jacobsp01: 0
Here is the code for the printTree method:
private ArrayList<AnyType> printTree( AvlNode<AnyType> t, String buffer )
{
if( t != null )
{
int count = counter(t);
if(count == 0)
freq = "EQUALHT:";
if(count < 0)
freq = "RIGHTHI:";
else if(count > 0)
freq = "LEFTHI:";
printTree( t.left, buffer + " ");
pTree.add((AnyType) (buffer + freq + t.element + ":") );
System.out.println(buffer + freq + t.element + ":" + count);
printTree( t.right, buffer + " ");
return pTree;
}
return null;
}
Here is the code for my counter method:
private int counter(AvlNode<AnyType> t){
int count = 0, left = 0, right = 0;
AvlNode<AnyType> temp = t;
while(temp != null){
temp = temp.right;
right++;
}
while(t != null){
t = t.left;
left++;
}
count = left - right;
return count;
}
It's probably a simple fix but I just can't see it. Any help would be appreciated since this is a really stupid problem to have in the grand scheme of things.
|
|
|
|
|
Never mind, it was a simple fix. Just needed to bring my freq variable down into the method.
private ArrayList printTree( AvlNode t, String buffer )
{
if( t != null )
{
int count = counter(t);
String freq = "";
if(count == 0)
freq = "EQUALHT:";
if(count < 0)
freq = "RIGHTHI:";
else if(count > 0)
freq = "LEFTHI:";
printTree( t.left, buffer + " ");
pTree.add((AnyType) (buffer + freq + t.element + ":") );
System.out.println(buffer + freq + t.element + ":" + count);
printTree( t.right, buffer + " ");
return pTree;
}
return null;
}
|
|
|
|
|
You get an up tick for showing us your answer and not doing just deleting the question. Have a
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Can anyone get this to work?
Goal: This program will implement Bakhshali Approximation for finding the square root of a number. The algorithm to approximate the square root of N is
1. Start with the initial approximation of 1.
2. As long as the approximation isn’t close enough, use a while loop to repeatedly refine the approximation using the following formulas
d = N – (initial approximation)^2
P = d/(2 * initial approximation)
A = initial approximation + P
new approximation = A - P^2/(2*A)
(What does “close enough” mean? For your program, you’ll take close enough to mean that )
∣newApproximation-oldApproximation∣<. 0001∗N)
Other features:
If the user types a negative number, he/she should be alerted that only nonnegative numbers are acceptable.
Your program should correctly compute the square root of 0, which is 0.
There should be a Clear button to clear both the text box and textarea.
Any help with be greatly appreciated!!
|
|
|
|
|
silentbob0321 wrote: 1. Start with the initial approximation of 1.
2. As long as the approximation isn’t close enough, use a while loop to repeatedly refine the approximation using the following formulas
d = N – (initial approximation)^2
P = d/(2 * initial approximation)
A = initial approximation + P
new approximation = A - P^2/(2*A)
Contains the answer to your homework. We don't do homework, we'll help but not just do it for you.
This is basic for any language, if you cannot do it either your tutor is stupid for not being able to explain the concepts or you are for not understanding. The only other advice I can give you is to practice the phrase "You want fries with that?"; you'll need it when you start working.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
silentbob0321 wrote: Can anyone get this to work?
Yes, but since this is your homework it is up to you to at least try. You have even been given the algorithm for your solution so it's just a simple matter of writing the java statements to implement that algorithm.
|
|
|
|
|
Hello all,
I have 5 textareas one below the another. They are packed using the GridBagLayout (Doing all this in NetBeans 6.5). When only one line text is fetched in the textarea everything is fine, but the moment the text fetched is of 2 or more lines, all the textareas size increases reducing the last panel's (below 5 textares) height.
This also happens with the combo box. when the items fetched are greater than the width of the combo its width increases and comes to normal if the items are of less width.
Anybody faced such problem ??? Plz. help.
|
|
|
|
|
You will probably need to develop a custom layout manager rather than using one of the pre-defined layout modes. Basically, you create a class that implements LayoutManager, providing an implementation for the layoutContainer method, among others. You then instantiate your manager and assign it to be the parent window's manager with setLayout.
Try plowing through the docs for LayoutManager and take a whack at it. I can help if you have trouble.
[edit]
Hint - If you make your custom layout manager an inner class of the parent window, it'll have access to the control variables representing the text areas and be able to manipulate their positions.
[/edit]
modified on Wednesday, December 2, 2009 1:02 PM
|
|
|
|
|
thanks ....
will get back to u, when i start working on that.
currently working on other higher priority issues.
thanks again..
|
|
|
|
|
Is it advanced concept?what is java effects?Where can i use it?Please give any url for java effects tutorial..
vinoli
|
|
|
|
|
Why not try this[^] first!
|
|
|
|
|
|
Hi,
I have created a web application in Struts 1. It has got a reporting tool module, for which i want to embed the features of Pentaho into it. I figured out that it is possible by adding some of the JAR files of Pentaho into my application folder and editing my web.xml file. I tried, but couldnt get it the way i wanted. Can anyone please help me with this? I am not using hibernate in my project.
Regards,
Vivek Vijayan
This code was posted by me...
|
|
|
|
|
This does not appear to be a Java question. Try the Struts or Pentaho forums instead.
|
|
|
|
|
Does anyone know what am i supposed to do to write xml attribute in java?
example
<body id="Content">Text</body>
|
|
|
|
|
i have got a code for Web Crawler in java language....
Following is applet code...
import java.applet.Applet;
import java.text.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.util.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class WebCrawler extends Applet implements ActionListener, Runnable {
public static final String SEARCH = "Search";
public static final String STOP = "Stop";
public static final String DISALLOW = "Disallow:";
public static final int SEARCH_LIMIT = 50;
Panel panelMain;
List listMatches;
Label labelStatus;
Vector vectorToSearch;
Vector vectorSearched;
Vector vectorMatches;
Thread searchThread;
TextField textURL;
Choice choiceType;
public void init() {
panelMain = new Panel();
panelMain.setLayout(new BorderLayout(5, 5));
Panel panelEntry = new Panel();
panelEntry.setLayout(new BorderLayout(5, 5));
Panel panelURL = new Panel();
panelURL.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
Label labelURL = new Label("Starting URL: ", Label.RIGHT);
panelURL.add(labelURL);
textURL = new TextField("", 40);
panelURL.add(textURL);
panelEntry.add("North", panelURL);
Panel panelType = new Panel();
panelType.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
Label labelType = new Label("Content type: ", Label.RIGHT);
panelType.add(labelType);
choiceType = new Choice();
choiceType.addItem("text/html");
choiceType.addItem("audio/basic");
choiceType.addItem("audio/au");
choiceType.addItem("audio/aiff");
choiceType.addItem("audio/wav");
choiceType.addItem("video/mpeg");
choiceType.addItem("video/x-avi");
panelType.add(choiceType);
panelEntry.add("South", panelType);
panelMain.add("North", panelEntry);
Panel panelListButtons = new Panel();
panelListButtons.setLayout(new BorderLayout(5, 5));
Panel panelList = new Panel();
panelList.setLayout(new BorderLayout(5, 5));
Label labelResults = new Label("Search results");
panelList.add("North", labelResults);
Panel panelListCurrent = new Panel();
panelListCurrent.setLayout(new BorderLayout(5, 5));
listMatches = new List(10);
panelListCurrent.add("North", listMatches);
labelStatus = new Label("");
panelListCurrent.add("South", labelStatus);
panelList.add("South", panelListCurrent);
panelListButtons.add("North", panelList);
Panel panelButtons = new Panel();
Button buttonSearch = new Button(SEARCH);
buttonSearch.addActionListener(this);
panelButtons.add(buttonSearch);
Button buttonStop = new Button(STOP);
buttonStop.addActionListener(this);
panelButtons.add(buttonStop);
panelListButtons.add("South", panelButtons);
panelMain.add("South", panelListButtons);
add(panelMain);
setVisible(true);
repaint();
vectorToSearch = new Vector();
vectorSearched = new Vector();
vectorMatches = new Vector();
URLConnection.setDefaultAllowUserInteraction(false);
}
public void start() {
}
public void stop() {
if (searchThread != null) {
setStatus("stopping...");
searchThread = null;
}
}
public void destroy() {
}
boolean robotSafe(URL url) {
String strHost = url.getHost();
String strRobot = "http://" + strHost + "/robots.txt";
URL urlRobot;
try {
urlRobot = new URL(strRobot);
} catch (MalformedURLException e) {
return false;
}
String strCommands;
try {
InputStream urlRobotStream = urlRobot.openStream();
byte b[] = new byte[1000];
int numRead = urlRobotStream.read(b);
strCommands = new String(b, 0, numRead);
while (numRead != -1) {
if (Thread.currentThread() != searchThread)
break;
numRead = urlRobotStream.read(b);
if (numRead != -1) {
String newCommands = new String(b, 0, numRead);
strCommands += newCommands;
}
}
urlRobotStream.close();
} catch (IOException e) {
return true;
}
String strURL = url.getFile();
int index = 0;
while ((index = strCommands.indexOf(DISALLOW, index)) != -1) {
index += DISALLOW.length();
String strPath = strCommands.substring(index);
StringTokenizer st = new StringTokenizer(strPath);
if (!st.hasMoreTokens())
break;
String strBadPath = st.nextToken();
if (strURL.indexOf(strBadPath) == 0)
return false;
}
return true;
}
public void paint(Graphics g) {
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
panelMain.paint(g);
panelMain.paintComponents(g);
}
public void run() {
String strURL = textURL.getText();
String strTargetType = choiceType.getSelectedItem();
int numberSearched = 0;
int numberFound = 0;
if (strURL.length() == 0) {
setStatus("ERROR: must enter a starting URL");
return;
}
vectorToSearch.removeAllElements();
vectorSearched.removeAllElements();
vectorMatches.removeAllElements();
listMatches.removeAll();
vectorToSearch.addElement(strURL);
while ((vectorToSearch.size() > 0)
&& (Thread.currentThread() == searchThread)) {
strURL = (String) vectorToSearch.elementAt(0);
setStatus("searching " + strURL);
URL url;
try {
url = new URL(strURL);
} catch (MalformedURLException e) {
setStatus("ERROR: invalid URL " + strURL);
break;
}
vectorToSearch.removeElementAt(0);
vectorSearched.addElement(strURL);
if (url.getProtocol().compareTo("http") != 0)
break;
if (!robotSafe(url))
break;
try {
URLConnection urlConnection = url.openConnection();
urlConnection.setAllowUserInteraction(false);
InputStream urlStream = url.openStream();
String type
= urlConnection.guessContentTypeFromStream(urlStream);
if (type == null)
break;
if (type.compareTo("text/html") != 0)
break;
byte b[] = new byte[1000];
int numRead = urlStream.read(b);
String content = new String(b, 0, numRead);
while (numRead != -1) {
if (Thread.currentThread() != searchThread)
break;
numRead = urlStream.read(b);
if (numRead != -1) {
String newContent = new String(b, 0, numRead);
content += newContent;
}
}
urlStream.close();
if (Thread.currentThread() != searchThread)
break;
String lowerCaseContent = content.toLowerCase();
int index = 0;
while ((index = lowerCaseContent.indexOf("<a", index)) != -1)
{
if ((index = lowerCaseContent.indexOf("href", index)) == -1)
break;
if ((index = lowerCaseContent.indexOf("=", index)) == -1)
break;
if (Thread.currentThread() != searchThread)
break;
index++;
String remaining = content.substring(index);
StringTokenizer st
= new StringTokenizer(remaining, "\t\n\r\">#");
String strLink = st.nextToken();
URL urlLink;
try {
urlLink = new URL(url, strLink);
strLink = urlLink.toString();
} catch (MalformedURLException e) {
setStatus("ERROR: bad URL " + strLink);
continue;
}
if (urlLink.getProtocol().compareTo("http") != 0)
break;
if (Thread.currentThread() != searchThread)
break;
try {
URLConnection urlLinkConnection
= urlLink.openConnection();
urlLinkConnection.setAllowUserInteraction(false);
InputStream linkStream = urlLink.openStream();
String strType
= urlLinkConnection.guessContentTypeFromStream(linkStream);
linkStream.close();
if (strType == null)
break;
if (strType.compareTo("text/html") == 0) {
if ((!vectorSearched.contains(strLink))
&& (!vectorToSearch.contains(strLink))) {
if (robotSafe(urlLink))
vectorToSearch.addElement(strLink);
}
}
if (strType.compareTo(strTargetType) == 0) {
if (vectorMatches.contains(strLink) == false) {
listMatches.add(strLink);
vectorMatches.addElement(strLink);
numberFound++;
if (numberFound >= SEARCH_LIMIT)
break;
}
}
} catch (IOException e) {
setStatus("ERROR: couldn't open URL " + strLink);
continue;
}
}
} catch (IOException e) {
setStatus("ERROR: couldn't open URL " + strURL);
break;
}
numberSearched++;
if (numberSearched >= SEARCH_LIMIT)
break;
}
if (numberSearched >= SEARCH_LIMIT || numberFound >= SEARCH_LIMIT)
setStatus("reached search limit of " + SEARCH_LIMIT);
else
setStatus("done");
searchThread = null;
}
void setStatus(String status) {
labelStatus.setText(status);
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.compareTo(SEARCH) == 0) {
setStatus("searching...");
if (searchThread == null) {
searchThread = new Thread(this);
}
searchThread.start();
}
else if (command.compareTo(STOP) == 0) {
stop();
}
}
public static void main (String argv[])
{
Frame f = new Frame("WebFrame");
WebCrawler applet = new WebCrawler();
f.add("Center", applet);
Properties props= new Properties(System.getProperties());
props.put("http.proxySet", "true");
props.put("http.proxyHost", "webcache-cup");
props.put("http.proxyPort", "8080");
Properties newprops = new Properties(props);
System.setProperties(newprops);
applet.init();
applet.start();
f.pack();
f.show();
}
}
But when i run it, and enter any URL it shows Following error in Cmd
Exception in thread "AWT-EventQueue-1" java.lang.IllegalThreadStateExcepti
at java.lang.Thread.start(Thread.java:571)
at WebCrawler.actionPerformed(WebCrawler.java:388)
at java.awt.Button.processActionEvent(Button.java:388)
at java.awt.Button.processEvent(Button.java:356)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDisp
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispat
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.jav
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.jav
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Please look at it...please tell what i need to do in it....
and what i need to edit in this Code of webcrawler
Properties props= new Properties(System.getProperties());
props.put("http.proxySet", "true");
props.put("http.proxyHost", "webcache-cup");
props.put("http.proxyPort", "8080");
Please help me out....
Please help.....
|
|
|
|
|
This is far too much code for anyone to read through. Please identifiy the line of code where the error occurs and post only the small amount of code near that line. Have you looked at the documentation to see what an IllegalThreadStateException is, and what may be its causes?
|
|
|
|
|
public static void main (String argv[])
{
Frame f = new Frame("WebFrame");
WebCrawler applet = new WebCrawler();
f.add("Center", applet);
Properties props= new Properties(System.getProperties());
props.put("http.proxySet", "true");
props.put("http.proxyHost", "webcache-cup");
props.put("http.proxyPort", "8080");
Properties newprops = new Properties(props);
System.setProperties(newprops);
applet.init();
applet.start();
f.pack();
f.show();
}
}
Ok ..just look at this MAIN
is there something needed to be done at
Properties prop=new Properties(System.getProperties());
Please compile it at your side ....and look at error..
there are some errors which i cannot explain...
please just once run it at your side...and put the inputs
and see what is that i am missing....
Please help me...
|
|
|
|
|
sangeeta2009 wrote: Please compile it at your side ....and look at error..
Well once I had commented out all the irrelevant code I could not see anything wrong; it compiles and runs. However, I don't see what this has to do with your original problem which as I recall, was some sort of ThreadException. My only suggestion is that you use whatever debug tools you have and try to step through your code. Assuming you wrote this code in the first place you should be able to extract small parts of it to test in isolation until you can at least narrow it down to a specific area of code.
|
|
|
|
|
Richard MacCutchan wrote: Assuming you wrote this code in the first place [...]
#wistles#
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|