|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Thanks for the help. Here's another question while I'm at it. This is basically just a question about making my code better:
+ Show Spoiler +do{ okInput = true; try{ System.out.println("Skriv in ett datum: "); datum = keyboard.nextLine(); if(datum.isEmpty()){ throw new IllegalArgumentException(); } } catch(IllegalArgumentException e){ System.out.println("raden är tom"); okInput = false; } } while(!okInput); do{ okInput = true; try{ System.out.println("Ange en beskrivning: "); beskrivning = keyboard.nextLine(); if(beskrivning.isEmpty()){ throw new IllegalArgumentException(); } } catch(IllegalArgumentException e){ System.out.println("raden är tom"); okInput = false; } }while(!okInput);
I'm using do/while loops and try/catch blocks to add dates and descriptions to an object, but I get the feeling this is a stupid way to go about this since I'm reusing the same code over and over again. This is just a small part of the method, there's actually 6 instances where I use the same do/while and try/catch to do basically the same thing, adding things to an object. So I'm assuming there's a better way. I've thought about making the try/catch blocks into a separate method so I can just run the method on each field that I want to check for exceptions. The problem is that I can't find a way to return to the beginning of the loop if the exception is caught in another method, the program just keeps running without asking the user for correct input.
So the question is, how do I set the boolean okInput to false if the exception is being caught by another method?
|
well ok so i made q quick reply for a quick homework question.
Array...DEPENDS on what the application would be used for, are the users sorted or not? he is allready using an array for his references (also called pointers by me previously), so what is the harm in using a second one where you store the corresponding integer values. Its a work around because i dont think java lets you have a 2 dim array with two different types. He allready has the users in an array, so the second array only doubles delete and insert actions and laos i dont think he has a sorted array, so the costs are not high anyways. And memory is no problem as well, because the array is the cheapest way to store the data, all custom classes will also need space for pointers.
so pointer, reference, meep, mooop.
If you make a custom class to hold those two attributes, why would you NOT make them public, there is nothing you do with them, you only want to store them. What would be the point of making a get and a set function, they dont have to validate anything. If you really care about java, you could also look up if there is some struct interface or object that you can inherit from, probably not the scope of the homework.
Also btw, i took the one array as a given, so that excluded all those collection classes :p
|
On December 02 2014 08:27 Animzor wrote:Thanks for the help. Here's another question while I'm at it. This is basically just a question about making my code better: + Show Spoiler +do{ okInput = true; try{ System.out.println("Skriv in ett datum: "); datum = keyboard.nextLine(); if(datum.isEmpty()){ throw new IllegalArgumentException(); } } catch(IllegalArgumentException e){ System.out.println("raden är tom"); okInput = false; } } while(!okInput); do{ okInput = true; try{ System.out.println("Ange en beskrivning: "); beskrivning = keyboard.nextLine(); if(beskrivning.isEmpty()){ throw new IllegalArgumentException(); } } catch(IllegalArgumentException e){ System.out.println("raden är tom"); okInput = false; } }while(!okInput); I'm using do/while loops and try/catch blocks to add dates and descriptions to an object, but I get the feeling this is a stupid way to go about this since I'm reusing the same code over and over again. This is just a small part of the method, there's actually 6 instances where I use the same do/while and try/catch to do basically the same thing, adding things to an object. So I'm assuming there's a better way. I've thought about making the try/catch blocks into a separate method so I can just run the method on each field that I want to check for exceptions. The problem is that I can't find a way to return to the beginning of the loop if the exception is caught in another method, the program just keeps running without asking the user for correct input. So the question is, how do I set the boolean okInput to false if the exception is being caught by another method?
Im not sure what your problem is exactly, but you can make a new mthod that returns boolean, for example false if its empty and true if its not empty. Is the string a class attribute or did you use that as the function return vlaue allready?
A method can also hand down exceptions to the calling code.
|
On December 02 2014 08:39 LaNague wrote:Show nested quote +On December 02 2014 08:27 Animzor wrote:Thanks for the help. Here's another question while I'm at it. This is basically just a question about making my code better: + Show Spoiler +do{ okInput = true; try{ System.out.println("Skriv in ett datum: "); datum = keyboard.nextLine(); if(datum.isEmpty()){ throw new IllegalArgumentException(); } } catch(IllegalArgumentException e){ System.out.println("raden är tom"); okInput = false; } } while(!okInput); do{ okInput = true; try{ System.out.println("Ange en beskrivning: "); beskrivning = keyboard.nextLine(); if(beskrivning.isEmpty()){ throw new IllegalArgumentException(); } } catch(IllegalArgumentException e){ System.out.println("raden är tom"); okInput = false; } }while(!okInput); I'm using do/while loops and try/catch blocks to add dates and descriptions to an object, but I get the feeling this is a stupid way to go about this since I'm reusing the same code over and over again. This is just a small part of the method, there's actually 6 instances where I use the same do/while and try/catch to do basically the same thing, adding things to an object. So I'm assuming there's a better way. I've thought about making the try/catch blocks into a separate method so I can just run the method on each field that I want to check for exceptions. The problem is that I can't find a way to return to the beginning of the loop if the exception is caught in another method, the program just keeps running without asking the user for correct input. So the question is, how do I set the boolean okInput to false if the exception is being caught by another method? Im not sure what your problem is exactly, but you can make a new mthod that returns boolean, for example false if its empty and true if its not empty. Is the string a class attribute or did you use that as the function return vlaue allready? A method can also hand down exceptions to the calling code.
The problem is that every time I get an exception for an empty string I want the program to return to the top of the loop and try again AND I want to have several prompts to the user to input different information in the same do/while and try/catch block.
|
you can print the message where you tell the user what to do right after you print the error message and then you dont need 6 different loops, only 6 different error handlers.
I also dont like the use of exceptions for things that are trivial such as this, but thats a matter of opinion.
|
Now that you mention it, the exceptions are really silly in this case.
|
On December 02 2014 09:31 Animzor wrote: Now that you mention it, the exceptions are really silly in this case.
I can't really tell what your code is trying to do, but maybe this might help:
boolean detailesComplete = false;
do{ detailsComplete = getCompleteObjectDetails(); }while(!detailsComplete)
public boolean getCompleteObjectDetails() { try { System.out.println("Skriv in ett datum: "); datum = getUserInput();
System.out.println("Ange en beskrivning: "); beskrivning = getUserInput();
//i have no clue what you're going to do with all these variables... but if your code reaches this point that //means that you've successfully gotten input without someone putting in an empty line return true; } catch(IllegalArgumentException e) { System.out.println("Empty Line"); return false; } }
public string getUserInput() throws IllegalArgumentException { string ret = keyboard.nextLine();
if(ret.isEmpty()) throw IllegalArgumentException; else return ret; }
Totally unverified code, i just wrote this in vim without compiling or anything but hopefully this gives you some ideas.
*edit* I really don't like the detailesVerified portion of the code. In general, you should call getObjectDetails() and have it return object details instead of a boolean, then pass it in to another function verified those details to make sure it's legit, then returns a boolean. Your while loop should then exit based on that condition. But this was just stuff I came up with without really thinking about it.
|
810 Posts
Ok this is driving me crazy. I got this list I'm supposed to display (still working on the ls utility), and the first element is .: when using the -R option, to display which directory will be displayed next. All good, I know it's in the list. But I also got this function to remove hidden files/directories when there is no -a option. This is ok too.
Now my issue : I want said function to not remove the first node, the one with .:, because reasons. Here's the function :
t_list *ft_rem_hidden(t_list **output) { t_list *cursor;
cursor = *output; while (cursor) { ft_putnbr(ft_strnequ((char const *)cursor->content, ".:", 2)); ft_putendl(cursor->content); if (ft_strnequ((char const *)cursor->content, ".:", 2) == 0 && ft_strnequ((char const *)cursor->content, ".", 1) && ft_strnequ((char const *)cursor->content, "./", 2) == 0) ft_lstfreeone(output, cursor); else cursor = cursor->next; } return (*output); }
The ft_putendl and ft_putnbr are just to check the value returned by ft_strnequ, and the value, as expected, is 1 only for the .: node. I checked the list content before the while loop, there's a .: at first place, as expected. I checked after the while loop, aaaaaand it's not there anymore. So, I checked if the .: node was freed by checking the if, which it shouldn't because of the strnequ return value, and it's apparently not, as expected. So where the hell is my .: node ?
Edit : I double checked everything to make sure, and I really can't make sense of that. So, my little node goes throught these lines of code, kinda :
t_list *ft_rem_hidden(t_list **output) { t_list *cursor;
cursor = *output; while (cursor) { cursor = cursor->next; } return (*output); } And it's there before the while, but not after. Ok.
|
is that a list of lists, or a pointer to a list?
|
810 Posts
It's a pointer to a list. Should be noted that I "cleaned" up the code a little bit, by saving the cursor->next position to a tmp before freeing it, so there is no chance of undefined behaviour. Doesn't change anything to my problem, but thought it was worth mentionning...
|
sorry, not a list, i meant pointer to an array. you don't have that. you have a linked list node which has different semantics.
your input is a pointer to a pointer to the first element of a linked list. if you set it up improperly such that it's not a pointer, but an address, more specifically the address to the pointer to the first node, when you dereference the pointer you don't get a copy of the pointer but the actual pointer. so when you iterate through your linked list, you're changing the value of the pointer to your linked list. so you're killing your linked list with that call.
|
810 Posts
Uuuuuh, not sure I understand that. A pointer not being equal to an adress is new to me already ^^
So, what I understood is : let's say the main calls rem_hidden. If in the main I initialize *ouput and call rem_hidden(&output), I fall into the category you described ? And what I should be doing is initializing **output in my main and simply call rem_hidden(output) ?
|
|
|
810 Posts
I created a git repo with the whole source for my ls, in case someone wants to take a look. There.
|
|
|
|
|
Edit: Ok my bad, good thread indeed :D
|
On December 03 2014 19:58 ZenithM wrote:Edit: Ok my bad, good thread indeed :D I got fooled for a second too :D
|
What a strange thing to say. I actually quite like the fact that C++ allows you to shoot yourself in the foot and the face at the same time if you don't know what you're doing; yet when you improve in skill it's very satisfying. Unlike Java.
|
|
|
|
|
|
|
|