javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

int i=123;
System.out.println(i[1]);
Any ideias?

'i' is an int, not an array:

int [] i = new int[2];
i[0] = 1;
i[1] = 2;
i[2] = 3;
System.out.println(i[1]);
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

javax.swing.JComboBox

When you add an Object to the Combo Box, the toString() method is called and it displays that at the GUI: void addItem(Object anObject) The method Object getSelectedItem() gets you the item you have selected.

So create an object with all the info from the DB including the primary key:

class Data {
  private long id = 0L; // assuming this is the PK
  private Sting name = null;
  private int age = 0;
  .....

  public String toString() {
      return name;
  }
}

Get the data from the DB, put each row in a Data object and put that object into a List (java.util.Vector).

Then add each of the objects from the List to Combo Box

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

i'm not familiar using that command...Could you give me some tricks of using it

In your code you have been using NumberFormat. It's the same thing. Have you looked at the link? Just read the API and use the methods. Experiment a little.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Sorry for my double post but when you are done with your solution, there is a much smarter - but more difficult - way you can try:

String [] words = {"Alpha", "Bravo", ....};

Now when you get each character you can use its ASCII equivalent, then normalize that value to be between 0 to 25 and use it as index. In that way you will not have to write entire code with if statements:

char ch = stringArray[i];
int index = // convert ch into an int and normalize
System.out.print(words[index]+" ");
_dragonwolf_ commented: Thank you greatly +1
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

This is totally unnecessary:

[B]stringArray.toString();[/B]
System.out.println("Alpha");

It doesn't do anything. You don't need it. And put the if statements in the for loop:

for(int i = 0; i < stringArray.length; i++) {
  char ch = stringArray[i];

  if  ((ch=='a')||(ch=='A')) {
     System.out.print("Alpha ");
  } else if ((ch=='b')||(ch=='B')) {

   } else if (...) {..
....
   } else {
      // IN CASE IT IS NOT A LETTER
     System.out.print(ch+" ");
   }
}

With switch:

for(int i = 0; i < stringArray.length; i++) {
  char ch = stringArray[i];
  
  switch (ch) {
     case 'a':
     case 'A':
       System.out.print("Alpha ");
       break;
     case 'b':
     case 'B':
       System.out.print("Bravo ");
       break;
     ....
....
...
    default:
       System.out.print(ch+" ");
  }
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Why don't you try:
DecimalFormat ?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
nf.format(calcTaxes (grossPay))

How do you define the "nf"?

I would suggest to write a simple program with only a double number and the Formatter, and try to print it the way you want. Once you accomplish that, transfer the Formatter to your main method

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

i am trying to learn servlets so i downloaded tomcat server.I learned that i have to do this...set path=c:\tomcat\lib\servlet.jar
or click the exe file tomcat6 in the bin folder. I tried to connect with localhost8080 it is not working.I am not getting Tomcat home page.

Any help will be great.

Two questions?
- Did you start the server?
- What url are you using?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

the implicite variable "this" is called even if it is omited:
so writing addMouseListener(this); issimilar to it's similar to this.addMouseListener(this);

I didn't understood you correctly, I thought you said this was an error

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

this instruction is ambigous: addMouseListener(this); it's similar to this.addMouseListener(this);

What do you mean by that? Do you think it is wrong?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

From what I see you use a lot of "extending" that I don't think is necessary. Your code works but it could have been simpler.
First don't use the "dispose" method.

Second you need to close the the MainPage and open the PageOne.
To do that you need to call the setVisible(false) on the MainPage that is open. In order to have access to the original MainPage that you opened you need to pass it as parameter to the MapLabel.
But MapLabel is called from the MainPagePanel so that also needs to have it as parameter:

MapLabel class

MainPage mp; 

public MapLabel(Icon icon, String name, MainPage mp) {

   this.mp = mp;
}

// AT THE LISTENER:

......
PageOne po = new PageOne();
mp.setVisible(false); // disposing PageOne

// [B]mp = new MainPage();[/B] // you don't need that. It will create a new MainPage and overwrite the existing one.
// you have already created a MainPage, don't create another

But in order to call this: public MapLabel(Icon icon, String name, MainPage mp) you need:

MainPagePanel class

MainPage mp;

public MainPagePanel(MainPage mp) {
		Toolkit kit = Toolkit.getDefaultToolkit();

		image = new ImageIcon("menu1.jpg");
                this.mp = mp;
display = new MapLabel(image, "Display", mp);

And now to pass the MainPage that is opened as paramater

MainPage class

public MainPage() {
		super("Test");
		setSize(180,100);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

// "this" is a MainPage. This MainPage that you just created and has all your Panels and labels
		mpp = new MainPagePanel([B]this[/B]);

		Container pane …
kathmartir commented: good job, your idea work well +0
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When I want to go from one frame to another I use:

JFrame.setVisible(true/false);

If you call it with false, the frame is not lost. It is just not visible. Meaning that you can call back the same method and set it to visible(true) whenever you want and the frame will reappear

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I believe that your problem is this:

String s1 = new String( secondLength );
....
lastName.getChars ( 0, 5, secondLength, 0 );

When you create the "s1" the "secondLength" array is empty. Then you call the method that gives to the array values, but the s1 was already created.
Try first to enter values to the array and then create the String "s1".

Also try to print the result at a System.out.println :

String finalResult = firstName.charAt(0) + s1 + number;
System.out.println("Final Result :"+finalResult);
Teethous commented: Very concise approach +1
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Check the class:
java.text.SimpleDateFormatter

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
for (int l = 0; l < info.length; l++){
             info[l] = loadPlayerPositions.readLine();

             info[0] = Players.nameOfPlayer1.toString();
             info[1] = Players.nameOfPlayer2.toString();
             info[2] = Players.loadplayer1currentblock;
             info[3] = Players.loadplayer2currentblock;

             Players.loadCurrentPositions();
             Players.currentPositions();

             [B]loadPlayerPositions.close();[/B]

       }

You are inside the loop, you close the BufferedReader and when the loop runs again you try to read from a closed BufferedReader. When you close the BufferedReader you cannot read from it again. Close it after you are done reading.
Also I believe that you should check if the line read: info[l] = loadPlayerPositions.readLine(); is not null in case the file has less than 4 line

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I tried putting e.printStackTrace() but it didn't accept it saying that "void" cannot be used. When i checked, printStackTrace() was "void" after all. I tried using getMessage() and I got a message saying "Stream closed" when I tried running the program again.

catch (IOException e){
               JOptionPane.showMessageDialog(null, "IOException: " +  
               e.getMessage());
}
catch (IOException e){
    System.out.println(e.getMessage()); //returns String

               JOptionPane.showMessageDialog(null, "IOException: " +  
               e.getMessage());

   e.printStackTrace(); // is void
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Put a e.printstacktrace at the catch block, post your new code with the errors that you get

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

For reading lined from a file:

try{
             BufferedReader loadPlayerPositions = new BufferedReader(new FileReader("/media/KEI-SAMA/CS11/JOKE CS11/KnowGoMilestone5/playerpositions.txt"));

String line = loadPlayerPositions.readLine();
while ([B]line!=null[/B]) {
  System.out.println("Line read: "+line);

   [B]line = loadPlayerPositions.readLine();[/B]
}
} catch (Exception e) {
  System.out.println("Exception: "+e.getMessage());
}

With that way you read each line.
Do some checking in case the file doesn't have all 4 lines that you want.
You get the exception at that line: info[0] = Players.nameOfPlayer1.toString(); probably because Players.nameOfPlayer1 is null

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Check the class javax.swing.ImageIcon with constructor: ImageIcon(String filename) Then you can use a JLabel with constructor: JLabel(Icon image) You might want to consult the above classes' API

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

And a new suggestion if you can't get you code to work.
I find your solution very confusing and not object oriented. I believe that you problem is that when it comes to time you return values from the array, but when it comes to name you declare an array but you use a single String variable which you set and return. Therefor all the times will have the same name from the method "getName".
Shouldn't you have a method that depending on the time or its position you set the at the appropriate position the name at the StringArray?

I don't know your whole logic but you can try this:

// SINGLE CLASS WITH JUST PROPERTIES

public class Appointment {
  private int time = 0;
  private String name = "";

  public Appointment() {
 
  }

// add get,set methods

}

Then have a class like this:

public class AppointmentBook {
  private int position = 0;
  private int max = 0;
  private Appointment [] appointments = null;

  public Appointment(int max) {
     // this.max is the global
    // max is the local from the argument
     this.max = max;

    //appointments is the global
    appointments = new Appointment[max];
  }
  
   // create methods that add and get appointments to the array by increasing the position variable
  // also check the value of position in case you have too much appointment
  // add whatever functionality you want
}

examle:

Appointment ap = new Appointment();
ap.setName("Name");
ap.setTime(200);

Appointment [] appointS = …
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all look at this again:

public void setName(String appt){
        temptext = appt;
    }
    public String getName(){
        return appt;
    }

You return the appt variable but in the code that variable never takes value. You just declare it and it always returns null. But you set the value to the temptext variable; shouldn't you return that instead?
Also these 2 appt variables are NOT the same:

private Strign appt = null;

    public void setName(String appt){ // appt exists only inside this method
// it overrides the global
       temptext = appt;
    }

When you call the set-method the global variable appt doesn't take value.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post more code. Skip the presentation code and post for start just the code with the logic.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can you see something wrong with this code:

public void setName(String appt){
        [B]setName[/B](appt);
    }

You calling the same method. When you call setName it will call setName which is inside it, which will call setName which is inside it, which will call setName which is inside it, . . . .

You need to assign the property name inside the method:

public void setName(String appt){
        name = appt;
    }
JimD C++ Newb commented: Thank you! +1
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I believe that they are both the same when it comes to the number of Objects created.
The difference is the scope of the "MyClass" instance. In the second case you can use the "m" outside the for-loop. With the first case you cannot.
The second case is useful when you want to create an Object inside a "block" and use it outside that block:

This will not work

try {
   BufferedReader [B]reader[/B] = new BufferedReader(....);
} catch (IOException e) {

} finally {
    [B]reader[/B].close(); //WRONG the reader was declared in the "try", so you cannot close it in the "finally".
}

This will

BufferedReader [B]reader[/B] = null;
try {
   [B]reader[/B] = new BufferedReader(....);
} catch (IOException e) {

} finally {
    if ([B]reader[/B]!=null) [B]reader[/B].close(); //You created it in the try and you close it in the finally. Will work because it was declared outside
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You haven't defined the variable: password1
Also you don't need 2 Scanner objects.
Use one and call the method nextLine as many times as you like

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When you go back to the DemoSession.jsp this code is executed:

String name = request.getParameter( "username" );
session.setAttribute( "theName", name );

But this: request.getParameter( "username" ) will return null since you didn't submit anything in the request. So you get null and you put null. There are ways to fix this but we generally don't do things that way. You could do this:

<a href= "DemoSession.jsp?username=<%= session.getAttribute( "theName" ) %>">home</a>

Also whenever you go to that page you need to make sure that you have submitted a field with name "username" or put it in the link as described.

Usually I don't get such a problem because I don't put these things together:

String name = request.getParameter( "username" );
session.setAttribute( "theName", name );

I put the setting of the session at a page before the homepage. So when I go to homepage I already have the session. And I don't return to the first page unless I have logged out.


You can also do this:

String name = request.getParameter( "username" );
String sessName = session.getAttribute( "theName" );

//check whether one of the above is null and act accordingly

Example: if name is null and sessName not null you can do this:
name = sessName
Or put the name in the session only when it is not null.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I don't know if it is possible to get the "My Computer" as root, but check this out:

File [] files = File.listRoots();
        for (int i=0;i<files.length;i++) {
            System.out.println(files[i]);
        }

This will get you all the files that are under the "My Computer". So instead of having one root ("C:/program files"),
you can create one father, and under that father put as nodes the "Files" that the above code returns:
C://, E://, D://

I don't know if it makes sense, but you can have this:

root = new DefaultMutableTreeNode("root", true);
 
          File fList[] = File.listRoots();
          for(int i = 0; i  < fList.length; i++)
              getList(root , fList[i]);
          }

After all, under "My Computer", all you have are the drives of your computer: C:/, D:/

Also you can check this tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html

And I would suggest not to populate the tree recursively because it takes awfully long. Try to put Listeners and when a folder is clicked, then take and add the children.

Question: Have you tried JFileChooser?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I searched for it on google but no success.
Thanks

Well if it is not on google you will have to write it yourself. If you have any trouble, post your code and you will get help.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When the user logs in take the username and put it the session:

String username = request.getParameter("username");
String password = request.getParameter("password");

boolean b = checkLogin(username, password);

if (b) {
  // user valid
  request.getSession.setAttribute("username", username);
} else {
  // Error: invalid username, password
}

That's it. It's in the session. You don't need to put it every time you go to a page, because it's IN THE SESSION . Just get it and check if it is not null.
Also learn the difference between session and request, and what it means to put something inside each one of them.

Also when you logout do this:

request.getSession.setAttribute("username", null);

These are the only two times where you will need to set the value of the session.

I didn't read your code nor will I ever read such un-formatted code.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Run this:

int N = 4;
int M = 3;

for (int i=0;i<N;i++) {
  System.out.println("I= "+i);

  for (int j=0;j<M;j++) {
    System.out.[B]print[/B]("J="+j+" ");
  } // end inside loop

  System.out.println();
} // end outside loop
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You are not suppose to change the username in the session. You put it once when you log in. You don't set it every time you load a page.
Of course it is null every time you do this:

String username = request.getParameter("username");
session.setAttribute("username", request.getParameter("username"));

If you don't pass the "username" as parameter in the request the first call will return null and then you will set null in the attribute.
The first call is completely useless if you have already put the username in the session after login.

So once you login put the username in the session and that's it. You don't need anything else. All you need to do next is get it from the session whenever you need it.
And after logout set it to null

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you put it in the session it shouldn't got away. You probably put it in the request, so better show some code.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

How do you get the responses?
Are they String objects like the one in your example or are they HttpServletRequest, HttpServletResponse objects?

What you said about the JSP is irrelevant. You don't need a JSP to get the response. If they were, how you think you get the request from a sevlet?

What code do you have to get the response/request?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The first time the page loads this is null:

request.getParameter('searchPhrase')

So this is translated to:

window.open("searchRequestPopUp?searchId=null", . . .

So when the pop up opens the searchId has value "null".

But after you submit the form of page A you send at the request the "searchPhrase" so this has value:

window.open("searchRequestPopUp?searchId=valueEntered", . . .

And you get it at the pop up.

I would suggest to remove this line:

function startPopUp() {
// REMOVE //document.PageAForm.submit();
window.open("searchRequestPopUp?searchId=" +  request.getParameter('searchPhrase') + ",'window','width=800,height=300','scrollbars=yes','resizable=yes','statusbar=yes','menubar=yes','toolbar=yes','dependent=yes');
}

You don't need it. All you need is to open the new page with what you entered at the search text. Right? Then you can do this:

<script type='text/javascript'>
function startPopUp() {

var v = document.getElementById("searchPhraseId").value;

window.open("searchRequestPopUp.jsp?searchId=" +  v, 'window','width=800,height=300','scrollbars=yes','resizable=yes','statusbar=yes','menubar=yes','toolbar=yes','dependent=yes');

}
</script>

<input type="text" name="search" id="searchPhraseId">

<input type='button' value='Search' name='sourceListSearch' onclick='startPopUp()'/>
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post code of the page that opens the pop-up

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hi,

I'm trying to use the request.getParameter("myInputField") on Page A. The problem is that the input field "myInputField" is on Page B. Does anyone know of a way to retrieve from Page A, the value of the input field that is on Page B?

Thanks,
-Ash

You need to submit the form that is at Page B to the Page A.
In details:
>The input field must be inside a form tag
>When you submit, the control will be transferred to the page that you define at the action attribute of the form tag
> At the next page you will have access to everything the form tag contained:

<form action="pageB.jsp">

<input type="text" name="myInputField" />
<input type="submit" value="Click here to submit" />

</form>

pageB.jsp:

<%
String myInputField = request.gteParameter("myInputField");
%>
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I want to create a custom NumberFormat which returns empty string for negative values and the double number iteslf for +ve and zero values. Please guide me....................

Then write a method that:

if value is negative return "empty String"
else return the value

You need to explain your problem better and show some code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post relevant code then

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I would suggest to use the split method:

String [] sentenceArray = enteredText.split(" ");
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I have this in my code:

<a href="loginservlet?action=logout">Logout</a>
String action = request.getParameter("action");

And it prints correctly the value "logout".

How do you map the servelt at the web.xml file? Are you sure there is nothing wrong?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
<a href="ProductController?linkValue=A" target="right">A</a>

And at the servlet use the "request.getParameter"

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you tried using 2 different forms?

<form name="form1" method="post" action="controller/servlet">

  <input type="submit" name="event" value="Add">
</form>

<form name="form2" method="post" action="controller/servlet"
 onsubmit="return confirm("Are you sure?");" >

<input type="submit" name="event" value="Update" >
</form>
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you tried my suggestion? I believe that you can't get the value from a "button", but I might be wrong.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What exactly are you trying to accomplish?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Where do you want to be printed?
I would suggest to have a button with each row that has that "onclick" event.
When the rows are dynamically created you can have the value you want to be printed passed as a parameter at a method in the "onclick" event.
I don't if you can just click the row. Check this site to see if the
<tr> tag has an "onclick" event:

Javascript
HTML

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hello, for the project I'm working on, I need to be able to access say, a JPanel object that was created from the execution of a Jar file, and use it inside another class outside of the Jar file. Any ideas?

Thanks

If you want to access any object, you need to have a method that returns it. If you want to use the classes and methods that are in jar you need to put the jar to your classpath.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

So the conclusion is only servlet can mapping.

Actually, I've never seen JSPs being mapped. From what I know you map servlets because, the servlet you define, is just the class name. But you cannot call class names, you need to map it to a url. So it is like saying what is the url of the java-servlet-class you created.
But for JSPs, they can go directly to the url. No mapping is needed.
Now if it can be done I don't know, but I am sure that it is not needed

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

There is no reason to do it for jsp. What are you trying to accomplish?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all we need to see the code at the servlet.
Second, you can have a hidden parameter in the jsp and the user clicks update to change its value:

<html><head>
<script language="JavaScript">
function update()
{
var r=confirm("Are you sure?");
if (r==true)
  {
document.form1.action.value='update';
document.forms[0].submit();
  }
else
  {
document.form1.action.value='';
  }
}
</script></head>
<body>
<form name="form1" method="post" action="controller/servlet">
...
...

<input type="hidden" name="action" value="" />

</form>
</body>
</html>

Also check these 2 links:

http://www.w3schools.com/default.asp
http://www.w3schools.com/js/default.asp

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I would suggest that you stop working on this servlet thing and start learning some basic java because this error is very basic and usually done by beginners.

You declare the Map in one method locally and you try to access it from another method.
Declare it globally or declare another Map in "doGet" depending on what you want.