Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
lord funk
Feb 16, 2004

I have a pile of SVG files.

They are all basic Bezier curves / lines. If you check the file in a text editor, you can find the path data coordinates in a line that looks something like this:

code:
<path d="M12,163c11-128,22-134,55-112c17,12,107,47,88-53"/>
It is easy enough to understand that (no thanks to Adobe's lack of spaces and commas), and translate the coordinates into a series of quadratic Bezier coordinates (here the code is from the app Processing, which runs in Java):

code:
bezier(12, 163, 23, 35, 34, 29, 67, 51);
bezier(67, 51, 84, 63, 174, 98, 155, -2);
Sometimes data points are relative, sometimes they are absolute. Sometimes a shorthand is used that assumes some coordinates from the last curve.

The point of this is, how would you go about translating hundreds of these SVG files into the new, absolute coordinates needed for the bezier() equation? How would you find and parse the relevant portion of the SVG file into the usable variables?

I'm looking for a general direction.

lord funk fucked around with this message at 01:14 on Oct 23, 2009

Adbot
ADBOT LOVES YOU

BizarroAzrael
Apr 6, 2006

"That must weigh heavily on your soul. Let me purge it for you."
I need a batch file to be able to tell tomorrow's date. If there is a more straightforward way I would like to hear it, but best I can tell I have to set the PC's time to a fraction-of-a-second to midnight, let it tick over, read the new date and revert it:

code:
@echo off

echo right now it is %time% %date%

set REALDATE=%date%
set REALTIME=%time%
time 23:59:59.99
SET TOMORROW=%date%
time %REALTIME%
date %REALDATE%

echo tomorrow is %tomorrow%
echo right now it is %time% %date%
pause
The problem here seems to be that I can't set the centi-second (is that even what it's called? One percent of a second, anyway) so it goes to 23:59:59, so it's not ticked over into the next day for the next line. Can I fix this?

I would rather not skew the system clock significantly over time, so I don't want to have to stall it for too long in the wrong time before I set it back.

yatagan
Aug 31, 2009

by Ozma

BizarroAzrael posted:

I need a batch file

You have a lot batch files that run headfirst into issues like these, can you just do Perl scripts instead? All you need is to install the interpreter and you're pretty much good to go. The benefit being that there are a million Perl libraries that do things like what you need.

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

BizarroAzrael posted:

I need a batch file to be able to tell tomorrow's date. If there is a more straightforward way I would like to hear it, but best I can tell I have to set the PC's time to a fraction-of-a-second to midnight, let it tick over, read the new date and revert it:

code:
@echo off

echo right now it is %time% %date%

set REALDATE=%date%
set REALTIME=%time%
time 23:59:59.99
SET TOMORROW=%date%
time %REALTIME%
date %REALDATE%

echo tomorrow is %tomorrow%
echo right now it is %time% %date%
pause
The problem here seems to be that I can't set the centi-second (is that even what it's called? One percent of a second, anyway) so it goes to 23:59:59, so it's not ticked over into the next day for the next line. Can I fix this?

I would rather not skew the system clock significantly over time, so I don't want to have to stall it for too long in the wrong time before I set it back.

http://www.robvanderwoude.com/datetimentmath.php

Also, I think this is at least the second time I've sent you to that site. I do it because that is the best batch file reference site I've ever seen. So make sure you check there to answer any questions.

BizarroAzrael
Apr 6, 2006

"That must weigh heavily on your soul. Let me purge it for you."

Jethro posted:

http://www.robvanderwoude.com/datetimentmath.php

Also, I think this is at least the second time I've sent you to that site. I do it because that is the best batch file reference site I've ever seen. So make sure you check there to answer any questions.

The content on it is good, but I have a hard time navigating it. Looks like I can get what I want out of that, none of the sites I found on Google even mentioned using Julian dating, mucking around with %date% seemed to be the best they could offer.

dyn
Jan 9, 2005

Barn duelin' since '07
I'm trying to create a regular expression in use with split in perl that will grab the base site from ARGV, but I have yet to find one that will work. Example: When the user enters http://www.google.ca I want to be able to extract google from the variable. Any ideas?

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

dyn posted:

I'm trying to create a regular expression in use with split in perl that will grab the base site from ARGV, but I have yet to find one that will work. Example: When the user enters http://www.google.ca I want to be able to extract google from the variable. Any ideas?
I don't know that you could do it with split. Maybe try a match with a regex like:
s!https?://(\w+\.)*(\w+)\.\w+/.*!g and then get the value of $2

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

dyn posted:

I'm trying to create a regular expression in use with split in perl that will grab the base site from ARGV, but I have yet to find one that will work. Example: When the user enters http://www.google.ca I want to be able to extract google from the variable. Any ideas?

You could do it with a regex but most languages have a URL class to assist with something like this, you may want to look for one.

covener
Jan 10, 2004

You know, for kids!

fletcher posted:

You could do it with a regex but most languages have a URL class to assist with something like this, you may want to look for one.

Any built-in is going to have a hard time with this notion of a "base site" being some substring of the domain/host.

edit: using it at least getting down to to the hostname with a URL class makes perfect sense, didn't intend to criticise your specific followup.

covener fucked around with this message at 02:10 on Oct 24, 2009

ante
Apr 9, 2005

SUNSHINE AND RAINBOWS

lord funk posted:

I have a pile of SVG files.

They are all basic Bezier curves / lines. If you check the file in a text editor, you can find the path data coordinates in a line that looks something like this:

code:
<path d="M12,163c11-128,22-134,55-112c17,12,107,47,88-53"/>
It is easy enough to understand that (no thanks to Adobe's lack of spaces and commas), and translate the coordinates into a series of quadratic Bezier coordinates (here the code is from the app Processing, which runs in Java):

code:
bezier(12, 163, 23, 35, 34, 29, 67, 51);
bezier(67, 51, 84, 63, 174, 98, 155, -2);
Sometimes data points are relative, sometimes they are absolute. Sometimes a shorthand is used that assumes some coordinates from the last curve.

The point of this is, how would you go about translating hundreds of these SVG files into the new, absolute coordinates needed for the bezier() equation? How would you find and parse the relevant portion of the SVG file into the usable variables?

I'm looking for a general direction.
I'm actually writing some code that will eventually incorporate the ability to read and work with SVG files in Processing. I haven't started this portion yet so I'm not intimately familiar with SVG, but it's essentially XML, isn't it?

It shouldn't take long to write an app to read, parse, and spit out a directory full of files in the format that bezier() requires. Better than that, just write it into your Processing program. It's is a very fast language and SVG is designed to be a very easy format to work with, so I'm not sure what part you're hung up on.

If you want some less general advice, I'm happy to help.

lord funk
Feb 16, 2004

ante posted:

I'm actually writing some code that will eventually incorporate the ability to read and work with SVG files in Processing. I haven't started this portion yet so I'm not intimately familiar with SVG, but it's essentially XML, isn't it?

It shouldn't take long to write an app to read, parse, and spit out a directory full of files in the format that bezier() requires. Better than that, just write it into your Processing program. It's is a very fast language and SVG is designed to be a very easy format to work with, so I'm not sure what part you're hung up on.

If you want some less general advice, I'm happy to help.

You can read and display SVG files (with display functions like scale and rotate) in Processing already using PShape. My real hang up is this: there is a function bezierPoint() that calculates x y coordinates along a Bezier curve. I need to use that, but there is no equivalent tool for SVG files.

How would you deal with that problem?

Edit: found a library (Geomerative) for Processing that works with SVG files. Solves that.

lord funk fucked around with this message at 18:43 on Oct 24, 2009

Fruit Smoothies
Mar 28, 2004

The bat with a ZING
Hey guys, I guess this is more of a maths question, but what the hell.

At the moment, we're testing the randomness of numbers found from a scientific spectrometer's background noise.
As part of the work, we're using dice outcomes in large scales to see if it eventually tends towards equal probability.

As an aside, we need to test the scenario using biased dice. At the moment, the background noise is "converted" into a number between 1-1000. A 3-sided dice is simulated, and if the value falls between 1-333, 334-666, 667-100 then sides 1,2,3 are chosen respectively.

The code needs to cater for bias. This would involve a 4-sided dice simulation with the input

code:
side 1: 0.25
side 2: 0.25
side 3: 0.25
side 4: 0.33
This is problematic for our current boundary system, as the probability exceeds 1. All of us on the team are completely stumped as to a way using this input.

Why use this input, I hear you ask? Well, for future work, it's important to express the outcomes as "1 in 4" times this will happen "1 in 3" times this will happen, and to avoid "25% of the time" (mathematically, I know they're the same, but they are different)


Any help on solving this seemingly impossible problem would be greatly received.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
There are exactly three ways to resolve this.

1) The input probabilities are only meant to be valid relative to other each and should be uniformly scaled so that they sum to 1. i.e. divide each probability by the sum of all the original probabilities.
2) Unlike the sides of a die, the events are not disjoint, so your model is totally wrong.
3) The input is ill-formed and you should bitch about it, possibly recovering with method 1.

Fruit Smoothies
Mar 28, 2004

The bat with a ZING

rjmccall posted:

There are exactly three ways to resolve this.

1) The input probabilities are only meant to be valid relative to other each and should be uniformly scaled so that they sum to 1. i.e. divide each probability by the sum of all the original probabilities.
2) Unlike the sides of a die, the events are not disjoint, so your model is totally wrong.
3) The input is ill-formed and you should bitch about it, possibly recovering with method 1.

Yes I know that the 0.25 should scale to 0.223, but the idea is the long-term statistics give 25/100, 25/100, 25/100, 33/100.
This means getting over their relativity and somehow finding a way of finding a way to bias it differently.

I thought it was impossible. I can't even think of an easy way to trick the system into changing the stats.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Legitimate empirical data will never give you probabilities of disjoint events that add to greater than 1. Can you actually explain this in some way that makes sense, or are you just looking for someone to hold your hand and tell you it's impossible?

Fruit Smoothies
Mar 28, 2004

The bat with a ZING

rjmccall posted:

Legitimate empirical data will never give you probabilities of disjoint events that add to greater than 1. Can you actually explain this in some way that makes sense, or are you just looking for someone to hold your hand and tell you it's impossible?

I know it's impossible. Assuming the probabilities are expressed exactly in a 100-data run, it would be impossible to have 25:25:25:33, because that would require 108 runs.

What I wanted to do, was to make the algorithm that calculated the boundaries work on the input aforementioned. Recalculating the probabilities is the only way to make that input work, even if the inputs will not be expressed.

ErIog
Jul 11, 2001

:nsacloud:

Fruit Smoothies posted:

Yes I know that the 0.25 should scale to 0.223, but the idea is the long-term statistics give 25/100, 25/100, 25/100, 33/100.
This means getting over their relativity and somehow finding a way of finding a way to bias it differently.

I thought it was impossible. I can't even think of an easy way to trick the system into changing the stats.

They're asking you to change the laws of mathematics such that percentage works completely differently than it should. If they insist on giving you input in such a ridiculous way, then you should figure out the percents from the numbers they give you.

Sum the total, and take the percentage of each of the sides so that the numbers they're giving you coalesce into a valid percentage. Then modify your dice side windows accordingly so that:

3(0.25) + 0.33 = 1.08

Side 1: 0.25/1.08, 0.231, 1-231
Side 2: 0.25/1.08, 0.231, 232-462
Side 3: 0.25/1.08, 0.231, 463-693
Side 4: 0.33/1.08, 0.307, 694-1000

You can't violate the laws of math in this project unless there's some piece of this you're not understanding. This is also going to make your simulations fuzzier than they would probably like. You should meet with them to sort this issue out, and make sure they're okay with the fuzziness introduced by you reframing their numbers into valid percents for real probability.

ErIog fucked around with this message at 00:21 on Oct 27, 2009

Fruit Smoothies
Mar 28, 2004

The bat with a ZING

ErIog posted:

They're asking you to change the laws of mathematics such that percentage works completely differently than it should. If they insist on giving you input in such a ridiculous way, then you should figure out the percents from the numbers they give you.

Sum the total, and take the percentage of each of the sides so that the numbers they're giving you coalesce into a valid percentage. Then modify your dice side windows accordingly so that:

3(0.25) + 0.33 = 1.08

Side 1: 0.25/1.08, 0.231, 1-231
Side 2: 0.25/1.08, 0.231, 232-462
Side 3: 0.25/1.08, 0.231, 463-693
Side 4: 0.33/1.08, 0.307, 694-1000

You can't violate the laws of math in this project unless there's some piece of this you're not understanding. This is also going to make your simulations fuzzier than they would probably like. You should meet with them to sort this issue out, and make sure they're okay with the fuzziness introduced by you reframing their numbers into valid percents for real probability.

I think they thought I could make it so "roughly every 1/4 is this, but also every 1/4 is that"

To be honest, I was so focused on trying to obtain their boundaries - even though I knew I couldn't - I completely forgot that 0.231 is approx 25% anyway.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

I'm writing a parallel cellular automata library, and I need to divide my simulation space more or less equally according to the number of processors that the program gets assigned.

My constraint is that subgrids should be more or less square, since I'd like to maximize the amount of surface area they have (so as to minimize their perimeter, which will cost me in interprocess communication). Obviously this is trivial if p is a square number, but I can't see a good way of doing it in other cases.

Of course, an actual optimal solution strikes me as Hard-With-A-Capital-H since it seems like it involves integer factorization, but I can't think of a good approximation to this problem which is "good enough". Anybody have any ideas?

RussianManiac
Dec 27, 2005

by Ozmaugh

Dijkstracula posted:

I'm writing a parallel cellular automata library, and I need to divide my simulation space more or less equally according to the number of processors that the program gets assigned.

My constraint is that subgrids should be more or less square, since I'd like to maximize the amount of surface area they have (so as to minimize their perimeter, which will cost me in interprocess communication). Obviously this is trivial if p is a square number, but I can't see a good way of doing it in other cases.

Of course, an actual optimal solution strikes me as Hard-With-A-Capital-H since it seems like it involves integer factorization, but I can't think of a good approximation to this problem which is "good enough". Anybody have any ideas?

Why do you need synchronization? Isn't cellular automata simulation pretty much taking the previous grid and computing next value for each point? The previous grid doesn't change and for the next grid points only depend on previous grid values.

Why do you need interprocess communication, can't you just use threads?

One optimization I can think of is padding a grid so that when you divide it between N threads, there is no false sharing going on. That is if one thread works on a chunk of grid, then all of its contents should fit separately in cache. If they dont then if thread 1 modifies some point in grid, and its on same cache line as points belonging to different thread, then it will cause cache invalidation and will have big performance hit.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

RussianManiac posted:

Why do you need synchronization? Isn't cellular automata simulation pretty much taking the previous grid and computing next value for each point? The previous grid doesn't change and for the next grid points only depend on previous grid values.

Why do you need interprocess communication, can't you just use threads?
It's for a parallel architectures class where we have to use MPI. So, each process' portion of the grid is locally stored whereever the process is executing. (And even if it wasn't, a thread-based implementation would have to partition the grid somehow anyway, which is where I'm stuck on.)

RussianManiac posted:

One optimization I can think of is padding a grid so that when you divide it between N threads, there is no false sharing going on. That is if one thread works on a chunk of grid, then all of its contents should fit separately in cache. If they dont then if thread 1 modifies some point in grid, and its on same cache line as points belonging to different thread, then it will cause cache invalidation and will have big performance hit.
Not quite what you're saying since my implementation is over a distributed system, but yes, my intention is to replicate the outermost boundary between grids so I can grab a neighbour's cells all in one message. (edit: and, yes, hopefully there's enough processors allocated for the problem such that it fits into a cache line :) )

Dijkstracula fucked around with this message at 01:51 on Oct 27, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

RussianManiac posted:

Why do you need synchronization? Isn't cellular automata simulation pretty much taking the previous grid and computing next value for each point? The previous grid doesn't change and for the next grid points only depend on previous grid values.

How is this complicated? He wants partition space among processors and needs to be able to share data at the edges. I really can't fathom why you'd think he doesn't need some kind of synchronization method for that.

RussianManiac
Dec 27, 2005

by Ozmaugh

Avenging Dentist posted:

How is this complicated? He wants partition space among processors and needs to be able to share data at the edges. I really can't fathom why you'd think he doesn't need some kind of synchronization method for that.

Maybe I am missing something but can't you implement cell automata with 2 grids that you interchange. One being previous state and the second one being the next state? when you are computing the next state you are just using the first grid, which is not modified until it becomes the next state in next iteration?

Hmm, I am not sure how false-sharing comes into play in distributed computing, but it certainly is a significant point if you are talking about sharing data on single machine but between multiple cores/processors.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

RussianManiac posted:

Maybe I am missing something but can't you implement cell automata with 2 grids that you interchange. One being previous state and the second one being the next state? when you are computing the next state you are just using the first grid, which is not modified until it becomes the next state in next iteration?

Uh, at the very least you need to put barriers in so that one processor doesn't get ahead of the others. And as mentioned, this is MPI anyway, which is a shared-nothing system.

RussianManiac
Dec 27, 2005

by Ozmaugh

Avenging Dentist posted:

Uh, at the very least you need to put barriers in so that one processor doesn't get ahead of the others. And as mentioned, this is MPI anyway, which is a shared-nothing system.

do you mean block synchronization? yea that is true.

False sharing occurs when you don't mean to share data but it is shared anyway because the division between thread's data falls somewhere in middle of cache line, so when one thread modifies its value, it causes all other processors to invalidate their cache and do memory look up, even though the data that those threads are concerned with didn't change.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Uh, yeah, I'm not really concerned about cache consistency since everything is replicated by virtue of it being written in MPI. (I'm not really sure how we got on talking about cache) It's really the grid partition problem that I'm stuck on.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Dijkstracula posted:

Uh, yeah, I'm not really concerned about cache consistency since everything is replicated by virtue of it being written in MPI. (I'm not really sure how we got on talking about cache) It's really the grid partition problem that I'm stuck on.

Divide the simulation space into an irregular grid of c columns where each column has either r or r+1 rows; we want cr ≤ p < c(r+1). If the simulation space is w x h, you want to pick c and r such that c/r ≈ w/h (or vice-versa; the point is that the aspect ratios should approximately match, or else the cells won't be very square).

So pick c := floor(sqrt(ph/w)), r := floor(p/c), and go from there.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Dijkstracula posted:

Uh, yeah, I'm not really concerned about cache consistency since everything is replicated by virtue of it being written in MPI. (I'm not really sure how we got on talking about cache) It's really the grid partition problem that I'm stuck on.

I still don't really understand why you care how the grid is partitioned. Once a node has the list of cells it should calculate, it can advance those cells without any syncronization with other nodes; it just has to arrange to broadcast its results when it completes. As long as you always broadcast results, every node will be updated with the results for every cell, so each node can always keep its own copy of the whole grid for the previous timestep.

Or is the grid so incredibly large that you want to avoid keeping a complete copy on every node? Or are broadcasts really expensive on your architecture?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

rjmccall posted:

So pick c := floor(sqrt(ph/w)), r := floor(p/c), and go from there.
Aha, thank you so much. I made it as far as deriving c but couldn't make it to r. Formulating it as fitting the aspect ratio made it far clearer, thanks :)

ShoulderDaemon posted:

Or is the grid so incredibly large that you want to avoid keeping a complete copy on every node? Or are broadcasts really expensive on your architecture?
It's more the former, but partly it's just wasted space, especially if p is large. Since a subgrid is only affected by local changes, you're replicating the vast majority of the simulation space which any given process will never ever see. :)

Thanks everybody.

lord funk
Feb 16, 2004

I've got some code optimizing to do, and I was wondering if there is a good resource / list of operations ordered by how expensive they are to run.

As in, if multiplying values together is more computationally expensive than adding, or if running if(this == that) 10,000 times is more expensive than multiplying something four times?

Is there a good place to learn this? My Google-fu is poo poo today.

floWenoL
Oct 23, 2002

lord funk posted:

I've got some code optimizing to do, and I was wondering if there is a good resource / list of operations ordered by how expensive they are to run.

As in, if multiplying values together is more computationally expensive than adding, or if running if(this == that) 10,000 times is more expensive than multiplying something four times?

Is there a good place to learn this? My Google-fu is poo poo today.

With pipelining and branch prediction, it's not as simple as listing a cycle count for each instruction anymore. The profiler is your friend.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Your first step in optimizing should never be micro-optimization of instructions. Better to reduce algorithmic complexity first.

Doghouse
Oct 22, 2004

I was playing Harvest Moon 64 with this kid who lived on my street and my cows were not doing well and I got so raged up and frustrated that my eyes welled up with tears and my friend was like are you crying dude. Are you crying because of the cows. I didn't understand the feeding mechanic.
I'm taking online classes for a major in software development, which means a lot of programming classes with no actual class, and terribly terse textbooks to slog through on my own. I'm almost down with the first class, which is java, and I'm having a terribly hard time finishing the last few assignments. The next class is supposed to be "object oriented programming," and I guess we are going to be doing c#. Does anyone have any tips on how to teach myself this stuff? I used "beginning programming with java for dummies" which really helped, but after I finished and it got into harder stuff, I got totally lost. I'm afraid I'm not going to be able to get through this program on my own. Any tips?

Contero
Mar 28, 2004

Here is a book called "Learning C#": http://www.amazon.com/Learning-C-3-0-Jesse-Liberty/dp/0596521065/ I have no idea if it is good or not, but it's linked on the first post of the C# thread.

Here is the C# thread: http://forums.somethingawful.com/showthread.php?threadid=2262300

I don't know what's available in your area, but if you're having trouble teaching yourself programming I'd suggest trying a community college programming class. There is a lot you can get from having a two-way face-to-face conversation with someone who (more or less) understands the material than from just watching lectures or reading books.

ErIog
Jul 11, 2001

:nsacloud:

Doghouse posted:

I'm taking online classes for a major in software development, which means a lot of programming classes with no actual class, and terribly terse textbooks to slog through on my own. I'm almost down with the first class, which is java, and I'm having a terribly hard time finishing the last few assignments. The next class is supposed to be "object oriented programming," and I guess we are going to be doing c#. Does anyone have any tips on how to teach myself this stuff? I used "beginning programming with java for dummies" which really helped, but after I finished and it got into harder stuff, I got totally lost. I'm afraid I'm not going to be able to get through this program on my own. Any tips?

Are you having trouble with the syntax or wrapping your head around overarching concepts like OOP?

Doghouse
Oct 22, 2004

I was playing Harvest Moon 64 with this kid who lived on my street and my cows were not doing well and I got so raged up and frustrated that my eyes welled up with tears and my friend was like are you crying dude. Are you crying because of the cows. I didn't understand the feeding mechanic.

ErIog posted:

Are you having trouble with the syntax or wrapping your head around overarching concepts like OOP?

The concepts I understand, pretty much, it's the details and how to implement the ideas that I have trouble with. I read through "programming with java for dummies" and I understood it for the most part, and for the first 7 weeks of the 9-week class I did pretty well. But then I had an assignment to use the bubble sort method, and now we are doing methods and objects and classes and strings and I'm pretty hopelessly lost. The textbook we are using is almost incomprehensible.

Safe and Secure!
Jun 14, 2008

OFFICIAL SA THREAD RUINER
SPRING 2013
http://pastebin.com/m17ac43a2 (java)

Why are exceptions being thrown for every input when an exception is caught for just one?

I mean, say I enter, "h" instead of a double for the first prompt. The program rushes through the rest and acts like I had entered a non-double for all of them, by executing the relevant "catch" block for each try block.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Safe and Secure! posted:

http://pastebin.com/m17ac43a2 (java)

Why are exceptions being thrown for every input when an exception is caught for just one?

I mean, say I enter, "h" instead of a double for the first prompt. The program rushes through the rest and acts like I had entered a non-double for all of them, by executing the relevant "catch" block for each try block.

Because that's how java.util.Scanner works: it only advances if the current input matches the request.

This is easily discoverable in the documentation.

Safe and Secure!
Jun 14, 2008

OFFICIAL SA THREAD RUINER
SPRING 2013
Thanks, I hadn't even considered that the scanner was what was giving me trouble. I thought it was some sort of crazy property of catch blocks and the API wasn't helping there.

Problem solved:
http://pastebin.com/m153c03b9

Would it be a better idea to just use
code:
Double.parseDouble(in.next());
instead of
code:
in.nextDouble()
and not worry about skipping lines in the scanner? I don't see any reason to prefer one over the other, other than readability.

Adbot
ADBOT LOVES YOU

Contero
Mar 28, 2004

code:
while (I want a goddamn double) {
   if (in.hasNextDouble()) {
      arg = in.nextDouble();
   } else {
      in.nextLine();
      System.out.println("hey, gently caress you buddy");
   }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply