• Ever wanted an RSS feed of all your favorite gaming news sites? Go check out our new Gaming Headlines feed! Read more about it here.
  • We have made minor adjustments to how the search bar works on ResetEra. You can read about the changes here.

metaprogram

Member
Oct 28, 2017
1,174
If you want to check for range, just use range checks :) lower < x && x < upper
Numbers have a value (x == 12) and an ordering (x < 13). Strings have a value (x == "12"), an ordering (x < "13") and a pattern (x.startsWith("1"), a visual representation).

I value good and pragmatic coding style over program efficiency (but I don't come from a C++ background). The first priority of a programmer should be to have a readable, working and extensible solution. If performance is a concern in the end, start optimizing then

String ordering and numeric ordering are not equivalent, though.

To me, saying "if you want to treat it as a number, treat it as a number, and if you want to treat it as a sequence of digits, treat it as a sequence of digits" is about the most readable thing you can possibly do. "Treat it as a sequence of digits even though you then need to do a numeric range check on it" is awkward and an anti-pattern, IMO
 

erd

Self-Requested Temporary Ban
Banned
Oct 25, 2017
1,181
I'm assuming something like %112==0 would do the trick right? But if not I'm unsure how to grab those first 3 digits. Math has never been my strong suit:/
If you have an integer number 12345678 and you divide it by 10, you get 1234567. If you divide it by 100, you get 123456, and you get 123 when you divide by 100000. Therefore, if you know the length of your zipcode, obtaining the first 3 digits is pretty easy. If the length is variable, you can either first determine the length, or use a loop to repeatedly divide by 10 until your result is small enough.

You can also use the modulo (remainder) operation to extract the last few digits. 12345678 % 10 is 8, for example. If you combine division and modulo you can extract things in the middle (although since I can't think of a single case where this would be useful with integers, you would very likely want to be using strings in those cases anyway).
 

MikeRahl

Member
Oct 27, 2017
421
Step through your code and see what is going on.

The first immediate thing I see is you are doing if(str.startsWith(112)){

I am fairly certain you are intending that to be zip. The fact that 'str' hasn't being initialized (or defined) might be causing a misleading error
 
Oct 27, 2017
16,630
What data type is "zipC"?

Code:
 public static boolean zipcode(String zip){
    if(str.startsWith(112)){
And where else are you using "str" in your program?
zipC was a string. I didn't use str anywhere else I thought it was a part of the method "startsWith".
If you have an integer number 12345678 and you divide it by 10, you get 1234567. If you divide it by 100, you get 123456, and you get 123 when you divide by 100000. Therefore, if you know the length of your zipcode, obtaining the first 3 digits is pretty easy. If the length is variable, you can either first determine the length, or use a loop to repeatedly divide by 10 until your result is small enough.

You can also use the modulo (remainder) operation to extract the last few digits. 12345678 % 10 is 8, for example. If you combine division and modulo you can extract things in the middle (although since I can't think of a single case where this would be useful with integers, you would very likely want to be using strings in those cases anyway).
Gotcha.
Step through your code and see what is going on.

The first immediate thing I see is you are doing if(str.startsWith(112)){

I am fairly certain you are intending that to be zip. The fact that 'str' hasn't being initialized (or defined) might be causing a misleading error
That was supposed to be used a way to compare with zip. I've removed the str since then.

The main thing that annoying the hell out of me is the boolean method, it's the only thing that seems to be causing me the most problem. I just don't "get" how boolean methods are structured and work. I've struggled with it previously in this course.-_-
 

MikeRahl

Member
Oct 27, 2017
421
Ohhh there is your problem.

zipC (this is a string) = zipcode (zip) (this returns a boolean)

The zipcode function is returning a boolean, but you are trying to assign it to a string. That is where your type mismatch is coming from.
 

vypek

Member
Oct 25, 2017
12,576
There you go. Guess you already caught the error then.

Since the Boolean is deciding true or false, you need to ensure that the returned value from the function is being used to dictate some decision in the main program.

Instead of assigning to a string, it's pretty easy to say

If true, zipC = "true"
Else, zipC = "false"

Or something to that effect
 

Zevenberge

Member
Oct 27, 2017
570
Now it's saying incompatible types: boolean cannot be converted to java.lang.String
In what editor are you working? I'd advise at least an editor with syntax highlighting (be it Notepad++, Vim, Ecplise, ..).
A few tips:
- Don't be afraid to be verbose. It's okay to have a variable named "zipCodeAsAString" and "zipCodeAsANumber".
- Similarly, functions do things. They have a name and don't be afraid to call them after what they do. "zipcode" would become something like "writeZipCodeToFileAndReturnWhetherItStartsWith112" (and yes, that should be a red flag :P but you'll grow in that when you get the hang of it).
- Try to keep your declarations (the first time you name a variable) as close as possible to its usage. This way it will be clearer what the type is.
This should hopefully help you in more easily tracking the errors. Error tracking is also a skill that you develop with practice.
 
Oct 27, 2017
16,630
Ohhh there is your problem.

zipC (this is a string) = zipcode (zip) (this returns a boolean)

The zipcode function is returning a boolean, but you are trying to assign it to a string. That is where your type mismatch is coming from.
I had scrapped that a while ago after some testing.
There you go. Guess you already caught the error then.

Since the Boolean is deciding true or false, you need to ensure that the returned value from the function is being used to dictate some decision in the main program.

Instead of assigning to a string, it's pretty easy to say

If true, zipC = "true"
Else, zipC = "false"

Or something to that effect
I think I'm confusing myself now as I've got a few different versions of this part opened up. one similar to what you've just typed. But there's always some roadblock.
In what editor are you working? I'd advise at least an editor with syntax highlighting (be it Notepad++, Vim, Ecplise, ..).
A few tips:
- Don't be afraid to be verbose. It's okay to have a variable named "zipCodeAsAString" and "zipCodeAsANumber".
- Similarly, functions do things. They have a name and don't be afraid to call them after what they do. "zipcode" would become something like "writeZipCodeToFileAndReturnWhetherItStartsWith112" (and yes, that should be a red flag :P but you'll grow in that when you get the hang of it).
- Try to keep your declarations (the first time you name a variable) as close as possible to its usage. This way it will be clearer what the type is.
This should hopefully help you in more easily tracking the errors. Error tracking is also a skill that you develop with practice.
I'm using Dr.Java.

I've seemed to figure out most problems in one version of the code. I may just have to take the L on this assignment.
 
Last edited:

vypek

Member
Oct 25, 2017
12,576
I think I'm confusing myself now as I've got a few different versions of this part opened up. one similar to what you've just typed. But there's always some roadblock.
Don't worry about it. Nothing wrong with that at all and when you debug programs it isn't really that odd to run into some other issue that you have to solved.

I've seemed to figure out most problems in one version of the code, now I just need to return the zip code from the while loop so that it can pass to the boolean method now. I've returned from a loop before but can't figure it out now.
Not sure if the code you posted before is your full code but I imagine you have a number of options here. You can literally "break" out of a loop if necessary. Its a keyword that breaks out of a loop and goes to the next statement. Under the right conditions you might also want to "return" from the loop using that keyword.

Also, have you been using pseudo code at all? It could really be beneficial.
 
Oct 27, 2017
16,630
Don't worry about it. Nothing wrong with that at all and when you debug programs it isn't really that odd to run into some other issue that you have to solved.


Not sure if the code you posted before is your full code but I imagine you have a number of options here. You can literally "break" out of a loop if necessary. Its a keyword that breaks out of a loop and goes to the next statement. Under the right conditions you might also want to "return" from the loop using that keyword.

Also, have you been using pseudo code at all? It could really be beneficial.
I've used pseudocode, it just this assignment has really gotten the better of me because of the detail of having to grab digits and a boolean method. I've solved most of my problems, I just need to 1) solve why I'm getting an error about not having initialized "zipcode" within my boolean method 2) solve how to get the method to pull more than one zip code while its in the loop. This is my code now:
Code:
  public static void main(String[] args) throws IOException{
    File myfile = new File("addresses.txt");
    Scanner infile = new Scanner(myfile);
    int counter = 0,houseNum,zipC;
    String street, zip;
    while (infile.hasNext()) {
      //counts amount of times ran
      counter++;
      //reads data
      houseNum = infile.nextInt();
      street = infile.nextLine();
      zip = infile.nextLine();
      isZip(zip);
      //prints read data and counter
      //System.out.println(houseNum + street);
      //System.out.println(zip); 
      //System.out.println("Ran " + counter + " times.");
    }
  }
  public static boolean isZip(String zip)throws IOException{
    String zipcode;
    if(zipcode.startsWith("112")){
      PrintWriter ofile = new PrintWriter("All112.txt");
      ofile.println(zipcode);
      return true;
    }
    else{
      PrintWriter ofile2 = new PrintWriter("allElse.txt");
      ofile2.println(zipcode);
      ofile2.close();
      return false;
    }   
  }
}
 

metaprogram

Member
Oct 28, 2017
1,174
Question #1: You have both zip and zipcode. Two separate variables. One is initialized and one isn't. Can you tell which? Do you need both?

Question #2: I'm not familiar with the Java Scanner class, but this looks fine to me on the surface. Why do you say it's not pulling more than one zip code? What is it doing instead?
 

vypek

Member
Oct 25, 2017
12,576
1) solve why I'm getting an error about not having initialized "zipcode" within my boolean method

2) solve how to get the method to pull more than one zip code while its in the loop

1) -REMOVED- Metaprogram's response is a better response since it has less direct response and is leading you to solve

2) How is the file set up? If you have it so that the zip codes are each on a new line you are already pretty close. If they are separated by commas you have to do something else.


EDIT: The way your program is now I'd assume your file has something like:


123
Main St
10020

789
Side St
10020

And you are looping over this, right? I think your loop would be fine for that kind of setup
 
Last edited:

DarkDetective

Banned
Oct 25, 2017
4,906
The Netherlands
It's pointless to check a variable if it's never assigned. You check if zipcode starts with "112" when you create zipcode (but not initialize it) literally the line above it.
(In case you don't know, initializing means assigning a value to the variable, like zipcode = "1234".)
However, there's another loose end in your code: You never use the string zip, which is the local variable you create as a parameter for isZip. ;)

Code:
public static boolean isZip(String zip)throws IOException{
    if(zip.startsWith("112")){
      PrintWriter ofile = new PrintWriter("All112.txt");
      ofile.println(zip);
      return true;
    }
    else {
      //stuff
    }
}

Wouldn't this be a much better solution?

edit: lol beaten twice
 
Oct 27, 2017
16,630
Question #1: You have both zip and zipcode. Two separate variables. One is initialized and one isn't. Can you tell which? Do you need both?

Question #2: I'm not familiar with the Java Scanner class, but this looks fine to me on the surface. Why do you say it's not pulling more than one zip code? What is it doing instead?
1)Zip is initialized. Wow, now I see zipcode was an error. Same problem I had earlier this semester with methods and passing arguments. I was thinking that it took string zip from parameters and passed it to a new variable.
2)So when it loops its taking the last zipcode and everything else isn't being written to their respective files.
1) -REMOVED- Metaprogram's response is a better response since it has less direct response and is leading you to solve

2) How is the file set up? If you have it so that the zip codes are each on a new line you are already pretty close. If they are separated by commas you have to do something else.


EDIT: The way your program is now I'd assume your file has something like:


123
Main St
10020

789
Side St
10020

And you are looping over this, right? I think your loop would be fine for that kind of setup
Its set up like:
123 Main St
10020
789 Side St
10020

It's looping but only taking the last zip code, I know it's looping properly as when I read it I included printlns to see if it read all lines in the address file.

It's pointless to check a variable if it's never assigned. You check if zipcode starts with "112" when you create zipcode (but not initialize it) literally the line above it.
(In case you don't know, initializing means assigning a value to the variable, like zipcode = "1234".)
However, there's another loose end in your code: You never use the string zip, which is the local variable you create as a parameter for isZip. ;)

Code:
public static boolean isZip(String zip)throws IOException{
    if(zip.startsWith("112")){
      PrintWriter ofile = new PrintWriter("All112.txt");
      ofile.println(zip);
      return true;
    }
    else {
      //stuff
    }
}

Wouldn't this be a much better solution?

edit: lol beaten twice
Yea, this seems to
 

MikeRahl

Member
Oct 27, 2017
421
When you start hitting up some design methodology courses, the thing they will harp on will be creating code that does one thing very well, rather doing an amalgamation of things. It might make sense conceptually to separate out the check for your zip, and doing the printing.

Pseudocode would end up something like

1. Read in Zip Code
2. check for zip starting with 112
3. do something with that information

Your function would be reduced to

Code:
public static boolean IsZip112(String zip)throws IOException{
   if(zip.startsWith("112")){
     return true;
   }
   else {
    return false
   }
}

which is much more manageable, and only does one thing.
 

metaprogram

Member
Oct 28, 2017
1,174
I think problem #2 is because you're recreating the file each time through the loop, causing it to overwrite whatever you put there the previosu time. Since that function is supposed to only *check* whether it starts with the prefix, you should pull the writing up outside of that function. Open each file once, OUTSIDE the loop, and then INSIDE the loop only write to it, depending on what isZip returns.
 

vypek

Member
Oct 25, 2017
12,576
Its set up like:
123 Main St
10020
789 Side St
10020

It's looping but only taking the last zip code, I know it's looping properly as when I read it I included printlns to see if it read all lines in the address file.

My first thought is that
Code:
houseNum = infile.nextInt();
street = infile.nextLine();
zip = infile.nextLine();

might have some issues if an address is presented on just two lines. Are you using breakpoints? Are you certain that during the loop that 123 is set as houseNum and Main St becomes street?

I haven't tested something like this before but nextInt() would grab that first 10020 zip code because "123 Main St" is not an int and the scanner would skip it, and then the next line that would be street would get assigned 789 Side st and then zip code would be forced to take the following line which is the last zip code.

I might be mistaken but that is my hunch.


EDIT: Or just listen to metaprogram instead. Lol. His post makes sense.
 
Oct 27, 2017
16,630
When you start hitting up some design methodology courses, the thing they will harp on will be creating code that does one thing very well, rather doing an amalgamation of things. It might make sense conceptually to separate out the check for your zip, and doing the printing.

Pseudocode would end up something like

1. Read in Zip Code
2. check for zip starting with 112
3. do something with that information

Your function would be reduced to

Code:
public static boolean IsZip112(String zip)throws IOException{
   if(zip.startsWith("112")){
     return true;
   }
   else {
    return false
   }
}

which is much more manageable, and only does one thing.
Makes sense, I'll keep this mind going forward thanks.
I think problem #2 is because you're recreating the file each time through the loop, causing it to overwrite whatever you put there the previosu time. Since that function is supposed to only *check* whether it starts with the prefix, you should pull the writing up outside of that function. Open each file once, OUTSIDE the loop, and then INSIDE the loop only write to it, depending on what isZip returns.
I've pulled the booean method out and put it after the while loop. I'm not sure how I'd go about the bolded as I was under the assumption the while loop was the way to pull the writing from the file. Also, with the method out wouldn't that mean I'd be testing each zip code in the method manually?
My first thought is that
Code:
houseNum = infile.nextInt();
street = infile.nextLine();
zip = infile.nextLine();

might have some issues if an address is presented on just two lines. Are you using breakpoints? Are you certain that during the loop that 123 is set as houseNum and Main St becomes street?

I haven't tested something like this before but nextInt() would grab that first 10020 zip code because "123 Main St" is not an int and the scanner would skip it, and then the next line that would be street would get assigned 789 Side st and then zip code would be forced to take the following line which is the last zip code.

I might be mistaken but that is my hunch.


EDIT: Or just listen to metaprogram instead. Lol. His post makes sense.
Not using break, I figured it wouldn't be a problem as I was grabbing the data from the zip each loop.
 

metaprogram

Member
Oct 28, 2017
1,174
I've pulled the booean method out and put it after the while loop. I'm not sure how I'd go about the bolded as I was under the assumption the while loop was the way to pull the writing from the file. Also, with the method out wouldn't that mean I'd be testing each zip code in the method manually?

I think it might help to take a step back and look at the big picture, because it seems like you're getting stuck on little things which are not really the main idea behind this exercise. Break this down into a sequence of steps. Forget about code, just break it down into a set of sentences that describe what you need to do:

If all you read is the main function (as you've currently written it above) you're going to see this sequence of steps:
  1. Open a file
  2. Until there's no more data in the file
    1. Read one entry
    2. Check if it's a zip code
That's what someone reading the body of the main function is going to see if they don't dig into all of the other stuff and see what your boolean function is going to do. To make this even clearer, we can take your original code and annotate it with comments to see this:

Code:
public static void main(String[] args) throws IOException{
   // Open a file
   File myfile = new File("addresses.txt");
   Scanner infile = new Scanner(myfile);
   int counter = 0,houseNum,zipC;
   String street, zip;

   // Until there's no more data in the file.
   while (infile.hasNext()) {
     counter++;

     // Read one entry.
     houseNum = infile.nextInt();
     street = infile.nextLine();
     zip = infile.nextLine();

     // Check if it's a zip code.
     isZip(zip);
   }
  }

Without having the body of isZip here, a casual reader would think your program does nothing. Your'e checking if it's a zip code, and then what? Nothing. To be clear, you are doing more, it just doesn't look like it, because that code is hidden and off somewhere that it shouldn't be.

What you would like it to look like is:

  1. Open a file named addresses.txt
  2. Until there's no more data in the file:
    1. Read one entry.
    2. If it starts with 112, add it to All112.txt
    3. Otherwise, add it to AllElse.txt
Forget about what this boolean function looks like for a second. Just make your main function look like that.

Easy, go back to the annotated function I wrote above and add in the new annotations:

Code:
public static void main(String[] args) throws IOException{
   // Open adresses.txt
   File myfile = new File("addresses.txt");
   Scanner infile = new Scanner(myfile);
   int counter = 0,houseNum,zipC;
   String street, zip;

   // Until there's no more data in the file.
   while (infile.hasNext()) {
     counter++;

     // Read one entry.
     houseNum = infile.nextInt();
     street = infile.nextLine();
     zip = infile.nextLine();

     // If it starts with 112, add it to All112.txt
     ????

     // Otherwise, add it to AllElse.txt
     ????
   }
  }

Just fill in the question marks with actual code.

You're going to quickly find out (well, you've actually already found out) that every time you open All112.txt or AllElse.txt, it deletes the entire contents. So, you probably want to try to avoid doing that every time. How can you alter the above sequence of steps (not the code! the bulleted list of steps) so that you don't open the files more than once? After you've done that, how can you modify the code above to reflect the new sequence of steps you've written?

Honestly, I don't want to write anymore code here, because there's no way to give any more hints that don't just completely solve the homework for you, and plus you really need to be able to get this if you're going to understand more complicated stuff later.
 
Last edited:
Oct 27, 2017
16,630
I think it might help to take a step back and look at the big picture, because it seems like you're getting stuck on little things which are not really the main idea behind this exercise. Break this down into a sequence of steps. Forget about code, just break it down into a set of sentences that describe what you need to do:

If all you read is the main function (as you've currently written it above) you're going to see this sequence of steps:
  1. Open a file
  2. Until there's no more data in the file
    1. Read one entry
    2. Check if it's a zip code
That's what someone reading the body of the main function is going to see if they don't dig into all of the other stuff and see what your boolean function is going to do. To make this even clearer, we can take your original code and annotate it with comments to see this:

Code:
public static void main(String[] args) throws IOException{
   // Open a file
   File myfile = new File("addresses.txt");
   Scanner infile = new Scanner(myfile);
   int counter = 0,houseNum,zipC;
   String street, zip;

   // Until there's no more data in the file.
   while (infile.hasNext()) {
     counter++;

     // Read one entry.
     houseNum = infile.nextInt();
     street = infile.nextLine();
     zip = infile.nextLine();

     // Check if it's a zip code.
     isZip(zip);
   }
  }

Without having the body of isZip here, a casual reader would think your program does nothing. Your'e checking if it's a zip code, and then what? Nothing. To be clear, you are doing more, it just doesn't look like it, because that code is hidden and off somewhere that it shouldn't be.

What you would like it to look like is:

  1. Open a file named addresses.txt
  2. Until there's no more data in the file:
    1. Read one entry.
    2. If it starts with 112, add it to All112.txt
    3. Otherwise, add it to AllElse.txt
Forget about what this boolean function looks like for a second. Just make your main function look like that.

Easy, go back to the annotated function I wrote above and add in the new annotations:

Code:
public static void main(String[] args) throws IOException{
   // Open adresses.txt
   File myfile = new File("addresses.txt");
   Scanner infile = new Scanner(myfile);
   int counter = 0,houseNum,zipC;
   String street, zip;

   // Until there's no more data in the file.
   while (infile.hasNext()) {
     counter++;

     // Read one entry.
     houseNum = infile.nextInt();
     street = infile.nextLine();
     zip = infile.nextLine();

     // If it starts with 112, add it to All112.txt
     if(zipCode.startsWith("112")){
        PrintWriter ofile = new PrintWriter("All112.txt");
        ofile.println(zip);


     // Otherwise, add it to AllElse.txt
     else
         PrintWriter ofile2 = new PrintWriter("allElse.txt");
         ofile2.println(zip);
   }
  }

Just fill in the question marks with actual code.

You're going to quickly find out (well, you've actually already found out) that every time you open All112.txt or AllElse.txt, it deletes the entire contents. So, you probably want to try to avoid doing that every time. How can you alter the above sequence of steps (not the code! the bulleted list of steps) so that you don't open the files more than once? After you've done that, how can you modify the code above to reflect the new sequence of steps you've written?

Honestly, I don't want to write anymore code here, because there's no way to give any more hints that don't just completely solve the homework for you, and plus you really need to be able to get this if you're going to understand more complicated stuff later.
So it'd be this then
Code:
     // If it starts with 112, add it to All112.txt
     if(zipCode.startsWith("112")){
        PrintWriter ofile = new PrintWriter("All112.txt");
        ofile.println(zip);


     // Otherwise, add it to AllElse.txt
     else
         PrintWriter ofile2 = new PrintWriter("allElse.txt");
         ofile2.println(zip);
   }
  }
 

metaprogram

Member
Oct 28, 2017
1,174
Remember what I said. Every time you open the output file it's going to delete whatever wwas there.

Which line opens the output file?

Does that happen once or more than once?

How can you fix it?
 
Oct 27, 2017
16,630
Remember what I said. Every time you open the output file it's going to delete whatever wwas there.

Which line opens the output file?

Does that happen once or more than once?

How can you fix it?
PrintWriter ofile = new PrintWriter("All112.txt") is opening the output file, its being opened more than once. Pull it out of the loop, but without the loop how does the data get written more than once is what I'm stumped with. Another loop would cause the same problem.
 

metaprogram

Member
Oct 28, 2017
1,174
Try it and see what happens first :)

You could ask the same question about your input file though. How does more than one item get read since it's created outside the loop? But that works, right?
 
Oct 27, 2017
16,630
Just wanted to thank everyone for their help earlier, I don't know why I struggled so bad with this one. I think after awhile I got a mean case of tunnel vision and then just looking at the code was pissing me off.
 

vypek

Member
Oct 25, 2017
12,576
Just wanted to thank everyone for their help earlier, I don't know why I struggled so bad with this one. I think after awhile I got a mean case of tunnel vision and then just looking at the code was pissing me off.

Got it working?

And it happens sometimes. There are times when I'm debugging an issue in the application for like 6 hours and then once I come back with a fresh mind I solve the issue in like 15 minutes. Sometimes you just need to give your mind a rest. Solutions to issues could come to you when its not even the focus of your thoughts.
 

metaprogram

Member
Oct 28, 2017
1,174
Speaking of bugs, had an interesting "bug" today at work.

We had this function that looked like this:

Code:
void doSomething() {
  if (cheapCheck) {
    doSomethingExpensive()
  }
}

cheapCheck was basically just a pointer comparison and a bit operation, and was failing 100% of the time. So basically the entirety of this function was "get into the function and then return".

In a release, fully optimized build the function accounted for close to 20% of the runtime of the entire program (literally, from a profile).

It turns out that because the code inside of the condition was large, it wasn't getting inlined, and since it was called VERY frequently, just the act of setting up a stack frame (saving and restoring registers, etc) was showing up on the profile.

The fix? Move the body of the if statement into a separate function and mark it noinline. 20% performance increase for a trivial one-liner.
 
Oct 27, 2017
16,630
Got it working?

And it happens sometimes. There are times when I'm debugging an issue in the application for like 6 hours and then once I come back with a fresh mind I solve the issue in like 15 minutes. Sometimes you just need to give your mind a rest. Solutions to issues could come to you when its not even the focus of your thoughts.
Yea, got it working. What's even more frustrating was that the solution was pretty simple. I think had I more time to just go at it with fresh eyes and not angrily looking at it I probably would've got it.
 

vypek

Member
Oct 25, 2017
12,576
20% performance increase for a trivial one-liner.

Haha. Pretty incredible stuff. :)

Yea, got it working. What's even more frustrating was that the solution was pretty simple. I think had I more time to just go at it with fresh eyes and not angrily looking at it I probably would've got it.

You probably would have. Sometimes the simple stuff gets glossed over easily. It happens. lol. But its solved now. Glad you were able to get it working.
 

MikeRahl

Member
Oct 27, 2017
421
My current frustration at work is that people don't understand the concept of garbage-in and garbage-out.

I am developing a plugin that sits between a version of AutoCAD and essentially a robot simulator (for welding together steel beams for huge building). I take the output from the CAD, which is just awful xml and text files, and turn it into 3d representations of all the objects that go into the beam and the welds that are required.

I am about the most approachable person at any step of the way so I get all the questions. The question du jour was, why aren't these welds showing up when it gets to the robot?

The issue was, for some reason, when AutoCAD encodes the output, for whatever reason it saw that we had all these similar parts, and so it just essentially randomized the weld points with these parts. So I have one weld that is next to one part, welding something else (where there is a part), but the one it THINKS it is welding to is 26 feet away... so of course it is going to fail. Because there is a weld and a similarly named part there it passes all our quality control checks so the first level thinks this is good output.

And the question that gets asked of me is how to do we make it so MY program can see this... which is pretty much impossible without a crystal ball because I can't read these peoples minds.

This was a lot more rant-y than I expected but the lesson to everyone still in academia is that users are the worst, they are your enemy and we would all be better off without them.
 

Deleted member 8166

Account closed at user request
Banned
Oct 26, 2017
4,075
i know this is programming, but ITIL is part of IT so I thought I'll ask here. anyone got a good youtube channel with videos to ITIL foundation 2011? I really need more than this binder they gave us.
 

Monorojo

Banned
Oct 27, 2017
1,673
I want to program video games but I'm 30 and have only the very basic knowledge of programming from when i was a kid and read a few C++ / C# / VB.Net books.

Is it too late for me?
 

Deleted member 8166

Account closed at user request
Banned
Oct 26, 2017
4,075
I want to program video games but I'm 30 and have only the very basic knowledge of programming from when i was a kid and read a few C++ / C# / VB.Net books.

Is it too late for me?
I just turned 31 and I started re-education in application development (2 year class)
the oldest student here is 54. most of us don't have ANY background in programming.
it's not too late for you.
 

Deleted member 21186

User requested account closure
Banned
Oct 28, 2017
55
Hi folks, looking for a bit of help.

Code:
        std::vector<byte> password = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
        for(int i = 0; i < size; i++)
        {
          if(!isdigit(password[i]))
            return error;
        }

In the following code example, the password input must be decimal only. My isdigit() is not working correctly.

The documentation states: 'Checks if the given character is one of the 10 decimal digits: 0123456789.'

I am inputting 8 hex values but they are getting checked against the ascii values 0x30-0x39 and failing. From reading a bit on SO, do I need to convert my password values to ascii first of all in order to check if all the values are decimal?
 
OP
OP
Moosichu

Moosichu

Member
Oct 25, 2017
898
Hi folks, looking for a bit of help.

Code:
        std::vector<byte> password = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
        for(int i = 0; i < size; i++)
        {
          if(!isdigit(password[i]))
            return error;
        }

In the following code example, the password input must be decimal only. My isdigit() is not working correctly.

The documentation states: 'Checks if the given character is one of the 10 decimal digits: 0123456789.'

I am inputting 8 hex values but they are getting checked against the ascii values 0x30-0x39 and failing. From reading a bit on SO, do I need to convert my password values to ascii first of all in order to check if all the values are decimal?

So what behaviour *are* you expecting? Shouldn't this fail as 0x55 is the ASCII code for `U`? How is this behaviour not within expectations?
 

metaprogram

Member
Oct 28, 2017
1,174
Hi folks, looking for a bit of help.

Code:
        std::vector<byte> password = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
        for(int i = 0; i < size; i++)
        {
          if(!isdigit(password[i]))
            return error;
        }

In the following code example, the password input must be decimal only. My isdigit() is not working correctly.

The documentation states: 'Checks if the given character is one of the 10 decimal digits: 0123456789.'

I am inputting 8 hex values but they are getting checked against the ascii values 0x30-0x39 and failing. From reading a bit on SO, do I need to convert my password values to ascii first of all in order to check if all the values are decimal?

Write a loop that prints the value of isdigit for all numbers between 0 and 255, then see if you can figure out your problem
 

metaprogram

Member
Oct 28, 2017
1,174
Great! Always like it when I can lead someone to the solution rather than just answering the question directly :)
 

Rune Walsh

Too many boners
Member
Oct 25, 2017
6,040
I didn't know if this was a good place to ask but Udemy has the CompTIA a+ courses and practice exams for $40. Are they reliable enough to get me through the exams?
 

Klappdrachen

One Winged Slayer
Member
Oct 26, 2017
1,630
Hi ERA, I might need your help. I started my university course in Aviation Computer Engineering last month and I'm already running into problems with the current exercise I have to hand in next week. It's basically the first step to write a Snake clone in C with the ncurses library. The instructor gave us a template with placeholders (the @XX symbols), which we are to replace with the correct code. The compiler gave me a few errors, which I was able to eliminate, but after compiling I couldn't get the executable file to run. Looking at my solution I can tell that I probably did some things horribly wrong but I just cannot tell where I fucked up.

This is the template:

Code:
// ********************************************************************************************
// MAIN
// ********************************************************************************************

int main(void) {
    int res_code;         // Result code from functions
    char* message_template = "The window has xxx lines and yyy columns";
    int msg_len;
    int min_rows = 3;

    // Here we start
    @01                             // Init various settings of our application

    msg_len = @02                   // Compute length of our template

    // Maximal LINES and COLS are set by curses for the current window size.
    // Check if the window is large enough to display our message
    if ( LINES < @03       || COLS < @03     ) {
        // Cleanup special curses settings and restore the normal terminal functionality
        @04
        // Print a conventional error message via printf.
        // Note: this only work after the call to  cleanupCursesApp();
        printf("Window too small: we need at least%dx%d\n", @05, @05 );

        // Set the result code to report the error
        res_code = RES_FAILED;
    } else {
        // Center output
        int mid_row = @06
        int start_col = @06

        // Write letter A to the top    left  corner of our display
        @07          // Move to position
        @07          // Put character there

        // Write letter B to the top    right corner of our display
        // Use combination of move() and addch() functions
        @08
        // Write letter C to the bottom right corner of our display
        @09
        // Write letter D to the bottom left  corner of our display
        @10
 
        // Write our message centered onto the display
        mvprintw(mid_row, start_col,"The window has %3d lines and %3d columns", @11, @11);

        // Refresh the screen in order to show all changes on the screen
        @12
     
        // Wait for user to press a key
        @13                   // make getch to be a blocking call
        getch();

        // Set the result code to report success
        res_code = RES_OK;

        // Cleanup special curses settings and restore the normal terminal functionality
        @14
    }
    return res_code;
}

This is what I came up with

Code:
// ********************************************************************************************
// MAIN
// ********************************************************************************************

int main(void) {
    int res_code;         // Result code from functions
    char* message_template = "The window has xxx lines and yyy columns";
    int msg_len;
    int min_rows = 3;

    // Here we start
    initializeCursesApplication(); // Init various settings of our application

    msg_len = strlen(message_template);  // Compute length of our template

    // Maximal LINES and COLS are set by curses for the current window size.
    // Check if the window is large enough to display our message
    if ( LINES < min_rows       || COLS < msg_len     ) {
        // Cleanup special curses settings and restore the normal terminal functionality
        cleanupCursesApp();
        // Print a conventional error message via printf.
        // Note: this only work after the call to  cleanupCursesApp();
        printf("The window is too small: we need at least %dx%d\n", msg_len, min_rows);

        // Set the result code to report the error
        res_code = RES_FAILED;
    } else {
        // Center output
        int mid_row = LINES/2;
        int start_col = COLS/2;

        // Write letter A to the top    left  corner of our display
        move(0,0);          // Move to position
        addch('A');          // Put character there

        // Write letter B to the top    right corner of our display
        // Use combination of move() and addch() functions
        mvaddch(0,COLS-1,'B');
        // Write letter C to the bottom right corner of our display
        mvaddch(LINES-1,COLS-1,'C');
        // Write letter D to the bottom left  corner of our display
        mvaddch(LINES-1,0,'D');
 
        // Write our message centered onto the display
        mvprintw(mid_row, start_col,"The window has %3d lines and %3d columns", LINES, COLS);

        // Refresh the screen in order to show all changes on the screen
        refresh();
     
        // Wait for user to press a key
                           // make getch to be a blocking call
        getch();

        // Set the result code to report success
        res_code = RES_OK;

        // Cleanup special curses settings and restore the normal terminal functionality
        cleanupCursesApp();
    }
    return res_code;
}

Sorry for the big post. I omitted the first part ("Standard curses initialization and cleanup") to make it more readable. I would really appreciate it if someone could look over this, it's driving me insane...

Edit: Is there a way to write code in colour? I tried to colourise the placeholders and my answers but everything's still white on black.
 
Last edited:

Zevenberge

Member
Oct 27, 2017
570
What error are you getting? Also, what is the application exit code and exit codes of the external functions you call?