Open Side Menu Go to the Top

12-15-2012 , 06:42 PM
Here is some Java code I am having trouble with on one of my programming assignment. We just learned exception handling and try-catch blocks.

int requestedRow = 0;
boolean goodInput = false;

while (!goodInput)
{
try
{
System.out.println("\nPlease enter the row that you would like to "
+ "be seated in " + START_ROW_FC + "-" + END_ROW_FC +
":\n");
requestedRow = console.nextInt();
goodInput = true;
}
catch(InputMismatchException imx)
{
System.out.println("\nSorry, but the row must be a number "
+ START_ROW_FC + "-" + END_ROW_FC + ". Please try "
+ "again.\n");
}
}

How come when this code executes if the input is not an integer it just continuously prints these two statements:

Sorry, but the row must be a number 1-2. Please try again.


Please enter the row that you would like to be seated in 1-2:


over and over and over continuously. It is just stuck in a continuous loop with no way to stop it except by pressing stop program. I expected it to go through the catch block, then loop back through the try block and stop at the console.nextInt(); statement allowing the user to retry with a valid input.


How do I get it to stop at console.nextInt() and allow the user to try again?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
12-15-2012 , 06:58 PM
I don't know exactly what "console" is but I guess it's a Scanner object. I took a quick look at the docs and I think when it throws an InputMistmatchException it doesn't actually clear the token that gave the exception. So your code is just constantly trying to treat the entered string value as an integer.

You need to figure out how to clear the console object when a user puts in a non-integer value.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-15-2012 , 08:13 PM
Yes, sorry, console is a Scanner object.

Your help got me on the right track. Did some Google work, and found out to clear the console object I need this statement:

console.nextLine();

Put that in and it works great now. Thanks!

Last edited by KatoKrazy; 12-15-2012 at 08:19 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-15-2012 , 08:20 PM
Quote:
Originally Posted by KatoKrazy
Here is some Java code I am having trouble with on one of my programming assignment. We just learned exception handling and try-catch blocks.
Code:
       int requestedRow = 0;
        boolean goodInput = false;
       
       while (!goodInput)
       {
           try
           {
            System.out.println("\nPlease enter the row that you would like to "
                    + "be seated in " + START_ROW_FC + "-" + END_ROW_FC +
                    ":\n");              
            requestedRow = console.nextInt();
            goodInput = true;
           }
           catch(InputMismatchException imx)
           {
            System.out.println("\nSorry, but the row must be a number "
                    + START_ROW_FC + "-" + END_ROW_FC + ". Please try "
                    + "again.\n");
           }
       }
Use code tags mate
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-15-2012 , 11:42 PM
Quote:
Originally Posted by kerowo
You want it without the order of A and B changing?
A team | 50%
B team | 20%
C team | 52%
D team | 65%

should become

D team | 65%
C team | 52%
A team | 50%
B team | 20%

Well both would work "become" or generate other entries i.e. have some formula stuff in empty cells that creates the second form by referencing the first.

The excel advice seems good "sort by pairs" and variants of it sounds like reasonable search terms. Too tired to do it now but I'll try it tomorrow.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-16-2012 , 07:36 AM
Clowntable, you don't want to create cells A, B, C, D, but rather A, B, D, E. This is becuse you'll create a nightmare situation for yourself later on, if and when you'd like to sort again. I'll explain why to you in a second.




First, you have your columns. Move your cursor to 1-A, then, while holding down SHIFT + CTR**, move your cursor to the right (arrow-key right), then press down (arrow-key down). CTR-C, then move to 1-D, then CTR-V to paste the selection.




**SHIFT is the hot-key for "highlight"

** The CTR basically means "go to end of data." If you don't hit CTR, then you'll only move one cell at a time.

Click on the LETTER E above your data, the whole column should highlight. In the top bar, there is an icon that looks like Z -> A. If you hold your mouse over it, it will say "Sort Descending."



Press the icon and a window will pop up titled "Sort Range." The button titled "Extend Selection" be highlighted by default. Press enter to extend the selection. The two columns will be sorted and the data integrity will be kept.




The "Expand Selection" is why you don't want to place this kind of data as A, B, C, D. Once you place these fields next to each other, you'll never be able to only sort just the two columns w/o either screwing up the other data or screwing up the new data. If you're absolutely married to the idea of only having column A, B, C, D, I don't know what to say, outside of this is not a very good idea, IMO, but nothing stops you from copy/pasting D and E to C and D.

If you want this to be automated, this thread might be helpful to you:

http://ask.libreoffice.org/en/questi...ction-in-calc/

You could also create a simple function, where you auto-input the values you just input into column A and paste it to Column D, but I'm not sure if this part is useful to you, but it looks like this:

highlight D1 then click on the formula bar (the bar with the summation sign), then enter:

=a1

Once you put something in A1 and press enter, whatever you wrote in a1 will appear in d1. You can actually highlight d1, hit CTR-C and highlight to D-X then press CTR-V, wich will automatically made d2 = a2, d3 =a3, and so on and so forth.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 03:55 PM
Anyone here spend a lot of time dealing with promises in javascript frameworks (using AngularJS and $q, but I believe equivalent functionality/implementations exists in a bunch of frameworks) beyond simply using a callback to return http results? I understand monadic patterns fairly well in theory, but haven't spent much time designing real-world software modules in practice. If you have objects that are constructed from asynchronous operations using promises and have to interact with asynchronous operations using promises, do you tend build the object model with the knowledge of promises built-in, so methods will return promises where necessary) or do you keep the object model ignorant and wrap around where necessary?

For instance, would you rather design your object interface to be like this:

Code:
function MyObject() {};

MyObject.prototype.records = function() {
  return http.get("/records") // returns immediately with a promise
}

var obj = new MyObject();
obj.records().then(function (yadayada) {
  // do something with yadayada
});
and force the object model and the user to know about promises or like this:

Code:
function MyObject(records) {
  this.records = records;
}

http.get("/records").then(function (yadayada) {
  var obj = new MyObject(yadayada);
  // do something with obj.records
});
and have the object model know nothing about promises and make it only usable within certain contexts.

Another way to ask this question is, when confronted with external asynchronicity, do I propagate that everywhere for maximum flexibility (but at the cost of interface complexity), or do I keep the interface as simple as possible and inject asynchronicity only where critical? How do you know which pattern is correct for a given application?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 04:51 PM
i've never heard the term "promises" used like this before, but i think to answer to your questions is:

you want to keep all the calls to an external API in on one place, preferably wrapped in an your own API object. Then inject that API object into the other objects in your code that need to use it.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 05:27 PM
I think jquery calls them deferred objects, http://api.jquery.com/category/deferred-object/
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 05:32 PM
Quote:
Originally Posted by gaming_mouse
i've never heard the term "promises" used like this before, but i think to answer to your questions is:
http://en.wikipedia.org/wiki/Futures_and_promises

http://wiki.commonjs.org/wiki/Promises

All event-driven callbacks can be understood in terms of promises, even when it isn't explicitly supported.

Quote:
you want to keep all the calls to an external API in on one place, preferably wrapped in an your own API object. Then inject that API object into the other objects in your code that need to use it.
The problem is that promises aren't a simple external dependency and they change how you structure your programs. If you have an API that uses a promise as a way to deal with asynchronicity, if the user of your object model needs to take advantage of asynchronicity, you too have to wrap things up in a promise, because you don't have the result to return. If you don't uses promises yourself, either you have to block until you have the result (with obvious performance implications) or you can only use those objects in a certain context (say, a callback to a promise, where you already have the result). If you do use it everywhere, then almost every part of your application has to be promise-aware, which makes the code, especially the interface, harder to understand (especially in languages like javascript).
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 06:07 PM
Quote:
Originally Posted by candybar
http://en.wikipedia.org/wiki/Futures_and_promises

http://wiki.commonjs.org/wiki/Promises

All event-driven callbacks can be understood in terms of promises, even when it isn't explicitly supported.
Interesting. Just trying to understand the motivation more: Can you describe the specific use-case you are trying to find a good solution for?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 06:20 PM
Choice #2 seems way way way more natural to me.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 06:51 PM
Quote:
Originally Posted by gaming_mouse
Interesting. Just trying to understand the motivation more: Can you describe the specific use-case you are trying to find a good solution for?
I'm writing a prototype for a Force.com/Database.com like platform (if these are foreign to you, think web-based Microsoft Access) for creating a database application without (much) coding. Obviously I can't load the entire database or even database metadata into memory, but related objects need to be able to connect to the db (via web service) to get missing information. A table object needs to be able to get record objects stored in the table and a record object needs to be able to get related records (via foreign key) in other tables. But since it's ridiculous to have those preloaded when the record object is constructed, these should return promises. But if I take that too far, almost every property should return a promise. So maybe I remove the act of getting specific information out of some classes to keep the interface cleaner and build objects within a promise callback. The question is, how should we think about making these design decisions? And what criteria am I missing?

I had a basic version of this working without any real object model, just plain javascript objects with an implicitly defined structure being passed around, but this pattern wasn't ideal for adding advanced features. And in the process of creating the object model, I ran into a lot of design questions. I have all the specifics implemented and to be honest, it'll work regardless of the quality of my decisions and it won't matter. It's a prototype (a lot of those features belong more in the web service end in the final product anyway),and my first time doing anything heavy with javascript so I'm more interested in exploring than with the result but I was curious how people thought about these problems.

It seems difficult to find good resources on this (it's possible that it's because there are no good answers) because the moment you start dealing with abstractions like this, options multiply and everyone is too excited about what they can do to think about what they should do.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 07:47 PM
sounds like an interesting and challenging problem. unfortunately now that i understand what you're dealing with i don't have any good advice for you. sounds like the kind of thing i'd really need to get my hands dirty with before i'd understand the design options and tradeoffs.

if you don't get a satisfying answer here, you should try posting in SO's design tag, and if they close you down there for not being a question with a specific answer, you could try programmers.stackexchange.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 08:10 PM
Quote:
Originally Posted by gaming_mouse
sounds like an interesting and challenging problem. unfortunately now that i understand what you're dealing with i don't have any good advice for you. sounds like the kind of thing i'd really need to get my hands dirty with before i'd understand the design options and tradeoffs.

if you don't get a satisfying answer here, you should try posting in SO's design tag, and if they close you down there for not being a question with a specific answer, you could try programmers.stackexchange.
Yeah, the more I think about it, the more it seems that there are no good rules, you just have to think hard about the consequences for each case.

On a related note, do you guys use a lot of javascript's prototype-based OO features or do you tend to use javascript objects as more of an open bag of properties and have behavior defined outside of prototypes, as you would do in procedural/functional languages? Again, for this project, I'm biased towards using more of javascript's unique features because I want to learn the language very well but it seems like using the more advanced features would be difficult for production web applications because of the need to support legacy browsers).
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 08:37 PM
Check out crockford on some OO stuff in js. Also read Javascript: The Good Parts if you haven't.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 08:51 PM
Quote:
Originally Posted by gaming_mouse
Check out crockford on some OO stuff in js. Also read Javascript: The Good Parts if you haven't.
Cool, thanks, this is also good:

http://killdream.github.com/blog/201...avascript-oop/

I'm a programming language geek so I soak up language features quickly, often before I have sufficient judgment to use them wisely so I was more curious in terms of whether to what extent people take advantage of this in production environments. Especially something like Object.defineProperty, which is available in the latest browsers, but isn't all that easy to emulate in earlier ones.

You can go nuts and do fairly complex stuff (type constructors, for instance, to emulate more complex module features in functional langauges) but I'm curious as to whether people are more restrained in practice due to compatibility issues.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 09:02 PM
candybar,

i haven't read closely and am not sure i understand what you're reall asking, but:

you'll probably be compelled to set up your API in a way that makes sense for asynchronous operations. e.g., when i call your API, i'm probably going to need to hand you a pointer to a callback function at the same time. you should handle as much of this complexity inside the API as possible (so i, the consumer, don't have to) but you're right that you can't just "hide" something as significant to the design as asynchronous operation.

Last edited by tyler_cracker; 12-17-2012 at 09:03 PM. Reason: +1 to JS:TGP. i've only skimmed it but my colleages like and recommend it
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-17-2012 , 09:05 PM
also the best part about JS:TGP is that it's a tiny book that still dedicates 10% of its length to "the bad parts" and "the awful parts".
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-18-2012 , 01:24 PM
daveT:

Works exactly the way I want it to
I really didn't find much searching for this and it feels like I should at least invest time in some Calc improvements. Are there any advanced calc stuff or OO-Calc for data analysis type blogs/books whathaveyou?

I know there's a ton of this for Excel

In case anyone else ever does it there's a minor stumbling block. My 50% and so forth are actually calculated, make sure to use the fixed version i.e. $X*$Y

Last edited by clowntable; 12-18-2012 at 01:50 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-18-2012 , 03:15 PM
I don't know enough about Calc to be incredibly useful with it, tbh. I also disagree with using Calc or Excel as a raw data store.

This is the thing: if you really want to be able to find advanced features of Excel (and I'm assuming Calc as well), you have to be able to speak in the language of SQL. Want to prevent duplicate values? No, you want to add a unique constraint to your columns.

Philosophically, I see no good that can come out of attempting to use a spreadsheet as a raw data-store when compared to just putting all of it into a bona fide database. The reason is that to really focus data, you have to be able to write queries by splitting and joining child tables, and I am guessing this is a total pain in the ass to do in Python, as you'd be doing excessive list processing only to output the results back into some other excel file. Why bother if you can just hook up a DSL?

The problem I ran into is that SQL, Python, or spreadsheets by itself cannot be used alone to do much of value. SQL is great for extracting the parts of the data you need. Python is great for creating charts and graphs, as well as doing basic calculations, especially in regards to numpy. Spreadsheets are great for doing further "deep" analysis that comes well with the ability to examine raw data and do further calculations.

I know that's probably not the answer you wanted to read, but as a Logic Programmer, I suspect you'd have little issue with the above. I should also point out that I am not an Excel maestro either, and that there is certainly much room for disagreement with these thoughts. I will point out that Calc pales in comparison to Excel and that those itt that are great with Excel can probably give you better pointers.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-18-2012 , 09:47 PM
I got a call from my old job asking me to make the program I had at work a little more user-friendly. I thought this would be easy and only take a day or two. Day 3 is ending and I am hoping to finish it tomorrow, but doubt lingers in my mind. It would have been helpful if they didn't delete the entire database, delete important plugins, and tie up the computer for a whole day that I needed it. I thought, well, just a few wrapper functions and I am in and out. Get a few charts to pop up and few csv files loaded and it's all good.

Pfft. Teach me. Every day is something like 100 LOC and fishing for bugs, telling the marketing girl to do data entry and all this other small crap. Adds up fast. Oh, and did I mention it has to be super-easy to use? That means I have to write instructions all over the place.

I also have to remember to write something in the comments apologizing to the next person who dares open the code base.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-18-2012 , 10:26 PM
Sometimes good labels and removing some stuff that's borderline optional can nearly eliminate instructions.

I once wrote instructions to do something and then after reading the instructions and looking at the UI it just dawned on me that if I changed a few labels and cut out some junk the instructions weren't even needed.

Won't be the case for everything but yeah, the "pick good names" rule definitely applies outside of just variable/function names. Sometimes a good label and maybe at most a tiny half sentence of a tooltip can replace 2 full paragraphs.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-18-2012 , 10:54 PM
Quote:
Originally Posted by Shoe Lace
Sometimes good labels and removing some stuff that's borderline optional can nearly eliminate instructions.
Definitely true. We've learned that Devs writing documentation is great because often we'll just change the product to be simpler to avoid writing complex documentation.

Edit: Actually this is true for all sorts of things. Having developers do customer support, IT support, documentation, etc. are all good for the product.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
12-19-2012 , 02:29 AM
I told the company that there is zero chance I could do a GUI. Obviously, if they were paying me hourly, I'd have plenty of time to do anything and everything. As it stands now, the program is click a Python icon w/ semantic name and the Scary Command Line opens, so the interface reads like:

===== Customers ======
[1] x
[2] y
[3] z

===== Sales History ====

[4] x
[5] y
...

I, and the company, wanted to make one particular process automated, but I couldn't get it to work. They already knew I wouldn't get stop one completed, which is basically exporting the data from the company database. What I wanted to automate was the act of importing the .csv to the machine database.

I got thrown off because PostgreSQL can read in a .csv when the data has " and , in it. It turns out that Python cannot make this distinction, so when reading the lists, the indices didn't match up well. I didn't realize this until I finished getting the output "right." I looked down the spreadsheet and saw funky things here and there.

I don't know for certain what happened, but I suspect one issue may be that the .csv file was created on a XP machine and I was working with a Vista machine. When I attempted to bulk-load the data to PostgreSQL, the Elephant got angry for not using UTF-8. I open the .csv in Notepad and I think it was in ascii, though I can't be sure. After I converted it to UTF-8, the file became tab-delimited, which was another several minutes wasted getting that crap straightened out.

The whole process of export-import is very easy, but it's not something I can explain to anyone with a walk through because there are many little things to do, which is delete a few columns, delete the headers, move the file to a public folder, etc etc etc, oh, and of course, convert the time formats.

That is one icon, and the command line interface for that part is basically step-by-step instructions on what to do. I think there are 13 steps, but I don't recall. Periodically, there is a prompt for the user to enter "y." So it's like:

1- x
2- y
3- z

After you have done z, press "y":
...

I didn't realize how many things I took for granted being so close the code. I had a file full of queries that I would just copy/paste into the code and make small modifications as needed. Turns out that all of these modifications aren't possible, or if they are, they have to be moved to the database so the user can upgrade and use the program. Basically around 200 LOC has exploded into... I don't even want to know. Tons of copy/paste and some really long chains of if statements.

In all total, the program generates kinds of 20 graphs, makes export / import easy as possible, and generates about 30 different data files. I think it's a pretty nice piece of software for something I am slamming together in a few days.

I feel like I should be proud of this, but I'm too irritated with it to like it, but does exactly what it promises. Never again will I put myself in a position to write such dreck.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

      
m