Luigi Plinge

Ranch Hand
+ Follow
since Jan 06, 2011
Luigi likes ...
Scala IntelliJ IDE Windows
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
39
Received in last 30 days
0
Total given
23
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Luigi Plinge

I'd start again if I were you. Shouldn't take too long since you can copy some of what you've already written. Just look at the places that are different in the two methods you have already, and replace with variables that you pass to the method: the upper and lower seat numbers, section name, etc. The question doesn't specify what to do if both sections are full, but you might want to add a boolean parameter to indicate whether the other section has already been checked.
10 years ago
It's not bad. The biggest problem here is the code duplication between firstClassSeat() and economySeat(). If you find yourself copy-and-pasting, you're doing something wrong. You should combine these into one `bookSeat` method, taking a parameter to indicate class. You can use an `int` for the class of seat, but in real life it would be better to use an enumeration.

Your system for checking if each section is fully booked doesn't seem very watertight (although it might be OK in this toy example). You know it's fully booked if you've been through and not found a seat, not by checking if one particular seat is full. You could use a boolean flag, something like `seatAssigned` that starts as false, and gets set to true when you assign a seat. If it's still false at the end of the loop, you know the section was full. You can also use this flag in the for-loop condition, which allows you to avoid `break` (which is bad style, because it provides multiple exit points making code harder to follow). Better, you could break out the search into a separate method, taking two ints for the range, which allows you to re-use it. Something like

Have a go at fixing these. After you've done that, you could try separating your input/output (I/O) logic from the business logic. Your methods should only really do 1 thing each, and be just a few lines long at most. Here, you've mixed `println` methods in with logic for determining the outcome and assigning seats. Hint: have your methods return values based on what happened, rather than using `void`.
10 years ago
I don't know where you have seen "synchronized" used, but your code is just fine. You can also make the instance a "public static final" field, which is simpler.

But the preferred way of creating a singleton, according to Effective Java, is to use an enum:
11 years ago
It doesn't stop because after you used `nextInt` it's still working on the same line of input. `nextLine` just takes the rest of that line of input up to the line end. Try adding another `nextLine`.

If you want your values rounded down to ints you can do a cast, i.e. `e.g. int i = (int) 1.1;`(add 0.5 beforehand if you want to round to the nearest int), but in genereal it's better to keep the accurate value and simply display it to the required precision using `printf` or `String.format`. In other words you need to store the values as doubles.
12 years ago
1) Calculations:

In Java, if you divide one integer by another, you get another integer. Integers are always rounded down. So 12 / 13 = 0, and 13 / 12 = 1. You can resolve this by making one or both of the numbers into a float or double, with an "f" or "d" postfix, or decimal point, e.g. 12.0 / 13 is 0.923...., 13 / 12d is 1.083....

2) Exceptions:

This is due to your use of `nextInt` when your input string was not an int. You need to validate that the input is actually 2 numbers separated by a space and decide what to do if it's not.
12 years ago
If we suspend disbelief and pretenddoesn't work, here's how I'd tackle the problem:
12 years ago
Your main problem is that you're not reading a new value each time the player get the answer wrong. You need to re-assign the value of "YesNo" within the while loop with YesNo = readLine. At the start you can initialize it to null with var YesNo = _

Also you might find using pattern matching is neater than if-expressions. e.g.
Should you succeed, you next task is to re-write it without using any mutable state (i.e. no vars, only vals)...
12 years ago
Garrett's way is definitely the best, but in answer to the original question:

You get the error because you're trying to get the head of an empty List. You can tackle this firstly by explicitly checking for an empty List, viz: or, more idiomatically, matching on the list itself:
Notice I've put the return type as List[String]. You could leave it as List[Any], but why not make it a List of Strings since you can.

Contrary to what Raymond Tong says, there is no problem in matching a String by type in a List[Any]. This is the point of matching by type.

BTW I highly recommend 99 Scala Problems for practice with this kind of construct.
12 years ago
I would just include the "\" character in the regular expression. You will need to escape it by putting another "\" before it.

Which reminds me... you will need to escape all the "\" characters in the above examples so that Java correctly incorporates them into the String. In the case of including the "\" character in the regular expression, you will need to escape both of them, so you have "\\\\".

It's a bit confusing, because "\" is required both to escape characters in the Java String, and also within the expression itself.
12 years ago
If you want to do it on the basis of allowed characters, you could do this:

or if you just want to exclude certain characters
In explanation:
"matches" is a method on String.
In the regex, "." is any character and "*" means "0 or more times". So .*[ ].* would mean a space surrounded by any other characters
Square brackets [] match any of the alternative characters within them
^ (as first character inside the square brackets) means "NOT these characters"
\w means word-characters, i.e. A-Za-z0-9 and _
Certain characters like [ and ] need to be escaped with a \

for more info see Wikipedia regular expressions
12 years ago

Yes. On the other hand - is there a problem with semicolons? They're more easy to type on a german keyboard than curly braces.


If you use parentheses you need 6 extra semicolons, whereas replacing them with curly braces is no extra characters. Usually when people use parentheses + semicolons it for saving space by putting everything on one line (and maybe to make it look a bit more like a Java for-loop).

On a bit of a tangent,

Well - I have a good reason, I want to find the max, and there is no problem, until you find one. Of course, the max-call you use is more elegant, but I think it wasn't available 2009, but a roll-your-own function to use in a fold would have been possible over then too.


That would work if you know you have at least 1 positive value; you could use Int.MinValue instead of 0, but better would be or equivalently For Int lists as in the case above, this could be simplified to This "max" method is the one on Int that has been around forever (I assume...), rather the one on lists that we're trying to emulate.

On even more of a tangent, I've done some research and found you can make a general version of this for anything that can be ordered (i.e. has the Ordered trait) using the Pimp My Library pattern: (Here <% is a view bound which translates into English as "can be seen as". Int is not an Ordered, but can be seen as a RichInt, which has the Ordered[Int] trait.)

So you can now do things like this:
12 years ago
Hey Walter, all I know is that it used to work just fine, they changed something, and now it doesn't! A quick comparison of the source codes reveals quite a few changes in the Timer class. I don't think the fault is with my little test example. It's a very simple and typical use-case.
12 years ago
It was a request... please keep posts on topic... you've already derailed this thread enough with your unhelpful comments. Thank you.
12 years ago
What's wrong with you people? I can (and do) write whatever I like in my blog, and I research ideas however I want! If you don't have any ideas to contribute, don't post (unless it's amusing or interesting in some way... trolling doesn't count ).
12 years ago

Ulf Dittmer wrote:Why do you want to write a blog post if you don't know what to put in it? That sounds a bit like "I want to write a novel, but I can't think of a subject."


I've already told you what my subject is. I have some ideas but I think crowdsourcing will lead to better ones.
12 years ago