javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Actually your initial code was just searching for the 1st and 2nd number.
If you want sorting, then the algorithm is entire different.
It depends on your specifications. I mean the easiest way to do this, is first to sort the array and then you will know that the 1st number is the first element of the array, the 2nd number is the second element of the array.
Sorting is easy. It is more difficult to find the second number without sorting, which why I was referring to your specifications. Maybe you were asked to find the 1st and 2nd without sorting

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The answer is very simple. You declare 2 variables:
"min"
"secondMin"
that hold the values of the "min" and "second min". So why don't you declare 2 more values that will hold the indexes:

"minIndex"
"secondMinIndex"

int min;// declare variable
int secondMin;// declare variable

int minIndex;// declare variable
int secondMinIndex;// declare variable

.....

if (values[0] < values[1]) { 
		        min = values[0];
		        secondMin = values[1];

                       minIndex = 0;
                       secondMinIndex = 1;
		    }// end of if 
			else {
		        min = values[1];
		        secondMin = values[0];

                       minIndex = 1;
                       secondMinIndex = 0;
		    }// end of else

.....

if (values[i] <= min) {
		            secondMin = min;
		            min = values[i];
                       
                       secondMinIndex = minIndex ;
                       minIndex = i;
		        } 
		        else if (values[i] < secondMin) {
		            secondMin = values[i];
		            
// This wrong. You don't want to change the values of the array.
// just save the values[i] to the secondMin variable
//values[i]= secondMin;

                       secondMinIndex = i ;
		        }

Look at my comments in the above code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can have a form at Main.jsp that submits to the return.jsp.
Create an input field or a hidden field at the main, and take its value at the return.jsp from the request:

Main

<form action="return.jsp">
  <input type="text" name="textName" />

  <!-- OR -->

  <input type="hidden" name="hiddenName value="some Value" />
</form>

return

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

First of all I find it surprising that you wrote this code and still couldn't find the answer.
I mean was it so difficult to add this to your code?

if (lineNo==5134)
{
  line = br.readLine();
} else if (lineNo==5100)
{
  line = br.readLine();
} else if (lineNo==5200)
{
  line = br.readLine();
}
..
..
..
 else {
  br.readLine();
}

Of course you code prints only one line because you have only ONE variable "line". If you want to print all of them either add a
"System.out.println" at the place that you read them:

if (lineNo==5134)
{
  line = br.readLine();
  System.out.println(line);
} else if (lineNo==5100)
{
  line = br.readLine();
  System.out.println(line);
}

Or declare an array that will store the line that you want:

String [] lines = new String[N];
int i=0;
.....
if (lineNo==5134)
{
  line = br.readLine();

  lines[i] = line; 
  i++;
} else if (lineNo==5100)
{
  line = br.readLine(); 

  lines[i] = line; 
  i++;
}

But that wouldn't be quite correct.I have noticed the way you read the file:
>> for(lineNo=1;lineNo<10700;lineNo++)
It is wrong. In general you wouldn't know how many lines the file has. So instead of the for loop use this:

FileReader fr = new FileReader("C:\\IMS.txt");
BufferedReader br = new BufferedReader(fr);
int lineNo = 0;

String line = br.readLine(); 
lineNo++;

while (line!=null)
{
  // do whatever you want with the line or the line number
System.out.println("Line Number: "+lineNo+", Line: "+line);

  line = br.readLine();
  lineNo++;
}

Whenever you read a new …

BestJewSinceJC commented: OP didn't mark as solved, although it was solved. +8
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you looked the API of Vector?
I assume you didn't because if you did you would have found this method:
Vector.add

Also you need to create a class:

class ClassA {
  private String a = null;
  private String b = null;
.......
}

with public get,set methods.
Then for each row/loop of the database you create a new object of that class and you put that in the vector.

Vector v = new Vector();
while (rst.next()) {
        ClassA cla = new ClassA();
        cla.setMethod...(rst.get....(1));
        cla.setMethod...(rst.get....(2)); //other method

        v.add(cla);
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Classes begin with capital. Methods with lower.

StringConcatenate stringConcatenate1 = new StringConcatenate(); 
stringConcatenate1.stringConcat();

You create an object/instance of type StringConcatenate. And you call the method stringConcat() of the instance stringConcatenate1.

Most of the time different instances have different results when they call the same method:

StringConcatenate string1 = new StringConcatenate("aaa"); 
StringConcatenate string2 = new StringConcatenate("bbb");

string1.stringConcat();  
string2.stringConcat();

That is why we create different instances of the same class.

Also this would only be correct if the method was declared "static"

StringConcatenate.stringConcat();

If the method is static you don't need to create an instance to call it

majestic0110 commented: Excellent response, thank you! +4
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Here is another code snippet that might help:

BufferedReader reader = null;
try {
  reader = new BufferedReader(new FileReader("fileName"));
  String line = reader.readLine();
  while (line!=null) {
       // do whatever you want with line
      System.out.println(line);

     // this MUST be the last command:
    line = reader.readLine();
  }
} catch (IOException ioe) {
   System.out.println(ioe.getMessage());
} finally {
   try { if (reader!=null) reader.close();  } catch (Exception e) {}
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

yeah i want them to put in an array, actually i want those data from the database to be display in the JTable, i have 8 columns instead of using 8 vectors i just want to put it in a 2 dimensional Object array, thats why i want to know the record count and also the row and the col from the database...the changes everytime ill search for a record......

You won't need 8 Vectors, but One. Create an Object that represents each row/record and put that in the vector

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First this could be helpful:

select count(*), col_1, col_2 
from myDatabase

Each "row" of the ResultSet will have the row count as well as the columns.

Also you can run 2 queries.
The first that will get you the row count
The second that will get you the columns


But the above suggestions aren't very good.


IF you want both the row count and the columns, I assume that you want the row count to create an array and then put inside the array the data from the table. Since you are using an array you need to know exactly how many rows will be returned before creating it. Right?

If that is the case, use the java.util.Vector. It has methods for adding and getting elements. It doesn't have a fixed size like the array. You can keep adding elements to it without knowing the number of rows the query returned

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

hi guys, how can I retrieve the total record count from a database? I mean is there a method like "rst.recordCount"? or whatsoever? thanks...

If you run this query at an SQL editor:

select count(*)
from your_table

It will get you the number of rows that table has.
In general the count() method "returns" the number of rows that the query would return.
So if you run the above query through java and do something like this:

ResultSet rs = null;
...
...
...

int count = rs.getInt(1);

The query return only 1 column (count(*)) so you need to call:
rs.getInt(1)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

That you want in the way you want it cannot be done.
If you want to call a java method, after the page has rendered, by clicking a button you need to submit the form, to a servlet or jsp (preferably a servlet) and there you can call your java method. Then you must redirect to whatever page you want with the results.

Check the first Read Me post of the jsp forum about MVC:

"JSP database connectivity according to Model View Controller (MVC) Model 2"

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all what do you mean by custom tag?
Second, I believe that they should behave the same, whether it is clicked with a mouse or by pressing <Enter>. If I am wrong someone please correct me.
And third, is it possible to see some code?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I don't know if this the case, but you MUST first call: rs.next() to get the row from the ResultSet.
Meaning that when you do: String Instructor = rst.getString("Instructor"); you probably get an exception, go inside catch, and nothing is displayed:
Also about this:

if (Instructor == null) 
    Sorry, no match for that user in our database. Please try again.

If there is no match the rs.next() will return false. So you need an if statement checking that. If it is false, THEN there is no match. If there is no match then the rs.next() will be false not the Instructor null. If you manage to call rs.next() and the rst.getString("Instructor") then there was a row returned so there are values to retrieve.
When Instructor is null doesn't mean that there were no rows. It means that you got a row from the database and the table column with name "Instructor" doesn't have a value. Of course if "Instructor" is Primary Key that would be impossible but I am talking in general.

So I suggest, first put some message inside the catch that are printed at the page:

catch (ClassNotFoundException e) {
            System.err.println("Could not load database Driver!");
%> <%= e.getMessage()%> <%
        } catch (SQLException e) {
            System.err.println("Could not connect to to the database!");
%> <%= e.getMessage()%> <%
        }

And then and MOST IMPORTANT do whatever you want to do from the beginning but this time follow the tutorial at the top of the JSP forum with …

CodeBoy101 commented: This guy is one of the best reasons to come to Daniweb!! +2
VernonDozier commented: Good advice. +16
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Threads merged

Finally, Good job

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you don't get an answer soon don't start another thread asking the same thing. What did you expect? That the very first minute you posted everyone should give you the solution? Your impatience was very annoying, flooding the forum with new threads.
We are not here to serve your needs nor we are obligated to give you immediately the answer.
If you didn't get a reply then wait for someone who knows the answer to post. Posting and posting again will not change things.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Actually what I said is what I believe to be best practice. In general, you can do whatever you want.
But I doubt if anyone will write an entire application using only unicode. Can you imagine?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

That's because this for loop is never executed:

for (index = 10; index > array.length; index--)
                arrayCopy[index] = array[index];

'index' has value 10, array.length has value 10.
you say: index > array.length; which translates: 10 > 10 . It will return false and you will not go inside the loop.

Since you set index to be 10 (It is WRONG. Should be 9 ) and then you decrease it, the boolean expression shouldn't use the 'array.length'
You already start from 9 and then go down, so you don't need to check the upper limit.
You need to stop when the index reaches 0: index >= 0; Also if one array used index: arrayCopy[index] the other should use something else because with the way you have them the same elements will go at the same place: arrayCopy[index] = array[index]; .
Doesn't matter that you start from 9 and go to 0

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

But even if you can use it for declaring methods, you shouldn't. It is better to declare methods and variables with English characters and English descriptions since it may be read by others.

No you shouldn't use unicode for declaring methods and variables.
You use unicode for unsupported characters. If your keyboard has the 'a' then use it for the name of your method. What's the point to use unicode then

puneetkay commented: Thanks for helping! +2
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You may need unicode to print characters that are not supported. Like having an application that needs to print messages in various languages (English, German, Chinese) depending on user's selection.

It is used for internationalization. In one of my projects we had different property files used for resource bundles to print messages in different languages. For languages like English, French we had normal characters, but for Chinese and others we used unicode.

Unicode is also used for printing unsupported characters in HTML pages

But even if you can use it for declaring methods, you shouldn't. It is better to declare methods and variables with English characters and English descriptions since it may be read by others.

Meaning that you should name a method like:
> getNumberOfUsers

but not:
> epestrepseArithmoChriston

The later translates to the first method but you shouldn't name methods using your native language with english characters. Someone else might want to read the code.

It happened to me where we had to debug an entire application written in Italian. Not a clue what was going on. Babel Fish became my best friend

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can we see part of the code?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can have the class extend the Thread class.
If you don't want to change the code of the old class though you can try to create a new one that extends the old and implements the Runnable interface:

Runnbale

public NewClass extends OldClass implements Runnable {
   
  public void run() {
      ......
  }
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Maybe the username_ctr which you increment inside the catch becomes greater than 5.
Meaning that you need to add some debugging messages like printing the value of username_ctr.
Also I believe that you are sure that inside the catch you don't get another exception?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

From what I understood, first you delete and then you insert, when you want to do an Update.

Well you can try and run an Update query. When you "SELECT" data from the database you use a userID as you say in your example. Probably you use it at your "WHERE" clause at the query to get the data (UNIMP and IMP).
You can have ONE update query setting the values of the UNIMP and IMP data taken from the TextFields.

Or if for some reason that cannot be done, you can use "commit", "rollback".
First set the autoCommit of the connection to false.
Run your queries.
If both where executed: commit and set back the connection's autoCommit to true
Else if there was an exception do a rollback inside the "catch (Exception e) " and again set back the connection's autoCommit to true

All the above methods are available from the Connection class. Check its API for more info

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I took this example:

<select multiple="multiple">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

From this site:
http://www.w3schools.com/tags/att_select_multiple.asp

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Do you have somewhere another class named Random?

freelancelote commented: thanks a bunch +1
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

For calculating the sum try something like this:

1+2+3 + ... + i + ..

int sum = 0;

for (int i=1;i<N;i++) {
  sum = sum + i;
}

Also there is this method: Math.pow(a,b) .
Calculates the power:
Math(2,3) = 2^3 = 8

Now let's see some code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can try the .charAt() method of String class

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Well, the first hint would be the exception itself, which you did not specify here.

I would recommend using executeUpdate() which returns the number of rows affected instead of executeQuery(), which return a result set.

I thought that you cannot use executeQuery for "insert", "update", "delete" queries.
Anyway apart from using executeUpdate maybe you should leave a space between 'Taken' and WHERE:

String sqll ="UPDATE Patient_Comp SET taken='Taken'WHERE ID="+x ;
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The answer is simple English grammar:

A method throws an Exception:

public void method(int i) throws Exception {

}

The method throws an Exception when you call it


But you throw an Exception:

if (i<0) throw new Exception();

If 'i' is negative you will throw an Exception.

And if you put it together:

public void method(int i) throws Exception {
      if (i<0) throw new Exception();
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

i do n't know what have you done in

changeItemValueNew(form,z,y)

method

We still don't know if the code before the call of that method is executed

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Create the variable in the javaScript
and store the value in it
then use it

var   z=productCode;
var y=index;

I don't believe that it will solve the problem. If this will execute correctly:

var   z=productCode;

changeItemValueNew(form,z,y)

then this will also:

changeItemValueNew(form,productCode,index);
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all describe better what do you mean by this:
after giving that alert messsage it works otherwise not.

Also, firstly does the method being called correctly?

Try this to check the above:

function changeQuantity(form,productCode,index)
{
alert("Method called");
}

Then see if the arguments are passed correctly:

function changeQuantity(form,productCode,index)
{
alert(productCode);
alert(index);
}

Then try your code with alert messages between the other commands (like debugging) to see what is wrong

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Thanks for reply....
It's not working..
when i first time load my project it gives me error like i is undefined
But after giving an alert it works fine...

What do you mean giving an alert. Post the code that works

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Try this:
changeValue('<%=i%>')

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What are perfect numbers?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Don't forget to close the ResultSet, the Statement, and the Connection.
Also instead of while you could use an 'if'.
If more than one row is expected, use 'while', but like this:

returnInfo = "";
while (rec.next()) {
				returnInfo += "DVD : " + rec.getString("Title") + "\t"
						+ rec.getString("Genre") + "\t"
						+ rec.getString("Category") + "\t"
						+ rec.getString("Cost") + "\n";
			}
if(returnInfo.equals("")){
	returnInfo="No Results Found";
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

2nd thing keep in mind that while converting a String to Date - take care of format according to your database. Some may take as mm/dd/yyyy or dd/mm/yyyy. So, accordingly pass the parameter to SimpleDateFormat constructor.

Hope it solves your problem. And do let me know.

If the column in the DB is declared to be type Date, the format is irrelevant. Because the column will contain Date not a Varchar. The data int the column doesn't have a format nor it needs one because it is type data. You can put data inside it in any way you want as long it is type Date.

Check for these sql functions:
to_date(), to_char()

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You need to print the elements of the array.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

//amount of accounts : 320
Account #/PIN/amount/type
30 39743 57.35 b
44 61405 68.96 l
9935 25686 2.67 l
9941 77300 9.8 b

Posting hundrends of lines will help us understand but if you just posted a few line like I did wouldn't ?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Read carefully my latest post

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Create a seperate Project with some classes and then you can import these classes like you import any other class:

import java.util.*

The only difference is that you will be importing your classes with their packages.
You can take the .class files (in their folder/package) or create a jar file with the classes. Then add that to the classpath of the project you want them to be used.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all what type of argument does ps.setDate() have and what type are the to_date, from_date at your database?

Also if you need to convert a String to a Date try this:

String sDate = 01/04/2009;  // 1st of April
String format = "dd/MM/yyyy"

SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setLenient(false);

Date date = sdf.parse(sDate);

Check the API for more: SimpleDateFormat

peter_budo commented: Simple and effective solution +18
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Well first of all you need to declare Scanner like this if you want to read from a file:

String fileName ="C:/MyFile.txt";  //myfile
      
      //Scanner scanner = new Scanner(fileName);

      Scanner scanner = new Scanner(new File(fileName));

With your way, I tried to do scanner.next() and it returned the fileName, meaning it returned the String you put at the constructor not the elements of the file.

Also in the while, you forgot to increment the cur value:

while(scanner.hasNextInt()) {
         int x = scanner.nextInt();
         temp[cur] = x;
      
         [B]cur++;[/B]
      }

I would suggest to add an extra check at the whill loop in case the file is too big:

while ( (scanner.hasNextInt()) && (cur<temp.length) ) {

Also at the for-loops it is best to use the length attributes. If you change the length of the array, you don't need to search and change the entire code for all the loops:

int i;
      for (i=0;i<temp.length;i++){
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I'm still working on that problem. I can't figure it out, would it be possible to give me a similar example, or even would you know where can I look for a tutorial on the subject.:)

Well I will repeat myself. In the calcSubstitutionScore you create new List: List<Integer> subScore = new ArrayList<Integer>() that has no elements inside. And then you are trying to access it, so naturally this: subScore.get(tmaMarks) will not work since it has no elements.
Shouldn't you be using the private subScore List after you enter some values to it, instead of creating a new one with no values?

Secondly, did the average method had an argument or you added it. Because I believe that it should calculate the average value of one the lists declared in the class, but again I don't know what it is supposed to do.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
while(line != null) {
            line = reader.readLine();
                       //int W = Integer.parseInt(line.substring(0,1));
                       //int xVal = Integer.parseInt(tokens[0]);
                      //int xVal = Integer.parseInt(tokens[1]);
                       // addPoints(W);
                       // yPts[].addPoints(W);
            int x = Integer.parseInt(line.nextToken());
            int y = Integer.parseInt(line.nextToken());
            xPts[cur] = x;  // these are created as public arrays earlier.
            yPts[cur] = y;
            cur++;
         }

"line" is a String. Where did you get the nexToken thing? Have you looked the example with spilt method?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

post the Student class

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

In your calcSubstitutionScore method you create new lists. You don't use the ones with the scores

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Again you cannot access the elements of a list without checking their size. When you are calling this:
subScore.get()
you need to make sure that the argument is between 0 and size()
According to the error your list is empty

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

One of your variables is a List, so natuarally you cannot add ("+") a list to an integer