javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Check the Read Me post at the top of the jsp forum:
JSP database connectivity according to Model View Controller (MVC) Model 2

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

On the top of this forum there is a thread with helpful information and tutorials

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Exception in thread "main" java.lang.NullPointerException
at Book compareTo(inventoryPart2.java:114

At line 114 of the inventoryPart2.java class you are trying to use something that it is null. There is a reason why you get such an explanatory message; so you can look it up yourself.
Not to mention that you didn't post the part of the code where you got that error.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

create a program using java for virus can u please help me.

Does this mean that you want a virus written in java or the opposite? An anti-virus written in java?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When you write this:

public void setName(String name)
{
   name = name;
}

Both "name" in name = name; refer to the argument. So you are just changing the value of the argument and you are putting the value of the argument back at the argument.

If you want to put the value of the argument "name" into the private property of the class then:

public void setName(String name)
{
   this.name = name;
}

Now "this.name" is the name defined in the class and "name" is the argument.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Use code tags. Click the button: "#" when creating a post

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post the code with "System.out.println" and tell us what it is printed and what is not. Does the rs.next returns true?
What is the value of rc ?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Your question doesn't make any sense.
For calculating sums and totals use a for loop

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Use code tags. Click this symbol when you write a post: "#"

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Semicolon ( ; ) separates commands. When you write this:

if (v1 < 500);
total = ((v1 * .02) + 5);

You have ONE if statement, that executes nothing because of the semicolon. It doesn't matter if it returns true or false. It "terminates" at the ';'
Then the second command will be executed always.
The same think happens with the rest.

Right approach:

if (v1<500)
  total = ((v1 * .02) + 5);

Better approach:

if (v1<500) {
  total = ((v1 * .02) + 5);
}

Even Better approach:

if (v1<500) {
  ....
} else if (v1 > 500) {
  ....
} else if (v1 > 1000) {
   ....
}

Right Approach:
The requirements say:

If more than 500, but not more than 1000 units are used

So the second if should be: ( (v1 > 500) && (v1 < 1000) ) Also you must have some of the '<' be '<='. If you leave them all '<' and the user enters 500 or 1000, none of the ifs will be true and nothing will be executed.
Also you must add in the end after the if the basic service fee of $5

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

Go to the JSP forum. On top of that there is a link with useful example

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

By searching this forum you will find plenty of examples on how to read files. Look for the classes:
BufferedReader
Scanner

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Try to put more debug messages:

System.out.println("Checking rs");
if (rs.next()) 
        {
         System.out.println("rs.next: TRUE");

           int rc = rs.getInt("RecordCount");
         System.out.println("rc= "+rc);
            if ( rc> 0)
            {
                isMemberAlive = true;
            }
        } else {
System.out.println("rs.next: FALSE");
        }
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I am sorry for being late to respond to this.

Firstly if you read all of the posts related to that particular question in both threads (the poster created 2 threads),
you will reach to the conclusion that the poster was accusing the programming language because he couldn't get his program to work they he wanted it.
Unfortunately this command hasn't been invented yet:

main() {
  runMyProjectTheWayIWantItToRun();
}

Secondly those who have replied, including me have suggested to and12 to post his code with relevant questions about his problem, as proposed by the forum rules. Not a single post contained code for us to look and comment

And lastly,
as VernonDozier said:

"Incompetent" means "not compotent in Java at this particular time"

I didn't called you directly "incompetent", I said:

...., for your incompetence

Meaning for your incompetence in java

It is different

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Then put the reading of the temperature inside the do-while.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Reading and writing to file using Buffered.....

BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(file));
           
                writer.write("Line is written to file");
                writer.newLine();
        } catch (IOException ioe) {
            System.out.println("Error: "+ioe.getMessage());
        } finally {
            if (writer!=null) writer.close();
        }
BufferedReader reader = null;
try {
            reader = new BufferedReader(file);
            String line = reader.readLine();
            while (line!=null) {
                System.out.println("Line read: "+line); 
                
                line = reader.readLine();
            }
        } catch (IOException ioe) {
            System.out.println("Error: "+ioe.getMessage());
        } finally {
            if (reader!=null) reader.close();
        }

For more info ask more specific questions

Also check the API of the above classes at:

http://java.sun.com/javase/6/docs/api/java/io/package-summary.html

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Is your code working? If not, where are you having problems with?
Also I would suggest, next time, to use the javax.swing.* package (JFrame, JButton, ....)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

This:

while (myPhrase != null) {
   myArrayList.add(newPhrase); /****ERROR here*/
}

is an endless loop. myPhrase is not null, you don't change its value inside the loop so it remains not null. Thus the loop will keep running forever and eventually you will run out of memory.

I don't know what the other classes do but:
1) You need to read inside the loop the next line
2) Search the forum on how to read a file using: Scanner or BufferedReader

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

hi
i am working on my final year project i am facing same kind of problem any suggestion for me

My suggestion is search relevant threads or start your own.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

This think:

dayDescription = (Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0) ? [B]28[/B]: [B]29[/B];

Returns int. The bold ones 28, 29 at the above code are ints. so you need to store them in an int value or have the expression return then as String: "28", "29".

You need to be more careful with these errors and learn how to fix them yourself. You should be able to fix these yourself and not go running for help with every single glitch.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Yes

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You need to give values to your variables:

int a; 
     int num2; 
     int num1;

I suggest you enter put some values inside the code in order to test if the logic is correct:

int a = 2; 
     int num2; = 4 
     int num1 = 10;

And then use the Scanner class to get the input from the keyboard:

Scanner scan = new Scanner(System.in);
if (scan.hasNextInt() ) {
   a = scan.nextInt();
} else {
   System.out.println("No number given");
   System.exit(1);
}

Also read this first
http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
int Monthname = input.nextInt();
int Monthday = input.nextInt();

switch (Monthname) {
case 1: Monthname = "January";
Monthday = "31";

Monthday, Monthname are declared as int, at the beginning of your code. But then you do this: Monthname = "January"; "January" is a String. You cannot put a String ("January") inside a variable declared as int.
Declare a different variable

int Monthname = input.nextInt();
int Monthday = input.nextInt();

String monthDescription = "";
String dayDescription = "";

switch (Monthname) {
case 1: monthDescription = "January";
dayDescription = "31";
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Do you know how to open a connection in java? That should have been your question all along. If you search this forum you will find many examples on how to do this.

I don't remember the exact syntax of all the commands needed but if you search this forum you will find plenty of examples.
The key words would be the classes I have described.
Also check the API of these classes.
Also for insert, update and delete queries use the executeUpdate method that returns an int.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Read the API for the method:
JOptionPane.showInputDialog

If you test your code and see what happens when the user clicks "Cancel" you will see that the method returns null as the API says.

So you need to do some checking of the "firstname","lastname".

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

P.S. I agree that java sucks :)

Why? Because Someone accidentally misplaced this '=' with this '=='.
I am not accusing anyone since I have made such mistakes when copying code, but tell me one language that doesn't use this operator: '=' for assignment.

I'm finding it to be frustrating and quite foreign from most other languages

And what other languages are you referring to, since most languages have if-else statements, assignment operators '=' and for loops.

But do other languages have such good documentation?
http://java.sun.com/javase/6/docs/api/overview-summary.html
There you can find the Scanner class and see all the methods with their description:

import java.util.Scanner;

You will have to go to the link:
>java.util<

After you have that, all you need to know is how to call a method and get what it returned, which is the same in all languages:

num1 = input.nextInt();

Calling method: nextInt with no arguments: nextInt() that returns an int:
num1 = input.nextInt()

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Read input from user.
Write if else statements to check what was given and print the appropriate message.
Search this forum for example on reading input with the java.util.Scanner class.
Check the API of the java.util.Scanner class

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Get the value.
Execute the query.
Check the API: java.sql. Connection, Statement, ResultSet
Ask specific questions about where are you having problem.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Use the api of the JCheckBox to get the values and then run an insert query to the database.
For better answer ask better and more specific question.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hi friends
what's a role of index in RMS?
when we use index in RMS?
plz I need your help.

What is RMS then?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Your second post is the wrong way to do things. Your first was correct, but you didn't understand the advice given to you.

for(int i =0;i<ar.size();i++)
{
   Employee emp = ar.get(i);
   System.out.println(emp.getPosition()  + emp.getDepartment() + emp.getName() + emp.getidNumber());
}

When you do this:

for(int i =0;i<ar.size();i++)
{
   Employee emp = ar.get(i);
   System.out.println(emp);
}

The toString method of the Employee class is called. Since you didn't override the method of the Object class is called, which returns what you used to get.

Read again the second post suggestion

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Your post is rather confusing. If I get it right:
You will have a list of <select> tags and checkbox tags. Each "select" will be connected with each "checkbox". When the user submits you will have to get the value of ONE "select" box and that select box would be the one that corresponds to the checkbox checked?

// int i is the index of the for loop
// length is the length of the for loop
for (int i=0;i<length;i++) {
  <select name="select_<%= i%>">
  ...
  </select>
  <input type="checkbox" name="check_<%= i%>" />
}
<input type="hidden" name="length" value="<%= length%>" />

After you submit have a loop that takes the values of the checkbox. When the one checked is found use the index to get the select:

String len = request.getParameter("length");
int length = 0;
if (len!=null) {
length = Integer.parseInt(len);
}
String valueSelected = "";
for (int i=0;i<length;i++) {
   if ("on".equals( request.getParameter("check_"+i) )) {
       valueSelected = request.getParameter("select_"+i);
       break;
   }
}

Notice this trick:

<select name="select_<%= i%>">
<input type="checkbox" name="check_<%= i%>" />

request.getParameter("select_"+i)
request.getParameter("check_"+i)
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I would have suggested for the user to give the date format as well. Or perhaps a try-catch with a throw inside the catch in case the user didn't enter a valid date.

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

and now that you believe me the explanation:

When you use "==" on objects you don't compare the value of object but the object itsself. Meaning that you compare if they are the same object. Assuming that I have created a custom class: Person

Person p1 = new Person("name");
Person p2 = new Person("name");

They have the same value but they are 2 different objects.

We use "==" to compare primitive types: int, float, ..
We use equals() to compare Objects: Integer, String, Point, Person

Person p1 = new Person("name");
Person p2 = new Person("name");

System.out.println(p1==p2);
System.out.println(p1.equals(p2));

The above will return
> false
> false (why?)

Because I haven't overridden the equals method in my class. So it will call the one from the super class. The super class is the Object class that checks if they are the same object


THIS:

Person p1 = new Person("name");
Person p2 = p1;

System.out.println(p1==p2);
System.out.println(p1.equals(p2));

The above will return
> true
> true

Because I do this: p2=p1 and I set them to be the same object.

If you want this:

Person p1 = new Person("name");
Person p2 = new Person("name");

System.out.println(p1==p2);
System.out.println(p1.equals(p2));

To return
> false
> true

You need to implement/override the equals method in your class:

public boolean equals(Object o) {
        try {
            Person p = (Person)o;
            return name.equals(p.getName());
        } catch (Exception e) {
        }
        return false;
    }
…
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you see the method its perameter is of type "EltType" so "==" will compare the objects. "equals()" can only be used on Strings.

You couldn't be more wrong than that.

"equals()" is not used to compare Strings. It is used to compare Objects and String is an Object. Also EltType is an Object

Check the API. Read a tutorial. If you don't believe me then I won't bother explaining it to you

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

asuming that words is an array list or a vector, modify your method as follows

public int indexOf(EltType e) {
        int i=0;
        for (i = 0; i < words.size(); i++){
            if(words.getElementAt(i)==e)
                  return i;
        }
   
        return -1;
    }

Actually it needs to be:

if(words.getElementAt(i).equals(e) )
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

If you check the API, the scan.nextInt() returns an int value, so it doesn't have a toString() method.
Objects have that method. "int", or "float" and the others are not Objects.
Try this:

myArray[i] = String.valueOf( scan.nextInt() );
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

Here are some refinements:

class searche2
{
   public static void main(String args[])
   {
     int result=0;
     String []mylist= {"tangerine","apple","blueberry","mango","jicama"};
     String []tosearch={"tangerine","grapes","blueberry"};
     for(int i=0;i<[U]mylist.length[/U];i++)
     {
       for(int j=0;j<[U]tosearch.length[/U];j++)
       {
          if(mylist[i].equals(tosearch[j]))
         {
         System.out.println("position is of: " + mylist[i] + " is: " +i);
         result=1;
         }
/* remove this for reasons described in previous post
        else
           result=-1;
*/
     } 
  }
 }
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

To say that I am not offended at being called "incompetent" in what i thought was supposed to be a friendly forum would be lying.

I stated long ago that i was NEWBIE to java.

All i meant by my original post was why not MAJORITY of people can enter a consonant with keyListener().

Forget all the little bugs about navigating away. I just mean why can't they even ENTER a consonant from the keyboard.

http://blogfriendlyzone.webs.com/wof/WOF.html

The problem is i can enter a char but my online friends can't. They're following my instructions as is and NOT doing anything weird.

I am sorry if insulted Java by saying it was full of bugs but i just don't get why they can't enter a consonant.

But people don't have to insult me personally by calling me names on posts I can't delete and feel offended by.

No one called you any names. Just stated that you blame others when it is you that you can't get something to work.
And instead of posting your code and asking for help like many others you started throwing accusation.
What was the point of your post. If you wanted your code fixed where is your code, and where is your question.


Also this the thread he was referring to:
http://www.daniweb.com/forums/thread216050.html

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Why didn't you answer my previous post then if you think you're so smart.

What previous post? Not to mention that no one here is obligated to solve every single thread as if we have some sort of deadline. I don't know what post you are referring to, but if I didn't answer it, that was my choice. I don't have to do it.

Also you never posted your code describing where is your problem and what you are trying to accomplish. We would have seen what you have written and provided solution. You just said that java has a bug and is preventing you from writing your code, because it is java's fault

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

I assume that this: words.add("apple"); is some sort of Vector or ArrayList?
If yes, you can loop the list and compare each element with what you are trying to much.
If found return the index. If none of the elements contain that value return -1

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

check this out

class searche
   {
     public static void main(String args[])
         {
         String s1="tangerine";
         int d=s1.length();
         int result=0;
       for(int i=0;i<d;i++)
          {
            if(s1.charAt(i)=='e')
             {
              result=1;
             System.out.println("position of e is at "+i);
             }		
           else
             result=-1;
          }		
             System.out.println("result= "+result);	

          
      }
}

I haven't read the entire thread, but I don't agree with the code.
The message will be printed when the 'e' is found but the result will not always be 1.
If the String was: "tangerin" the last character would be "n", The loop will check the last character and that will changed the value of result from 1 to -1. Better have this:

class searche
   {
     public static void main(String args[])
         {
         String s1="tangerine";
         int d=s1.length();
         int result=0;
       for(int i=0;i<d;i++)
          {
            if(s1.charAt(i)=='e')
             {
              result=1;
             System.out.println("position of e is at "+i);
              break;
             }		
           else
             result=-1;
          }		
             System.out.println("result= "+result);	

          
      }
}

Or you can initialize result to -1 and when it is found give it the index:

if(s1.charAt(i)=='e')
             {
              result=i;
             System.out.println("position of e is at "+i);
              break;
             }
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The only bug I see is you accusing a programming language which has been around for decades, used by thousands, for your incompetence.

Just because you can't get it to work doesn't mean it has a bug, it means you don't know what you are doing.

It is OK not to know, it is not ok to say that "java" has a bug when no one else has complained just because you rushed into doing something without the proper experience

This simple example can be found anywhere:

JButton butt = new JButton();
JTextField field  =new JTextField();

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
  if (source==butt) {

  } else if (source==field) {

  }
}

That is how you know what was clicked

tux4life commented: Yup, you're a legitimate owner of a 'Featured' badge :) +21
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