JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

yes, affected

Presumably you will have downloaded that library and incorporated it into your deliverable, so nothing that happens on GitHub will affect it.
It it were removed I'd be more worried about (a) confirming that its licence allows you to continue to do what you are doing and (b) what will happen when that library needs to be updated for some future change in Android?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yeah. I would just move on now, and remember that Scanner nextInt and nextLine don't mix well!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's a well-known problem with Scanner's design - so many people fall into this trap.

your nextInt reads the menu number, leaving its newline character in the input buffer. The nextLine reads that and returns a zero-length String. See below for more discussion.

You have some input with an int followed by some text, eg
101
John Doe
... and you try to read it with
int num = scanner.nextInt();
String name = scanner.nextLine();
... and name is an empty String ("")!

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

Possible fixes:

  1. Add add extra nextLine() between the nextInt and the real nextLine to clear the unwanted \n. This may be difficult if the nextInt and the nextLine are in different areas of code which are not always executed together.

  2. Give up on nextInt (etc) and just read whole lines and parse then into ints (etc) with Integer.parseInt (etc) - in which case you can junk the whole scanner and use a BufferedReader instead.
    new BufferedReader(new InputStreamReader(System.in))

  3. Use this class instead of Scanner:
    https://www.daniweb.com/programming/code/506346/a-simpler-safer-alternative-to-java-util-scanner
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't know firebase, but as nobobdy else has answered....

MAYBE there's a time attached to that data, eg
30-5-2019 10am
31-5-2019 11am
and your query is defaulting to midnight, ie 30-5-2019 00:00 to 31-5-2019 00:00
???

If so the fix is to end the date range with a date one day later.

rproffitt commented: The more I thought about your reply the more likely that's it. +15
Saboor880 commented: Thanks for trying James. Actually there was need to convert the string date in date object, I did so and the problem solved +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It can be done using loops like that, but you have to do something like:

while some input remains...
  find the most frequent character (like in your code)
  add the right number of that character to the output
  remove all instances of that character from the input

A much simpler approach is

Create a Map<Character, Integer>
Popuate it with the chars in the input and their corresponding counts
Get the maps EntrySet as a List and sort it by value descending
(thats just 4 lines of code so far)

build the output string from the sorted list (this is actually the messiest part!)

Finally, if you know the input is just ASCII you can use an array[128] instead of a Map for a wonderfully simple solution (just 6 short and easy lines of code)...

create an array of 128 empty Strings
append each input char to the string at index = the char
sort the array on string length descending
append all the array members to the output

(use StringBuilders instead of Strings if performance is really important)
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's just the limited intelligence of the compiler. You and I can see that you always return a boolean, but the compiler sees that you may exit the for loop without returning a boolean. Just add a (dummy) return false; at the end and the cpmpiler will be happy.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

== and != for objects test for RH and LH expressions both referring to exactly the same object (or not).
The equals method tests that the two objects are equivalent in some sense that depends on what kind of object they are. Eg for two Strings it tests that they both comtain the same sequence of characters.
So if a refers to a String that happens to contain "" the first test will be true, but in general the second will not because they will be two different Strings that just happen to have the same characters.

What can make this a little confusing when trying it out is that Java is very smart about how it stores Strings, and it will re-use an existing String if it has a suitable one. eg

String a = "";  // creates a new ''" object
String b = "";  // re-uses the existing object
// a.equals(b)  and a==b are both true

but

String a = "";  // creates a new"" object
String b = new String("")  // creates another new "" object
// a.equals(b) is true but a==b is false
rproffitt commented: You're right. I was sleepy and know better to write as I doze off. Removing my answer. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The double quote marks in your input string are open/close double curly quotes, not simple straight double quotes, so you do not remove them.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

have resulted in errors.

What errors exactly? Help us to help you.

rproffitt commented: Yes. "Missing value on line 42" can really speed this along. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

the simplest solution is probably to call an extra nextLine() after the nextDouble()

Please read what I wrote more carefully. I said to add a nextLine(), not a println, and I explained why.

Once again:
The user's input looks something like
"Brown\n123\nSmith\n456\nJones\n789\n" etc

Now remember that nextLine gets all the text up to the newline character and discards the newline char.
But nextDouble gets all the digits that are part of a valid double, then stops. It does not discard any newline char

So in your loop you execute:
nextLine() - returns "Brown" - leaves "123\nSmith\n456\nJones\n789\n"
nextDouble() - returns 123 -leaves "\nSmith\n456\nJones\n789\n"
nextLine - returns "" - leaves "Smith\n456\nJones\n789\n"
nextDouble() - sees "S" whic is not a double. Throws an exception.

so after each nextDouble call you need a call to nextLine() to clear out the \n that was left in the input buffer.

It doesn't mater whether you start the loop by asking for the name or the score. Once the loop gets going you alternate between name and score, with the problem and solution I explained above.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Standard answer:

It's a real problem with Scanner's design - so many people fall into this trap.
You have some input alternating an int followed by some text, eg
101
John Doe
... and you try to read it with
int num = scanner.nextInt();
String name = scanner.nextLine();
... and name is an empty String ("")!

Even worse, the name is still in the input buffer so the next nextInt() call gets that and throws an exception.

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

In this particular case the simplest solution is probably to call an extra nextLine() after the nextDouble() to clear the newline from the input stream

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What do you mean by "the statistical value"? If it's "average, std deviation etc" then you can't alter them except by altering the random numbers. Eg adding .7 to each value will increase the average by .7

Or, are you looking to convert uniformly distributed random numbers into a set that follows the standard bell curve? If so Google "random uniform to normal distribution" (but the easiest approximation is to add together 12 randoms (0-1) and subtract 6... an implementation of the Central Limit theorem)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yup. I thought everyone except corporate support sites were already on OpenJDK?

Also new in 11...
JavaFX is unbundled. Good luck trying to find easy instructions on how to re-integrate it into your Jave dev environment after downloading it.
You can now compile and run a single file source with one command, eg. Java mycode.java. (Good for beginners)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes.
It's hard to get excited by this release. Its only significance is that it's a LTS release that finalises and fixes the (minor) goodies that were introduced in 9 and 10.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thanks for the thanks!

Write an efficient algorithm ...

I posted that code to show what was wrong with your original loop, but that solution is NOT an efficient one. Consider it a starting point maybe, but to meet the requirement you will need to research some of the ways in which this can be made much more efficient for large arrays. It's a stadard question, so you will find lots of discussion aboit it with a simple Google.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, it finds the minimum value NOT in the array!

ps: If I was doing that for real I would consider including a set of redundant parentheses for the while loop to make it really clear what was intended, ie

 int solution(int[] A) {
        int n = 0;
        while (contains(A, ++n)){}
        return n;
    }

(although the two versions are exactly equivalent)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

To be honest I don't understand what you intended with that loop anyway.
All you need to make it work is

    int solution(int[] A) {
        int n = 0;
        while (contains(A, ++n));
        return n;
    }

... but that's the simplest solution, not an efficient one - it's O(N^2) and you can get O(NlogN) by pre-sorting the array, and O(N) by cunning stuff with a second array. ;)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, it does, but then you go back round the loop and lines 7-11 set it back to the lowest value in the array every time

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your do/while will loop forever because the body always sets the same value for minValue and therefore the while clause always returns the same value.
(A couple of quick print statements would have revealed this immediately.)
If its stil not clear, add some prints so you can see what's going on.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't think your problem is with code syntax, it's with the overall design/flow of the app.

Let's try to understand that... (stop me when I go wrong here)

You have a FeedReader class. An instance of that runs via a repeating scheduler to read some web info. When it finds the right info it creates an instance of Article - a class with variables like img, url etc - and adds it to a List.
There's a problem here because the List is a local variable that immediately goes out of scope and gets garbage collected.

You also have a method to add the same info to a database, but for some reason that does not use the Article instance that you created. Maybe that's the problem?

ps: all those set methods in Article are almost certainly a mistake. Why isn't Article immutable?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I still can't see where you expect to get the initial values of img, url etc from. Is it from the // some logic to parse method? (if so, then the constructor is wrong and you don't need to pass in those parameters)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

To run that code you need values for img, url etc. Where do those values come from? Eg: should the user type them in?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You need to loop through that array processing one line at a time. Each line can be exploded into the 3 words so you can put them all back together in the desired order with the desired punctuation.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You could use a strategy like
Remove all the blanks. Remove the first and last paren.
Split on ),( to give you each block of 3 words
For each block of 3 words: split on comma
Now you have just the words, grouped in threes, so it’s easy to concatenate them in the right order with the desired brackets etc

Alternatively there’s bound to be a single regex that does it, if you like long incomprehensible undebuggable strings of bizarre character sequences.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are you missing a ] in the converted version, or is that deliberate?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The obvious JavaFX way to display a ResultSet is a TableView. You will find lots of examples with a quick Google, such as this one from GitHub
https://github.com/seifallah/Dynamic-TableView--Java-Fx-2.0-/blob/master/DynamicTable.java

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Not sure exactly what behaviour you want, but if you set the editor but leave the default renderer then the combo box only shows when a cell is being edited and all the other cells just show their current value. That would be a normal behaviour in my opinion.

ps. Not dumb at all. We've all done stuff like that. Fortunately you could harness the power of castle DaniWeb.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, I've loaded and run your code.
The problem definitely seems to be in the shared renderer.
If you simply comment out the line (~135) that sets the renderer then the table uses a default text field renderer and everything works correctly.

When swing refreshes the table it uses your renderer (of which there is only one instance) to paint all the cells in col 0 that are not currently being edited. That renderer paints exactly the same thing into each of those cells.
You need the renderer to use the actual value from the table model, which is passed into the getTableCellRendererComponent method, but ignored in your code.

Just for fun I added a setSelectedItem(value);call into that method. That way the combo box has the right item selected when the renderer is called for each cell.

However it does not work when the cell hasn't had a value set yet. In that case you just get the value left over from the previous call, so to make it work you need to initialise each cell to a valid value from the combo box when the new row is added.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Renderer does not use latest value from model.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, I don't have the time to set up a multi-file project to try that...
Can you strip it down to a single small file that demonstrates the problem?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are you saying that the missing line was the fix for the problem (looks like it should be!), or do youy still have the problem with that line in place?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

At a quick look it seems that all the cells in Col 0 share exactly the same renderer and editor , so they should all look exactly the same all the time!

rproffitt commented: That's how clones are made! +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

How much RAM (main memory) do you have?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Congratulations on a really interesting question!

maybe what’s going on is in the String.format you process a l-r string, then a r-l and after that the direction is still r-l when the third argument is processed.

There are Unicode chars to override the direction, but more than that I don’t know. They are

left-to-right mark: 0x200e
right-to-left mark: 0x200f

I think you insert them in the string, but I don’t know where exactly. Maybe that’s enough info for you to find more details?

Cheers
JC

stephen84s commented: Yea this is one I've never seen before +11
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There is no universal standard for writing pseudo-code. It's up to you to chose whatever words you like to explain the algorithm or process you are trying to document. Use brackets where they help make things clear.
A lot depends on the level of detail... if you are explaining some technical detailed thing (eg compressing audio data into an mp3) the you will probably be careful about showing all the exact scope of loops and if tests. If you are explaining a business process that going to be automated, then a much higher-level syntax would be appropriate.

In this case you are getting hair color from a photo. You don't need to explain how to do that yet, but it helps if you show what kind of detail you need... just quick blond/brown/black categorisation, or much finer detail... what about people with streaks of a different color... etc By saying one of black/brown/blondyou are telling the reader all they need to know for the moment. Exactly how you say that doesn't matter as long as its clear to the reader.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Definitely better.
Pseudo-cde is for explaining algorithms, flow of control etc, so you don't need quite so much detail, and you can make up you own "syntax", eg:

GET passenger photo 
get eye color from photo (one of blue. brown, green, unknown)
get hair color from photo (one of etc
if eye color and hair color both match the id then passenger is matched, otherwize passenger is denied

details like switch don't belong here unless you are making a point about exactly how to get the color.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You create the new balance in newBalance, but then you ignore that.
Either just update Balance directly, or copy newBalance to Balance at the end of the transaction.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The "standard" solution is to parse the expression into RPN form using a stack, eg see http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Did you try debugging by printing the value of retVal at line 10?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This request is bizarre. You want to type French characters, but you don't want a French keyboard? So you want o create your own custom keyboard that... what? has a US layout for alphabetics but puts accented characters?... where?
I really don't understand why you can't simply install a standard French keyboard to type in French. And what do you mean by "completely messes up my keys unusably". I switch between UK and French all the time and my keys aren't messed up.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

First a quick point...
You never need toString() in a print because print does that anyway. And if you just want to print the value you can use a simple method reference like I showed in the previous post.

customer1.ifPresent(System.out::println);

Anyway... to print the value if present and some other string if it's not, just use orElse eg

System.out.println(customer2.orElse(new Customer("Unknown", "Unknown"))); 

(if you're going to do that more than once you could create a static final constant for the "unknown" customer, eg

static final Customer N_A = new Customer(){
  public String toString(){return "N/A";}
};

then it's just

System.out.println(customer2.orElse(N_A));
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's not how Optional should be used. The Optional variable itself should never be set to null. Use Optional.empty() to create an Optional that has no value. Ie line 8 should be

Optional<Customer> customer2 = Optional.empty();

PS: The way you are using isPresent() is perfectly legal, but not recommended. It doesn't really gain you much compared to the traditional use of a null value. The recommended use is with a lambda, ie replace lines 9-1 with something like

customer1.ifPresent(System.out::println);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your mistake is that class RaceTest is inside another class. Move it out so it’s a top-level class, then you can make your main method static. You will then need to run with RaceTest as the class that you execute and put each public top-level class in its own .java file

If that seems complicated then forget the RaceTest class and put the main method in class Project2_1

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Ok, just think of it like this.
You have a method that gives you a random int in a range starting at 0
You want to start at 1
That’s just 1+random int 0-9
You know how to get a random int in the range 0-9, and I’m quite certain you know how to add 1
:)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That code will give you a random int in the range 0-10 (inclusive). Is that what you want?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Im not sure where this is accually leading

Doen't matter. Sometimes it's just fun and educational to set off in some direction and see where it leads you and what you learn along the way.
Have you thought about all the things that are common to balls and rectangles (the way they move, how they can be controlled with the keyboard and mouse, etc.) - do they look like subclasses of some abstract superclass that models a thing that moves about, can be controlled, collides with other such things etc? Just a thought.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi Jonathan - it sounds like things are going really well. How are you doing for structuring all this code?
ps Why the new user ID?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hold fire... I have a better idea..

Imagine the rect. Now draw an extended shape around it that's distance r from the rect (where r is the radius of the circle). That shape has 4 straight segments and 4 ninety degree arc segments.
At the point of contact the center of the circle is on exactly one of those 4+4 segments.

Now draw the lines segment joining the position of the center of the circle at the previous time step with its position now.

If there was a collision then that line will intersect one of the 4+4 segments, and which one tells you which side/corner it collided with.

LIne2D has an intersects method that tells if two straight line segments intersect.
Line2D has a ptSegDist method that gives you the closest distance between a line segment and a point. If the point is the center of a circle and the distance is less than the radius then the segment intersects the circle. Voila! The Java API has al the geometric methods we need!

Just one possible complication: depending on the speeds and sizes and the size of the time step, the circle's center could enter and leave the extended shape in one tick. That case gives you two intersections, and you have to use the velocity to see which was the entry point. A little tedious set of if tests, but not in any way hard.

Summary: This algorithm should be exactly correct for all possible values, …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm worried that none of these algorithms seems to consider the velocity of the circle.
Eg
At time t the circle overlaps the top left corner of the rectangle, which means at some time betweeen t-1 and t it made first contact.
Depending on which way it was moving it could have first contacted the top (eg moving down & left) or the left side (eg moving up & right) or the corner (eg moving down & right). AFAIK there's no possibility of .determining which it was by just considering the position at some time after first contact.

I tried to distinguish these by considering the velocity and also the position of the circle centre relative to the rectangle. The results look OK to the naked eye, but I have little confidence that this is really correct for all angles of approach.

My next idea is to try to go back and find the exact moment of first contact. At that moment there is no ambiguity and just testing for circle touches point or line is enough. We know position at time t, we have the velocity, so we know the position t-1. A quich binary search of the times between those should get the first contact to within (say) 1/10 pixel very quickly...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The great thing about Shape is the Ellipses, Rectangles etc are all Shapes, so the only draw method you need is draw(Shape). So leverage the classes that Java gives you and define all your movable things ("Sprites" is the jargon) as being or having a Shape and they can all share (inherit) the same simple drawing method.