javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Stop replying to 2 threads. Have only one and use code tags.
Press the code button when making a new post and place your coded inside the the 2 code tags.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Why do you stop your code at case 6? Don't you close the main and class right parenthesis.
Anyway, given the instructions, place the switch statement, with the case block in the do while loop.

You will need to define the userChoice and give it value from the keyboard:

do {
 print_menu
 userChoice = take_from_keyboar

 switch () {

 }

} while();
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Use code tags and post your whole code. Don't tell me you stopped at case 6 and haven't closed anything. If you don't know how to do it then don't. But at least close all the unclosed brackets.
Next time when you open one, immediately close it and then write between.

Also the same code was posted in another thread. Wait until you get all the answers to the other thread before opening a new one. If the code doesn't work in one thread wait until it's fixed before posting a new question to another.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Use the String class. Specifically the split method. Check the String API for that method:

String s = "Bleach,22,13,TiteKubo";
String [] tokens = s.split(",");

//Now print the array and see what happens:
for (int i=0;i<tokens.length;i++) {
  System.out.println(i+": "+tokens[i]);
}

System.out.println("-----");

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

You have misplaced lots of '}' and '{'. You haven't some of those and you have opened others where you shouldn't. Review your code and check those brackets.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The OpenCV library is very good for image processing. Unfortunately it is for C.
I am not sure if there is one for java as well.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
boolean b = true;
while (b) {
   try {
      Thread.sleep(5000);
      // call method
   catch (Exception e) {
   }
}

The problem would be where are you going to put that code and when will it stop. I would advise you not to put that code in the gui because you will never be able to exit the loop once it is inside. It needs to go to a thread, so it will run in parallel with the rest of the code.

Can elaborate more on that:
"On another note can I set my status update to pull the get method every 5 seconds or something like that ?"

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can add get methods to your thread class. Assuming that the thread class instance is visible in your gui class, you can start it and you can call its get methods anywhere you want in the gui and get the latest values of the thread's attributes.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

what nested loops in java
1. sample output
*
*
*
*
*
2. enter a number: 3 input
*
*
*
3.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Start a new thread and show some code. For your problems you will need for loops.
For the first use a for loop with fixed upper limit. For the second use the input as upper limit.
For the second, you will need 2 loops one inside the other.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I would have done this:

<img src="images/bus.jpg" id="ck" alt="" width="40" height="40" name="imgChange" onclick="changeImage();">
function changeImage() {
  if (document.getElementById("ck").src=='images/bus.jpg') {
    document.getElementById("ck").src = 'images/nice.jpg';
  } else if (document.getElementById("ck").src=='images/nice.jpg') {
    document.getElementById("ck").src = 'images/bus.jpg';
  }
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You have to "connect" the objects: BookList and UserList. Either add an attirubte to one of those to be the other. Add to the BookList an UserList attribute.

book.setAvailable(resultSet.getBoolean("available"));
appliedUser.setId(resultSet.getLong("user_id"));

book.setAppliedUser(appliedUser);

bookList.add(book);

Or create a new class that has those 2 as attribute and return that class.

In the first case the code would look like that:

public ArrayList<BookList> getAppliedBooks(UserList user){
...

    ArrayList<BookList> bookList = new ArrayList<BookList>();

    try {

...

        BookList book;
        UserList appliedUser;
        while (resultSet.next()) {
            book= new BookList();
            appliedUser = new UserList();

...

            book.setAvailable(resultSet.getBoolean("available"));
            appliedUser.setId(resultSet.getLong("user_id"));

            book.setAppliedUser(appliedUser);

            bookList.add(book);
        }
        
    }
    catch(Exception e){

        e.printStackTrace();
    }

    finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }

    return bookList;
    }
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Then do some studying and check the API of the classes I suggested. I don't know if you have to implement your own stack class, but java provides one for you.
You should have been taught by now how to create instances of those classes and call their methods.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You will need JSP as well, not only HTML. Search for tutorials at the top of the JSP forum

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Assuming you have this: "((a<b)&&(c<d))"

Start by looping the String (check the String class API, use the methods, length, charAt() )
Write a loop and take each character of the String using the charAt method.

In the loop.
Whenever you find a '(' put it into the stack. Whenever you find a ')' remove the previous '(' from the stack. If the stack is empty and you can't remove then INVALID.
Ignore all the other characters.

After the loop check the stack.
If it is empty then the String is VALID.
If it still has elements, then INVALID

You can write your own stack, or use the one that java provides for you. Check at the API of the java.util package for the Stack class.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What values do you get when you print them? Where in your code you print them?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You haven't defined an id attribute at your fields in the form, you haven't "imported" your javascript file in the jsp file, and the onsubmit event must return true or false.
http://www.w3schools.com/default.asp

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you want validation in the servlet, then use java code after you get the request.
But that is not very good because you have already sent the request. The idea is to validate before sending the request at the jsp.

So you can try this:

<form name="..." action="... " onsubmit="return validateMethod();">

</form>

The validateMethod must return true or false. If it returns true the form is submitted.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Start with small steps.
First create the class, the constructor and start working on the first method that it is described.
All you have to do is fill the 9x9 array with numbers. Use the notes hat you have. You can use the Random class to generate random numbers.

You can have a 2D array of Strings with all of its elements to have the value:
"123456789".
When you put a number to a cell, use the replaceAll method of the String class in order to remove that number from the elements that are at the same row, column and "smaller square".

If you put at [0][0] = 1, then remove from all the cels: [0] the 1, as well as from [j][0] and the upper left square. Then select a random number for the next cell and loop the array until all the cells have one number. The random numbers must be selected based on the numbers that they already have.

You can have the random class return not a random number but a random position of the number. If one cell has this value: "1245679" then its length is: 7, So select a random number from 0 to 6 and put to that cell the number of that position.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post in the javascript forum or the JSP forum your code.
The error is with your javascript. You probably haven't defined an element with name eNew or you are trying to use it incorrectly

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When was this project assigned to you? How much time was it given to your class to finish it and when do you have to submit it?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I don't know your logic, but what if you try this:

class MultiThreader {

    main(...) {

 
       new MultiThreader().jobSpawner(jobName);
    }

    void jobSpawner(String jobName) {
        new Thread(new JobThread(this, jobName)).start(); // use 'this' here
    }

}

In this case the 'this' would reference the instance that is created in the main:
new MultiThreader().jobSpawner(jobName)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
new MultiThreader().jobSpawner(this, jobName);

'this' is a key word that can be used in a class only by its class members and it used to call class variables and methods:

private int a = 0;

public void setA(int a) {
  System.out.println(this.a); // it will print the value of the 'a' class variable
  System.out.println(a); // it will print the argument.

  this.a = a; // it will set the value of the class variable to be the argument
}

You cannot use the 'this' in a static method like the static void main()

In the main method you need to pass a instance of that class

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Then use something like this and see if it works:

document.<the name of the form the text field is inside>.employerName.value=""
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Does this have an id attribute? Can you try to write this:

<html:text property="employerName" id="employerName" />
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Just to add something to jon.kiparsky's good advice, each object has a toString method, whether it is overriden or not. Even the ArrayList.
If an ArrayList has String objects:

ArrayList<String> list = new ArrayList<String>();

list.add("aaaa");
list.add("bbbb");

// They both do the same
System.out.println(list);
//or
System.out.println(list.toString());

Then the above code will print all the Strings in the list, because that is how the toString method of the class ArrayList is overridden. What it does, it returns all the Strings in that list.
Meaning,
if you add any other object in the ArrayList, then again, the toString method of the ArrayList, will call the toString method of each of the objects in the list and return that:

Try this.

class Person {
  private String name = null;
  private int age = null;

  public Person(String n, int a) {
     name = n;
     age = a;
  }

  public String toString() {
     return name+" is "+age+" years old";
  }
}
ArrayList<Person> list = new ArrayList<Person>();

list.add(new Person("Aaaaa", 10));
list.add(new Person("Bbbbb", 20));

System.out.println(list);

So you can call the toString method of the list and append that to the JTextArea, or you can loop it and append each object in the list in any way you want.

In addition you can look the class JList. You can also add objects in the way you add them to an ArrayList and the toString method of each of those objects will be displayed like a list. It has a better looking …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I am not familiar with these tags:
<html:text property="employerName"/>
So I don't think that this will work:
document.getElementById('employerName').value="";
The employerName in the getElementById must be the id attribute of the text field.

Also if you want any of the javascript functions you have declared to be executed, you need to call them like you did with box function. Using events: onxxxxxx (onclick, onsubmit).

For the onsubmit, the function you call must return true or false.

function validate(){
   if (employee is empty) {
      alert("your error message");
      return false;
   }
   return true;
}

......
<html:form action="/GetCallEmployerActionPath" onsubmit="return validate();">
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Neater to use the modern Java for-each loop:

for (Object o : obj) { 
   ...

rather than the old C-style looping:

int length = Array.getLength(obj);
int i=0; // NB this is redundant!
for (i=0;i<length;i++) {
  Object o = Array.get(obj, i); 
  ...

I am old school and old habits die hard.

I am also using that in order to distinguish when to call the print or the println.
As you can see when i=0 I call the print with the extra tabs at the right
When i>0 I simply call println(o+" ");
And at the last i I use println

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What errors do you get. In the catch print the exception that you get in order to know what went wrong.
That message means nothing: System.out.println("Error in file")

catch (Exception e) {
   System.out.println("Error: "+e.getMessage());

   // or less elegant
   e.printStackTrace();
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post your code. Have you read the tutorials I gave you?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

That is the same question you asked in one of your previous threads:
http://www.daniweb.com/forums/thread305019.html

You got answers to that thread. If you didn't like the answers don't start a new thread.

In that thread we asked what a "picture puzzle" was and got no answers. Instead you created a new thread with the answer of our question. It is like you ignored our attempts to help you.

As for an idea.
Use JLabels or JButtons that display different images and have one button with a blank image. Once a button is clicked, check if it has an empty space next to it (the button with no image). If yes, swap the buttons images.

Check the JButton class API to see how to add images to a button.
Also read some tutorials about swing, on how to write ActionListeners.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hi All. This is my first code snippet. I don't know how practical it is, but I
came up with the idea, so I wanted to do it.

Assuming you have an array. You use a for loop to display its data.
For 2D arrays you use 2 for loops. But that is not dynamic. Every time an extra
dimension is added, you need to write an extra loop. How about a method that
takes as argument an Object, that is actually an array of unknown dimensions (NxD).
By using recursion the program will "generate" dynamically the right amount of
for loops needed to print all the data of the array without having to know how
many dimensions it has.

The first method (printArray(Object obj)), is only to simply demonstration how
is done. We initially check if the object is an array. If yes, we loop it and
we take each of its elements. If that is also an array, we recursively call the
same method. Else we simply print the element itself.
Now because for multidimensional arrays what is printed will not be very well
formatted, a second method was implemented:

printArray(Object obj, int level).
We first call that method with level=0 and for each recursive call we
increase the level. By doing that, we print each dimension/level of the array one extra \t character to the right. The second method prints …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I find it difficult to understand the shadowing, privacy and inheritance...
I declared a class that has an array as an attribute. I have declared it as private.
Then I created a series of classes that inherit from the former. I thought that these new classes already had the attributa as part of themselves, but I find an error at compiling time stating that I have declared the array as private. The sub-classes try to use their (I assumed) own arrray with this.theArray.someMethod(), but this is when I got the error...

Should I declare it as public or should I have to (again and again and again) code a method to pass the attribute between a parent and his children?

Yes, the child classes do have as attributes any members declared at the parent classes. The issue was that when declared private they cannot be accessed form outside the class even from their child classes.

Apart from declared public or protected, you can have get methods. With that way, they can be altered by methods in the parent class but accessed by any other class.

class A {
  private int someVar = 0;

  // in here there is code that does some stuff with the someVar variable.
  // You do not want other classes to be able to change it.

  // But in order for its value to be accessible:
  public int getSomeVar() {
     return someVar;
  }
}
class B extends A {
   public void …
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Come up with some ideas based on what you are good at and start coding. You can search this forum or the internet for project ideas

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Thank you! Yes, I did what the above, exccept, I did it this way:

coordinatesDiff[1][0] = coordinates1[1][0] - coordinates2[1][0]; //X
coordinatesDiff[1][1] = coordinates1[1][1] - coordinates2[1][1]; //Y
coordinatesDiff[1][2] = coordinates1[1][2] - coordinates2[1][2]; //Z

but how do i get the array of (9,1,3)? Please advice. Thank you!

'On top', it means when I integrate my visualizer with my java codes, I can actually see my protein structure on top of one another.

You already have that array:
coordinatesDiff[0] (X)
coordinatesDiff[1] (Y)
coordinatesDiff[2] (Z)

coordinatesDiff=
i | X Y Z
0 | 1 2 3
1 | 5 6 7
2 | 9 1 5

Example for i=1
coordinatesDiff[0] = 5 (X)
coordinatesDiff[1] = 6 (Y)
coordinatesDiff[2] = 7 (Z)

Now that you have the data in those arrays, do whatever calculation you see fit.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Use javascript, get the id or the name of the field and set its value to "" empty.

<form name="formName">
<input type="text" name="field" id="field_id" value="aaaaa" />
</form>

Then write code to execute this javascript:

document.formName.field.value="";

//OR

document.getElementById("field_id").value="";

Definitely check this: http://www.w3schools.com/default.asp for more.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I am really not sure if it will work, but I think that if an element is set to be disabled then it is not sent.
So you can try this:
Whenever you click a check box, use javascript to enable the text field attached to it. If you un-check it then disable the text field.

Try then to send and see what will be passes at the request.

If it doesn't work, then there is another way, less elegant. Let me know.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You stop when the \n is read, but with this code: while((buf[bufPos++] = (char)System.in.read()) != '\n') {} First you assign the new line in the array:
buf[bufPos++] = (char)System.in.read()
And then you check for the new line.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Call the method:

int [] array = breakUp("some string");
array[0];
array[1];
array[2];
....
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

It seems that you have messed up with the brackets.
You have this:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == writeBtn) {
        
            if (e.getSource() == libraryBtn) {

            } else if (e.getSource() == exitBtn) {

	    }

       }
}

Maybe you need this:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == writeBtn) {
             
	} else if (e.getSource() == libraryBtn) {

        } else if (e.getSource() == exitBtn) {

	}
}

Can you please verify it?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hi javaAddict, I followed your suggestion and did this:

System.out.print("Enter a password: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String passW = in.readLine();
char[] password = passW.toCharArray();

I pretty much took the string and then converted it to a char array. You were right. That's what was causing the problem!

Cheers :D

I think that the problem was that when you were reading char by char the input, you were stopping when the new line was read. That means that you were trying to generate a key from a password that contained the \n character.

Can you please verify that? Can you use your previous code and print all the elements of the buf array:

System.arraycopy(buf, 0, password, 0, bufPos);
for (int i=0;i<password.length;i++) {
  System.out.println(">"+password[i]+"<");
}

If the last elements prints nothing but only changes lines means that that was the problem.
You can use a loop to copy the elements of the buf to password:

char[] password = new char[bufPos-2];
for (int i=0;i<password.length;i++) {
  password[i] = buf[i];
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Is it so difficult to have a second loop?

for () {
  read random numbers into the array;
}

sort the array;

for (loop the sorted array) {
  output.write(list[i]+" "); // save the sorted array
}

output.close();
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Well the error says that Password is not ASCII. So maybe if you could print the values of the password array one by one and see what it has.
Perhaps the problem is that the last element of the array is the \n new line.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Yes JSP, it can be done with jsp, but do you know java? Because you can not write jsp, and especially such project without knowing good java.

Check the tutorials at the top of the java and the jsp forum.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Yes, after the loop.

First of all if you do it in the loop, the array starts like this: {0,0,0,...
Then you put the 0th element: {3,0,0,.. If you sort that 3 will go at the end of the array.
So after the sort you have: {0,0,0,......,3}. Then you will put the 1th element at the i position: {0,5,0,... If you do that you are stuck with a zero at the 0th position. Nothing will change that because you keep adding elements at the 2nd, 3rd. That zero and the other zeros are there to stay.

Also if you do the sorting in the loop you will be calling that method 100 times. Why call the sort 100 times, when you can call it once after you have the array ready.

Also the array has been sorted. There is no doubt about that. But you don't save the sorted array in the file:

for () {
  get random number;
  write to file (unsorted);
}
sort array;

The numbers saved to the file are unsorted, but the array is.

Now what do you want?
Do you want to sort the array? Do you want to save the unsorted array? Do you want to save the sorted array? Do you want to save both?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I think I can assume what the problem is. Do you do the sorting in the for loop? If yes here is what happens.

for () {
  number[i] = random;
  sort;
}

First of all when you create the array all of its elements are zero.
When you sort you rearrange the elements of the array. So when you try to add the second random number it is being put at the second position of the sorted array, not after the previous number.

Array = {0,0,0,0,0}
Adding number = {5,0,0,0,0} // i = 0
Sorting = {0,0,0,0,5}

Array = {0,0,0,0,5}
Adding number = {0,2,0,0,5} // i = 1
Sorting = {0,0,0,2,5}

Array = {0,0,0,2,5}
Adding number = {0,0,4,2,5} // i = 2
Sorting = {0,0,2,4,5}

If you keep this up, the initial values you entered will be overridden by the new ones you added:

Array = {0,0,2,4,5} // adding the next number at the i=3
Adding number = {0,0,2,8,5} // i = 3 // the 4 is gone
Sorting = {0,0,2,8,5} // and the zeros are still there


Array = {0,0,2,8,5} // adding the next number at the i=4
Adding number = {0,0,2,8,1} // i = 4 // the 5 is gone
Sorting = {0,0,1,2,8} // and the zeros are still there

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

In that case, I would assume that there is something wrong with the sort method.
Write a small program in another class:

class Test {
 public static void main(String [] args) {
   int [] array = { 
    63, 44, 55, 83, 71, 93, 91, 95, 62, 97, 12, 57, 7, 35, 50, 30, 49, 41
     /* put here the numbers you want to sort */ 
   };
   // call the sort method
   // print the array
 }
}

Check if the sort method works. Post the code of the sort method as well as the results of the above code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You need first to populate all the array and then sort. What you do is this:

for () {
 array[i] = number;
 sort;  
}

But with that you put the first number and you sort an array that has only 1 number. Then you add a second number and again you sort an almost empty array.
First finish with populating the array with all the numbers and then sort the full array.

When you say you get a lot of zeros when that happens? What is saved in the file? What is printed.

Also I think that you should write your own sorting method. But you'd better ask your teacher if you can use the Array.sort method or write your own.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When you say 'on top of' can you provide a small example.
For example if the file1 is:
(X,Y,Z) = (10,2,5) and file2 is:
(X,Y,Z) = (1,1,2).

What do you want your program to do?
You can create an new array: coordinatesDiff, and do this:

coordinatesDiff[i][0] = coordinates1[i][0] - coordinates2[i][0];
coordinatesDiff[i][1] = coordinates1[i][1] - coordinates2[i][1];
coordinatesDiff[i][2] = coordinates1[i][2] - coordinates2[i][2];

The result would be:
(X,Y,Z) = (9,1,3)

Is this what you want?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can you try to print the pbeKeySpec value and see what it has?
Also is there any specific reason that you need to read the input character by character and then copy it to another array? Can you just use the Scanner class to read the whole line and get the password?