Campbell Ritchie

+ Follow
since Oct 13, 2005
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Campbell Ritchie

sohail hussain wrote:. . . if you create string literal it is stored in string literal pool, so again if you create the same literal object . . .

But the OP isn't writing the same String literal twice. It doesn't matter whether you use the String pool or not.
What matters is that the assignment operator takes the value on its right and applies it to the variable on its left. I think OP is getting confused and thinks it takes the contents of the variable on its left and applies that to the expression on its right, but it doesn't.

Please don't simply quote the whole of a preceding post, but only the relevant parts. I have removed some of the quote.
4 hours ago

Norm Radder wrote:Also posted here . . .

Please always tell people on each website if you post on multiple sites.
4 hours ago
No, I don't think the String pool has anything to do with why the two are different. It has to do with assignment, as BG said. You are assigning the two references in that order, so that is what they will contain. The assignment in line 5 is applied before that in line 6 and the two are independent.Why did you expect to have both references pointing to the same String in line 7?
6 hours ago
Check the books out carefully to see how much Java11 they contain.
Cert books are good, but they are not tutorials. There are lots of books covering the gaps between Java8 and Java11. Have a look at Modern Java in Action (Urma Fusco and Mycroft, Manning) or Mala Gupta's introduction to Java11 book (Packt).
Is there anything useful in the Java™ Tutorials? Try the section about creating a .jar file.
7 hours ago
MP: welcome to the Ranch

I think your suggestion will work, but I am afraid it is by no means an object‑oriented solution.
7 hours ago
Please search this forum; there are new Java11 cert exam FAQs, but we still know little about the Java11 exam.
Please use the code button; yu already know how it works.

arwa ali wrote:below the code that I tried so far but it is not working well  . . .

That makes it look as if you were guessing; you can guess 1,000,000 times and some of the guesses will be correct. Or you can think about the problem and get it right first time.
What are you doing in the code in your second post? It looks as though you were writing a list to the text file and then trying to alter the text to get it back to its original format. That is only making things difficult for you. I suggest you need to get out of the world of Strings as soon as possible.
I also suggest you need a class to encapsulate those data. I think a simple double[] isn't sufficient, and a List<Double> is hardly an improvement, least of all if you call it array. I presume the other peculiar names, e.g. kk, are only there to obscure private details of your code. You can give your class a method to return the last element, which you can use in the comparingDouble() calls Rob showed.

Let's imagine that you have a text file like this:-That doesn't contain doubles, but text. There are various ways to read that:-
  • 1: That is a csv file, and there are libraries for reading csv files. You should use one of them unless this is a class exercise and you have been told to do your own file reading.
  • 2: It appears that the lines contain eight numbers each. In which case you can use a Scanner and nextDouble() eight times for each instance you are creating. You will ned a different delimiter; maybe ",\\s*|\\s+".
  • 3: You can read each line and pass it to a Scanner constructor for further processing, or you can use line.split(). Maybe ",\\s*" would work as a delimiter or a regex to split on.
  • Some of my suggestions might work and some might not.
    Don't close anything reading or writing to files; use try with resources instead.
    8 hours ago
    Welcome to the Ranch

    James Mac wrote:. . . to replace a key you need to first remove the key that is going to be replace and put a new one. But that doesn't save the position of the removed key and value. . . .

    No, you are correct. A hash map doesn't have any concept about the order of its K‑V pairs. So you are going to have to use something different if you want to retain insertion order and replace a K in the same position as its predecessor was. Remember that if your map already contains that K, calling put() a V against that K, you will have something completely different happen (I think you know that already).
  • I suggest you go back to the Java™ Tutorials and look at the two pages about Maps, and remind yourself of their capabilities, If I haven't made a mistake about your requirements, I don't think any of the types shown there will do. I think, if you put() anything with a new K into a linked hash map, the implementation will perceive that as a new K and put that last in the accompanying list, so it will be out of order.
  • I suggest you try searching for Apache Commons Bidirectional Map or Google Guava Bidirectional Map; maybe a bidirectional map will allow you to change the K on its own.
  • I suggest you consider a plain simple hash map, accompanied by a list for your Ks. Whenever you are altering a K, you can find it in the accompanying List and call set() on that List. I suggest you find out about the performance of that technique if you have a large map (maybe 1,000,000 pairs)
  • I suggest you consider two maps, one being a Mao<String, Long> where the V represents the insertion position. Again find out about the likely performance of such a second map with a large collection.
  • I did wonder whether you could write your own map implementation whereby you can change a K directly, but I think that won't work.
  • I suggest you will learn quite a lot when you read all that lot.
    Find out about ways to copy maps; you will probably find that most implementations have copy constructors. Much better to use such a constructor that trying copying with clear() and addAll().
    There are all sorts of things in your code which could be improved; I hven't got the time to tell you about everything, but there are two things I shall mention:-
  • 1: You shouldn't create a new Scanner to read from for each method call. You only need one Scanner reading for the whole application.
  • 2: It is usually an error to make something static without a good explanation. I think you should have multiple classes and shorten your main method.
  • Well done producing an interesting and unusual question
    9 hours ago
    Don't call classes the same as types supplied by the JRE, least of all if their names are the same as types in the java.lang package. That will store up trouble for you in months to come.
    You aren't overriding any of those methods, but overloading them. The subclass inherits methods from the superclass, but they are the same methods as the superclass' methods. Since you declared the variable with the superclass' type, the subclass' overloaded methods aren't even visible to the compiler. In lines 6 and 8, you are getting promotion from int to double and calls to the methods from the declared type.
    1 day ago
    Have another cow for persistent generosity with those resources
    Please explain that formula a little more. How can you have a probability > 2?
    Did you say Enemy class or Enemies? The usual convention is to use singular words (Enemy/Object) and reserve the plurals (Enemies/Objects) for utility classes. But don't call anything Object or Objects, becuase classes with those names already exist. You might have an Enemy class with subclasses like Kilngon, Dalek and Skrane, in which case you can use those as parameters to your attack() method.
    1 day ago