javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can you post the code where you add rows to the table or you change their values?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Check the forum for the usage of the class Scanner. It is used for reading from the keyboard.

Operator: '%'
Assuming: A = b*c + d
Then:
A%b = d
A%c = d

Small example:

int a1 = 10;
int a2 = 9;

int r1 = a1%2;
int r2 = a2%2;

Don't expect a complete solution. You have many small problems. Start by writing code for each one of them separately and post the code you have written. Do not flood this thread with unfinished code from all your questions. Try to solve one by one. Post code for one problem. Once you solve it, post the next

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Even though it is not a problem, you don't need to call the getSelectedCol, since you don't use it. Also it is not necessary to call so many times the getSelectedRow. You call it once and you get the row. So you can use that.

Also if the user clicks the "Delete" button without selecting a row, it will return -1, as you have noticed from your previous exception. You need to handle that.

int row = visittable.getSelectedRow();
if (row==-1) {
  // no row selected
} else {
   if want to delete the selected row {
     // USING YOUR CODE: 
     String idnum = (String) visittable.getModel().getValueAt(row, 1);

// first delete from database
                    VisitMethods delmthd  = new VisitMethods();
                   delmthd.delete(idnum);

// if the above executes, then you can safely remove it from the table
      model.removeRow(row);                            

model.fireTableRowsDeleted(0,row+1);
                   refreshwdw();
  }
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Okay? I have no idea why anyone would want to "vote down" that post, but ......

To each his/her own, I guess. What about it was disagreed with would interest me though.

Another victim of the anonymous down voting, raises the issues that we have so often discussed regarding that matter in other threads

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I have been busy, so I could not reply back. Even though your responses haven't been most helpful.

You initially posted 4 values: 0,1,-1,1 without explaining anything. Then again you posted 4 System.out.printlns. Ok I know that this is the command you used , but from where did you call those. Before calling that method you created, after, inside?
Also that method takes 2 arguments and you posted 4 values. The idea is to print the arguments of the method that is giving you the error: getValueAt.
But from the code you wrote that method takes as arguments the row, col which as you said have values 0,1. So you shouldn't be getting ArrayIndexOutOfBoundsException.

So try to post the whole code. Not just some unrelated System.out.printlns. The one that calls the method and prints the values.
Print only the arguments of the method that gives you the exception just before you call it with those arguments.

Because if you use the getSelectedRow/Col that returned -1, of course you would get that exception. But you posted 4 values without saying which of these 2 you used.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The method takes 2 arguments. Row, Col index. What are those 4 values?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Well, what do you get from printing the arguments? Why do they have those values? What are your conclusions? Asking your self those questions will help you find the answer.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you tried to print the arguments you are using to that method in order to see why you get that error?

You know it is very easy to debug on your own. We are here to help you, but don't just post every error you get and expect others to solve it so you can post the next error and so on and so on. What actions did you took to understand the above error. Have you given any effort apart from posting the error here and waiting.

The above trick of printing the arguments was very easy to think on your own. Students that program for the very first time use that, not someone that wants to be hired as a professional.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Create the image object from the file, then create an ImageIcon frmo it and the set the button's icon with that icon.

Image myImage = getToolkit().createImage("myImage.jpg");
ImageIcon myIcon = new ImageIcon(myImage);
myButton.setIcon(myIcon);//here you set the icon for your button

If you had read carefully his question, jemz has asked how to make the image smaller in order to fit, not how to put an image in a button

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

In a previous post you used a method that allows you to take the value of a specific cell. It takes 2 arguments (the row and the column). What is wrong with using that.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You want to save what is at the jtable to the database. And just because you can't get the data from the jtable you insert null values and wonder why you get the exception?????

In previous posts you have been told how to get the values from the jtable. Have you looked at the API of JTable?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You got that exception because you tried to insert null values at the database. The variables of the instance you passed as argument are null. You need to give them the values you want to save

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Try to use capital first letter for classes:
Visit
Also don't give a method the same name as the class name. It can be confusing.


From that I understand it is not passing any values.
what can I do now

Also if you look at the code you are calling the method save with argument the visit object:
savmthd.save(vInfo)
The values that the vInfo has will be saved in the database.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Create a class with name, age, ... as attributes. With each loop create that object and put it into a Vector.
Then write code that displays the values of the Objects inside the Vector at the table

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You have the method that returns the selected row and the method that allows you take the values of whatever cell you want. And you have showed in your code that you know how to use them. The rest should be easy. It is only common sense

And by the way, are the cell already editable with defaulttable model or do I have to write codes to make them so

I don't know. It is your gui, have you tried to see if you can edit them?

Insert isn't a query, its an update. If I were to do that at the database I'll just be inserting the records manually.

Insert and update are both queries. And I didn't tell you to run all your queries manually. You have a query at the code that doesn't run. Have you tested it? I simply said, try to print the query and the values in order to run it at the database and see what is wrong with it

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The JButton takes as argument an Icon.
If you look at the class ImageIcon which extends Icon you will find 2 things:
The constructor of ImageIcon takes as argument an Image object.
There is a method: getImage() that "Returns this icon's Image."

The Image class has the method that you need to accomplish what you want.

Use that method to get the new Image, create a new ImageIcon and pass that as parameter to the JButton contructor

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

how to get it to begin after the index.

Being able to figure that out has nothing to do with your knowledge of java. It has to do with you being able to think.

Everything you didn't know about coding has been said. There is nothing else I can tell you regarding the coding. You know which method to use and what it does.

Now it is time for you to think and show how smart you are.

BestJewSinceJC commented: Ballsy thing to say, however, much more helpful to OP in the long run. +4
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Tried the print result and still no go. I really cannot find a way to get rid of the . in this API stuff. I can get it to stop at the . and show the first word but i cannot get rid of it at the end. i may have to ask my teacher on this.

Read what the subString(int) method does. Forget about the 'index' variable and what it has.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

yes, that is correct, i am actually using the JOptionPane so i have a UI or dialog box but yes it still does that.

Forget the JOptionPane. Try to print the right result first.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Looking for the last letters. So if its hello.java, i just would like java to come out. im still stuck with that dang . in there lol.

So when you call this:

System.out.println(In.substring(index));

You get:
> .java

Then read again the substring(index) API and see how you can change the argument.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I am looking over the API's and the substring can use 2 integers except when i do something like String one = In.substring(index, 0); its out of reach but if i do String one = In.substring(0,index); it gives me the first letters i have typed. Just trying to figure how i get the last letters/extension without the dot.

You want the first letters or the last letters? I thought you wanted the first. Anyway. Try to print these:

System.out.println(In.substring(0,index));
System.out.println(In.substring(index));

If you want the last letters without the '.' then it shouldn't be too difficult to understand which one to use and how, provided of course you have understood how they work.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Good you are on the right track.
I believe that you got rid of the NullPointerExceptions.

But you haven't looked very careful at String API for the substring methods.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I have the substring there, yet i still get errors. I dont think it knows where to end the substring and i dont think it will work when my input and output strings = null.. i get errors when those are there.

You know "where" the '.' is. Look again the substring methods and stop using variables that are null.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Well i have the Output.substring() but i have no idea what i put in between the ().. do i put Index, our Output or find. Ill let ya know if i figure it out

The exception tells you the line and that you are using something which is null. As for the other: READ THE STRING API FOR THAT METHOD.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The exception tells you where the error is.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You have the index of the '.' like I showed you. Now use the substring method to get the part of the string that you need. Then display in any way you want.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The indexOf will tell you where is the '.' dot:

String s = "abc.txt";
int index = s.indexOf("."); // 3
// a b c  .
// 0 1 2 3

Then use that index as parameter to another method of String that returns what you want.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you looked at the String API?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

visittable.getSelectedRow() visittable.getSelectedColumn()
When you select the row, the column get unselected. When you select the column the row gets unselected.
The api says that if a row or a column is not selected, they return -1, as the exception says.

About the insert, try printing the values you are passing and run the query directly to the database and see what happens.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

At your insert statement what values do you pass to each '?'. Are those values mapped correct with the database?

Also, you wrote the delete query. Meaning that you should know that it takes as argument the ID not the row number. You need to think how to get the ID of the selected row

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you tried printing the query to see what do you run.

About the delete method:
I will repeat myself once again. Read the JTable API. Are you sure that the getSelectedRow is what you need to pass as argument? What your query takes as argument and what the getSelectedRow returns.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

That is how it's done. Also at your insert query. Does that table have only one column? You might get errors on that query if the table PatientTable has more columns.


Also about how to call them.

I did this but how could I call this in the buttons' actionPerformed methods?

I am sorry to tell you this, but you don't know how to call a method, and you are trying to create a gui that connects to the database?
No wonder that you couldn't understand anything I was telling you.
I am not saying this to put you down. It's the truth. Everybody would agree with me.
Calling a method is the most basic thing. You have written all this code and you don't know that? That proves you shouldn't be doing this.

Stop what you are doing and start over. Learn how to create objects instantiate them, use their methods. Try small exercises.

Given the amount of information you received it should have been a piece of cake to finish this. You have been given anything you need to finish this. All the advices and all the code that you couldn't find by yourself.
Now if you can't do this, it would be because you haven't studied what is needed.

Given your last question made me doubt that you even understand what the code you have written does or that you understood the code I gave you. There …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you don't know sql you should have said that. You can search the web to find the syntax of the delete command. Then when you select the row use the JTable API to get the values of the row. You should have a primary key that you can use at the where clause. That needs to be taken from the JTable.

For the rest, I told you were and how to call it

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What errors do you get?

Did you look at the API for the JTable to see what the getSeletectedRow does?

Why aren't you closing the Connection, Statement? How many times do I have to tell you that?

I already told you that you need to call a query to delete the row form the database as well. Didn't you read that?

Why aren't you using separate methods for the database operations. Do you find it easier to repeat code every time you want to insert or delete something:

public void delete(int id) throws SQLException {
   // commands
}

All the Connections, ... will be declared in that method.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Thanks anyways

The solution is explained here. The example doesn't have a '-' operator but that is the idea of learning. Don't expect anywhere to find the exact code that you will need. You will find similar code, understand how it works and apply that behaviour to your needs.
Imagine what would have happened if we have told you to read the API

First of all use capital S:
System....

Second you do this:

system.out.println("The sum is:      "[B]+[/B]num1 + num2 + num3);

- You forgot the '+'
- Things are executed from right to left:

"The sum is: "+num1 + num2 + num3 -->
"The sum is: "+13 + 27+ 14
Meaning that first this will calculate:
"the sum is: "+13, which will create the String:
"The sum is 13".
Then this:
"The sum is 13" + 27, which will create the String:
"The sum is 1327".
Then this:
"The sum is 1327" + 14, which will create the String:
"The sum is 132714".

Whenever you "add" something with a String is turned into a String as well.


> num1=5
> num2=6

This: "A= "+num1 will return
>A= 5
This: "A= "+num1+num2 will return
>A= 56
This: "A= "+(num1+num2) will return
>A= 11

You should do this:

System.out.println("The sum is: " + (num1 + num2 + num3) );
System.out.println("The product: " + (num1 * num2 * num3) );

Or better, use …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What didn't you understand form post #8 of this thread:
http://www.daniweb.com/forums/thread255237.html

Also what errors do you get from the code you posted?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hi javaAddict: I hadn't realised that you were already dealing with mseck's problem. (And, once again, I apologise for my "senior moment" with the + , even worse now I've read the previous thread!).
I gonna bow out now, before I make a bigger fool of myself.

That was a different thread. But that is irrelevant. Anyone can post their opinions in any thread. It is not like that once some one posts an idea no one is allowed to continue with the help.
My point was that the OP already made the same question in another thread and got an answer.


To mseck:
It seems to me that you didn't add the parenthesis I told you in one of my answers at the other thread. What is the point in asking questions if you don't follow the answers?
Add those and I believe that the errors would be fixed.
If not post again the new code with the errors

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

For some reason when i try to compile it, i still get an error pointing at the operator

In your other thread, I already told you how to correct all your mistakes. I explained much in detail and told you what to change and correct, but you INSIST in writing the same things, as if you didn't pay any attention whatsoever to what I said.
You already have the solution in your other post but you chose to ignore it.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Yeah I read some
here is for the delete button what I've done, but the prob is it removes the row from the Jtable but not from the datadase table.

Because you need to run a query to do that. And try to put it in a separate method, in a separate class that takes only arguments.


At the save row, what do you want to do? Because there are methods that can make the cells of the table editable, so you can change their values.
And then, when you click "Save", just take the selected Row, use the API to take the values of the cell and call a query at the database

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hello musthafa!
Please help on this , I didn't get what JavaAddict meant.
Can you materialize the words by some snippets?
Thank you!

Did you look at the JTable API? There is a constructor that takes as argument TableModel. That is how you will instantiate your JTable. You already have the DefaultTableModel. Just use this as argument
And read their APIs. All the methods you need are right there.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You need to pass at the constructor of the JTable as parameter the DefaultTableModel. Check the API of JTable.
Then use the methods of DefaultTableModel to add, remove, or update the rows. Check the API of DefaultTableModel.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What do you expect to get?
Try to put some System.out.println outside and inside the loop and see if it goes inside. Maybe it never goes inside the loop becuase the command: java -classpath /home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/ filen[0] doesn't return anything. Try to print hte filen[0] and run it at a command prompt.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

This has more chances of working. Compare the changes I made with your own. What do you get as ouput:

try
                  {
   
       String line;
        // String path="/home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/"+filen[0];
      Process p = Runtime.getRuntime().exec
       ("java  -classpath /home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/" +filen[0]);
     BufferedReader input =
       new BufferedReader
         (new InputStreamReader(p.getInputStream()));
     line=input.readLine();
out.println("<html>");
    out.println("<body>");
     while (line  != null) 
     {
       
    out.println("<br/>Output is:" +line  );
[B]line=input.readLine();[/B]
    }
input.close();
    out.println("</body></html>");
                    }
      catch (Exception err)
      {
     err.printStackTrace();
    }

Also these need to be outside the loop: the <html> and <body> tags should be printed only once.

Also you only call the readline only once. Meaning that you will keep printing the same line and the loop will never end. It will run indefent.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

In your first code you were printing this:

line=input.readLine();
out.println("Output is:" +line  );

That should work. Have you tried it?


At the second code you are printing the object: p.getInputStream() . That is an InputStream object. It doesn't have any "printable" values.
It is not like you have a String object and you can print its value. When you call this: out.println(p.getInputStream()) with an object as argument, the toString method of the object is called.
It is like doing this: out.println(p.getInputStream().toString()) If you look at the API that object doesn't override the toString method from its super class: Object.toString(). That is why you get that, in comparison with this:
String.toString()

You need to use the BufferedReader to print each line.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all why are you calling out.close ? And especially inside the while. What needs closing is the BufferedReader input, outside the while. You close it once you are done reading. If you close it inside the while, the next loop will execute and try to read the next line, from a closed BufferedReader.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you looked the API for JFileChooser?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

No, with your way, he will get an OutOfMemoryException:

private void getStudentsByGrade() {
  if (...) {
     getStudentsByGrade();
  } 
}

After pressing yes too many times.

Couldn't you just use this in a single method:

while (ans.equalsIgnoreCase("Y")) {

}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can have a method in the Student class:

public int comporeByGrade(Student st) {
    return grade - st.getGrade();
}

The above method will return positive if this Student is greater than the argument, negative if it is lower than and zero if their grades are equal.

So write a for loop that loops through the array. Take each Student and use that method in order to determine which Student is greater than the other and then use any of the sorting algorithms provided.


If the issue here is not to implement a sorting algorithm for educational purposes but simply sort the list using any means necessary then you can have the Student class implement the Comparable interface.
Then implement its method: compareTo and use the sort method of the Arrays class.
Of course you will need to convert the ArrayList to an array for the sort method of the Arrays class, but that shouldn't be a problem

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

But those global connection, statement and resultset are used by other methods in the class.

Are you closing them? Because in your code, you call the getPatient, inside you have a ResultSet which you declare and close, and outside you are using another ResultSet which has no data inside, even though you already got the data.
My suggestion is to have methods that Declare the above (connection, statement and resultset), "open" them, "close" them and return the results.

You are not gaining anything by having them global. They shouldn't be in the same class. You should have another class with those methods, that return only results

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You have a method: Patient pt = getPatient(ptNo); which executes correctly, you call it and you get the patient info.
You are calling the method that brings you the data you want.

Then you call the rs.getString("..") again, outside the method, after you acquired the data
Why?

Delete all the global declarations of Connection, ResultSet and Statement.