BestJewSinceJC 700 Posting Maven

Hi BJSJC
The square root answer is the correct one. If "half the possible prime" is a factor, then so will 2 be - which has already been checked. Ditto 1/3 the possible prime, which is only a factor is 3 is (ditto). Once you go bigger than the square root then the other factor must be less than the square root, all of which values have been checked.

*blush*

Of course, you're correct, though it eluded me when I posted earlier. I'm leaving my original post intact so as to not make things even more confusing. Thanks for clearing that up James.

BestJewSinceJC 700 Posting Maven

please suggest, do not disgrade or disrespect others work....
atleast the person tried to solve and solved. if you had something to edit , could have mentioned , why to go harsh..

I think you meant do not "degrade". And they aren't being harsh, they are explaining the difference between being handed the answer and learning how to solve the problem.

1. Using an optimized method by not checking whether the number is divisible till one less than number,instead using the concept that is number is prime,it will nt be divible by any numbers between 2 and square root of the number.

No, a prime number is divisible only by one and itself. So the OP must check all numbers between 2 and half of the "possible prime number" to see if any of those are divisible. There may be other tricks that can be used, but that's the general idea.

BestJewSinceJC 700 Posting Maven

What happens if you execute those SQL statements from within SQL Server itself; does anything get updated? Are the results what you expect? Alternatively, have you tried running the C# code and using SQL Server Profiler to see what SQL statements get executed on the Server (you can run profiler from within SQL Server management studio)? Sometimes it is your SQL that is incorrect and not the C# code.

Example of using multiple sql statements in one query
http://www.java2s.com/Code/CSharp/Database-ADO.net/ExecutemultipleSQLstatementsusingaSqlCommandobject.htm

BestJewSinceJC 700 Posting Maven

Details. Code you have so far, specific questions, etc

BestJewSinceJC 700 Posting Maven

So do you need any more help? If not mark the thread as solved

BestJewSinceJC 700 Posting Maven

invokeLater() probably has nothing to do with the problem you are describing. You didn't really explain what you mean by "resuming the program after you go through all the MouseListeners". What does it mean to "go through all the MouseListeners"? MouseListener is an interface; it has several methods that respond to mouse events. Are you waiting until each of those methods gets called, then continuing your program afterwards? If so, you may want to declare some boolean variables that get set to "true" from within the corresponding MouseListener method. Once all of those variables are true, you can "continue your program" by calling a method from within the MouseListener method.

For example:

void mouseClicked(MouseEvent e){
if (all mouse methods have been called){
continueMyProgram();
}

void continueMyProgram(){
//Put the rest of your program's code here!!!

}
}

References: http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

BestJewSinceJC 700 Posting Maven

Use the Scanner class. There are tons of different ways you can do this with the methods in there, some involving regular expressions/Pattern, some with only the basic Scanner methods.

BestJewSinceJC 700 Posting Maven

isEmpty and isFull should be easy. Maintain a variable for the size. Whenever you enqueue, add 1 to that variable. Whenever you dequeue, subtract 1. isEmpty and isFull just depend on the value of that variable. For the actual enqueue operation, maintain the head of the queue as the index 0 and the tail of the queue as the last index. Then check the size variable I previously mentioned.

Example to help you get on the right train of thought for enqueue...

-- starting values --
queue[0] = something, queue size = 1
-- do an enqueue operation for the new element = 2 --
queue[0] = something, queue size = 1, queue[size] = queue[1] = 2

Hopefully that will help you for dequeue as well. Remember that when dequeueing, you need to shift the values in the array one place to the left (if you are using array[0] as the head) and you need to maintain the right queue size.

BestJewSinceJC 700 Posting Maven

To add to jon's reply, look into

Auto boxing and unboxing (example: Integer i = 5; will result in auto boxing) and especially look into inheritance. Don't sweat specific examples, such as your array example, until you have a thorough understanding of inheritance and the is-a relationship. Since Character 'is-a(n)' Object, that is why you can put a Character (or any other java class, since they all inherit directly or indirectly from Object) into an Object array.

BestJewSinceJC 700 Posting Maven

No, you do not have to read it as a String - there are Java library methods that will allow you to read other types from a file. However, that is ignoring the point - that your code, the way you have it now, will work with very minor changes.

Read it in as a String, like you're doing now. Then use the Double.parseDouble() method that I mentioned in my first post to convert the String you just read in into a Double. Then put that Double into your ArrayList<Double> using the ArrayList class's add method.

BestJewSinceJC 700 Posting Maven

The <Double> means you can only put that type into the ArrayList. So if you try to put a String into the ArrayList, then yes, you'll get an error. If that doesn't help, you'll need to post your exact error.

BestJewSinceJC 700 Posting Maven

Using ArrayList as a raw type isn't a good idea. You can use Java generics to enforce type checking. In other words you can change it to ArrayList<Double> list = new ArrayList<Double>(); in order to allow only Doubles in the list.


That being said, you can use the Double class's parseDouble method to parse a Double from a String. For example, Double result = Double.parseDouble(myString); . You'll need to be careful because an Exception will be generated if the 'myString' variable cannot be converted to a Double, so you will need to use a try/catch and deal with the condition where it can't be parsed appropriately.

The code you posted before would allow you to insert any type of Object into your ArrayList. However, as I've indicated, it is a better idea to keep the list of one type (Double in this case). Then you can use an add() method which takes a String parameter to add Doubles to your list. For example:

String myString = reader.getValueFromUser();

public boolean add(String str)
{
   try {
     //parse the Double in here and add to your list
   } catch(Exception e){
     //do something else here, probably just return 'false'
   }

}

Hope that helps. If any of the above didn't make sense, feel free to ask questions, but you should also check out Java Generics, the Double class documentation, and Exceptions.

BestJewSinceJC 700 Posting Maven

My inclination would be to have a class variable(s) that track the Mouse position. The way you could do this is by setting up the variables to track x, y position and in the MouseMoved method, first you'd update those variable, then you'd call repaint() which causes paint() to be called. In the paint() method you could then utilize these variables. As for detecting when the mouse moves off screen, use the MouseListener interface, specifically the mouseExited method. Combined with some simple if statements in your paint method, this should be all you need.

It's been a long time since I've coded Java ("coding" sharepoint now), so hopefully this was good advice.

BestJewSinceJC 700 Posting Maven

Can you explain your reason for putting Integers and Characters in the same array? Also why you'd want a "DoubleStack" ?

BestJewSinceJC 700 Posting Maven

What you did by calling replaceAll is construct a new String with all of the whitespace removed. The returned String was smaller than your original String, hence when you used the original index, it was larger than the size of the String. In my first post I gave a suggestion: use replaceAll (like you used it) to remove the whitespace. Then use a loop to check the strings to see if they're palindromes. Simple if statements can ignore punctuation ..

BestJewSinceJC 700 Posting Maven

In your new String, either "LHS" or "RHS" represents an Index which is no longer valid. Since replacing whitespace with "" makes your string shorter, your index is no longer valid in the new string. In your new string, which is shorter than the old string, more than likely the "RHS" is greater than the highest index

edit:

to add to that, the above is the reason why you should really be making a custom method which simply goes character by character through your string and ignores whitespace. The replaceAll technique isn't bad, but after you do that, you should use a for loop to check the two strings to see if they're the same.

BestJewSinceJC 700 Posting Maven

If you're in a method, which you have to be, you could say "return;". I wouldn't worry about it too much if you have a working method though.

BestJewSinceJC 700 Posting Maven

Use an if statement.

boolean firstGuess = true;
long totalTimeMillis = 0;
if (firstGuess){
...
}

That should get you started

BestJewSinceJC 700 Posting Maven

Why would you declare "k" as type float? It should be declared as an int. Also, the ";" at the end of the for loop ends the loop. In other words, the for loop does not apply to the line directly after it since the ";" is there. Repost the code and tell us what errors (if any) you get.

BestJewSinceJC 700 Posting Maven

Because arrays get special treatment as per the specs:

Why is that special treatment? Because they actually overrode clone..? Do they not usually do that?

edit: jumping ahead of myself, trying to catch you before you got out of the thread.

BestJewSinceJC 700 Posting Maven

ob is an array which is also treated as an object, and hence has a default clone method which is called at (1). But the default clone method returns an Object reference, so that should not be assignable to an array of type hello. But its being assigned here to an array of hello type without errors. Why ?

An array is-an Object and therefore you can call methods of the Object class on an array, which inherits these methods. However, as you can tell by writing a short program, the array class overrides the clone method. Therefore when you call clone() on an array, it does not simply return the same Object reference, but it actually creates another array.

Hello ob[]={new Hello(),new Hello(),new Hello()};
Hello ob1[] = ob.clone();
if (ob == ob1) System.out.println("Same");
else System.out.println("Not the same");

The above code prints not the same, which to me, indicates that a new array Object has been created. The exact details of what the clone() method does for the array class I don't know, or really care. You will note, however, that if you print out

if (ob[0] == ob1[0]) System.out.println("ob the same");

it will indeed print out that they're the same, which is because in that case, your Hello class does not override clone, so it just returns the reference.

BestJewSinceJC 700 Posting Maven

Changing the variable "size" in the Vizatuesi method has no affect on the value of the variable size that was originally declared in the paintComponent method. In fact, the 'size' variable in the paintComponent method and the 'size' variable in the Vizatuesi method are two completely different variables. When the Vizatuesi method is called, a variable called 'size' is created on the stack. This variable is completely separate from the size variable back in paintComponent.

bibiki commented: it's not the first time that BestJew answers my question right. +0
BestJewSinceJC 700 Posting Maven

It can't hurt so when in doubt, do it. As far as I know the only possible consequence is that no other file will be able to open the file for writing until you close the FileWriter, but I also suspect that garbage collection might cause the FileWriter to relinquish its resources as part of the garbage collection process anyway. Again, those are my suspicions, do not take them as fact. But do know that only one thread at a time can have a file open for writing, so if your FileWriter is not closed, you haven't given that resource back to the OS, and as a result, nobody else can write to the file while it is open.

BestJewSinceJC 700 Posting Maven

I should have used the sarcasm tags. :idea:

~s.o.s~ commented: LOL? :) +0
BestJewSinceJC 700 Posting Maven

You're such a sweetheart Nick.

BestJewSinceJC 700 Posting Maven

Ancient Dragon wasn't trying to be rude, just point out the distinction since in your previous post it seems like you didn't understand. Either way, as I said, there have been a large number of posts about mobile development, but if there was a forum for it, it would not have good traffic. The questions about mobile development only get posted a few times a month.

BestJewSinceJC 700 Posting Maven

Plenty of people have posted various questions in the Java forum about developing for mobile devices .. Peter Budo usually answers those questions. But I do think it is a bit too specific for Daniweb. Those kinds of devices usually have their own communities dedicated to questions about development.

BestJewSinceJC 700 Posting Maven

Agreed - I'm pretty sure Applets use the init method to start, not main. Your "Applet" also contains standard Java code that would print to the console, whereas it should get input via a GUI.

And use JApplet not Applet
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html

BestJewSinceJC 700 Posting Maven

This is incredibly insulting. Do you honestly think we are this stupid? Please, do yourself a favor and don't post here again.

Also, regardless of my level of rust, I'm sure I could handle this joke of a program. You seriously can't do it yourself?

camdaddy09 commented: rude +0
Nick Evan commented: Rude? Nah.True? Yup. +12
BestJewSinceJC 700 Posting Maven
Your code goes here


Do that. Clicking the "#" button will do it automatically for you.

BestJewSinceJC 700 Posting Maven
import java.util.Scanner;
import java.util.InputMismatchException;

public class TourPrices
{
   public static void main(String args[])
   {

   Scanner scan = new Scanner(System.in);
   int tourPrice = 0;
   boolean ok = false;

   do
   {
      try
      {
         System.out.println("Enter a score");
         tourPrice = scan.nextInt();
         if(tourPrice >= 29.95 && tourPrice <= 249.99)
         ok = true;
      }
      catch(InputMismatchException e)
      {
	    scan.nextLine();
       }
   }
   while(!ok);
      System.out.println("The valid price is " + tourPrice);

   }	
}

...

BestJewSinceJC 700 Posting Maven
BestJewSinceJC 700 Posting Maven

<<< Study how object are initialized.

The best suggestion yet. As was said previously, you cannot call a method without an Object of the corresponding class. The only exception to this rule is when the method is declared as 'static'. You need to understand that there is a distinction between declaring and initializing. You declared an array of "n" College Objects. All this does is tell Java to create an empty container that can hold "n" Objects. To initialize a variable means to put something into it. So to initialize your array would mean to fill the slots of the array with College Objects. Initially, your declaration of College[] array = new College[n]; causes Java to create an array of size n with the value "null" in each slot of the array. Attempting to reference (call a method on) null gives you a NullPointerException, hence your error initially. If this info is a lot right now, that's fair, just remember that you must say "new Object()" before you are allowed to call a method on an index of an array or on any variable. It may help if you think about an array as a collection of variables of the same type (such as College), and then think of a variable as only being able to call methods after you say yourVariable = new College().

BestJewSinceJC 700 Posting Maven

You declared an array of "n" College Objects, but you did not initialize it. You must create a new Object at each index before you can call methods on it. Also, post a regular thread in the future, not a code snippet.

BestJewSinceJC 700 Posting Maven

Use methods. Your example and explanation are convoluted and I can't give more specific advice, but to "do something" under certain conditions (i.e. in a control structure such as a for loop, if statement, etc) you can call a method.

BestJewSinceJC 700 Posting Maven

Be clear with your questions, instanceof is a specific java statement that can be used (for example) in the following way:

if (x instanceof y){
   System.out.println("Cool");
} else System.out.println("No runtime error or compiler error here.");

And btw, I did understand your question after I read the quote, I'm just saying to avoid confusing language. Anyway, you can tell what kind of error it will produce by designing a program and attempting to run it. If it won't run it is a compiler error. If it runs but crashes its a runtime error. But with a little knowledge of inheritance you can probably deduce that if you try to instantiate one type (for example, class Whatever) and do something like Whatever c = new OtherThing .. where OtherThing is not a child of Whatever in the inheritance hierarchy, then it should generate a compiler error. Again, one test program is worth a thousand expert opinions, but read about inheritance and you will begin to understand. I could point out one by one what happens in your example above, but that won't help you learn nearly as much as writing a test program and seeing what an actual compiler does after reading a little bit about Java inheritance.

wildplace commented: helps a lot! +1
BestJewSinceJC 700 Posting Maven

This problem is teaching you about method overriding and how it is an important part of Inheritance. For example, your baseball's play() method should print "Playing America's past time" to the user, the Basketball class's play() method could print out "Playing Basketball", etc. The same goes for the bounce() method - certain types of balls cannot bounce, so they would just implement the bounce() method so that it says "I don't bounce()!" ...

BestJewSinceJC 700 Posting Maven

As amusing and ironic as I find this, I don't think

"500 Internal Server Error

Servlet processing error


java.lang.NullPointerException
at com.thruport.jbird.core.JBirdFilterChain.doFilter(JBirdFilterChain.java:36)
at com.thruport.jbird.core.JBirdContext.processRequest(JBirdContext.java:1989)
at com.thruport.jbird.container.JBirdRequestHandler.execute(JBirdRequestHandler.java:159)
at com.thruport.jbird.server.TcpConnectionHandler.run(TcpConnectionHandler.java:56)
at java.lang.Thread.run(Thread.java:619)"

Should be at the top of the Java forum.

BestJewSinceJC 700 Posting Maven

Public should be "public"

BestJewSinceJC 700 Posting Maven

validation rules are invalid, solution rejected.

Yeah, but the validation rules for email addresses are quite complicated and I doubt the OP has made up the rules posted above, they are created by his teacher.

BestJewSinceJC 700 Posting Maven

You have to create a new Object of that classtype i.e.

dataClass dc = new dataClass();

BestJewSinceJC 700 Posting Maven

Btw, I am very curious as to why label == "1" actually returned true. Perhaps the JVM somehow knew that the String Object "1" had already been created so it pooled them together, therefore resulting in the addresses being the same. But if that is actually the case, I doubt you realized it, hence what I pointed out above.

BestJewSinceJC 700 Posting Maven

You never initialized the instance variable "lbl" which is declared on line 14 above. At line 21, you declared and initialized a different variable that is also called "lbl". But inside your actionPerformed method, the "lbl" that it is trying to call setText() on is the "lbl" that you declared at line 14, but never initialized (using the new keyword).

if(label == "1")

What you just did in the code above is compared the reference (memory addresses) of label and "1". If you want to check if any two Objects (and String is an Object) are logically equal, then you need to call the equals() method:

if(label.equals("1"))
BestJewSinceJC 700 Posting Maven

In addition to what moutanna said, you should be wary of bad user input. For example, before you call nextDouble() you should verify that there is actually a double present in the input by calling hasNextDouble(), which returns true if there is a double that can be read in. Oh, and the user could also enter something like "a whatever" and your program would read in the letter, then the nextDouble() call would choke on the next token of input, which is 'whatever' and you would get an InputMismatchException.

BestJewSinceJC 700 Posting Maven

Moutanna's suggestion was a good one; break in this case would be a good idea. If you don't want to use break, though, you can use a for loop and make one of the conditions a boolean. Set that boolean to true once you find the value so it will end the loop.

for (int i = 0; i < size && !finished; i++){
   if (youFindTheMinimumRating) finished = true;
}

There are a ton of other ways to do this..

BestJewSinceJC 700 Posting Maven

You should set your initial lowest value to array[0], otherwise the lowest value variable will probably be initialized to 0, therefore, it will tell you the lowest value is 0 at the end.

flyingcurry commented: Thanks! +0
BestJewSinceJC 700 Posting Maven

Avoid giving people fully working code. Give them tips that will help them learn and occasionally bits of code.

BestJewSinceJC 700 Posting Maven

Search the ArrayList for the name. Use the indexOf method. Then edit the Object at the index that is returned, and edit the phone number at the same index in the other ArrayList.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

BestJewSinceJC 700 Posting Maven

When do you want to check to make sure none of the fields are empty? When the 'add' button is clicked? You need to give more information. Regardless, you'd use an if statement.

if (buttonName.getText().equals("") || otherButton.getText().equals("")) //do something, your button's text was empty!
BestJewSinceJC 700 Posting Maven