Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > Other Topics > Programming

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 04-11-2012, 11:05 PM   #61
Pooh-Bah
 
Join Date: Jan 2005
Location: Chicago
Posts: 4,412
Re: Masters In Comp Sci With No Prior Experience

As a brief update:

So far everything is going well. I have two really good teachers, and I am learning a lot. I no longer have doubts about job prospects once I graduate; completing this degree with good grades will mean that I have worked very hard/learned a lot.

Java started out with some pretty simple/interesting programs. I actually like taking this class online because I can pause the lectures and practice programming what he teaches us.

Discrete Math started out seemingly pretty easy, but then got much more difficult at a rapid pace. I have been forced to dedicate more time to that class than the Java class.

The full time job makes things more difficult...I am generally gone from 6:30am to 10:30pm every day. Anyway, so far so good.
Go_Blue88 is offline   Reply With Quote
Old 04-24-2012, 11:45 AM   #62
Pooh-Bah
 
Join Date: Jan 2005
Location: Chicago
Posts: 4,412
Re: Masters In Comp Sci With No Prior Experience

Ohhhhhh the humanity

So last night, I took my first exam in 5+ years. It was for Discrete Math. I experienced the whole post exam "Son of a...I should have done x,y,z on that problem!" dread, which is unfortunate. Hopefully they won't be too ruthless on that one.

Also, last night i had to write 3 Java programs for an assignment. Two of them went great, but then on the third one I somehow got stuck in an infinite loop using the While loop. I literally spent from 2:00am until 6:00am trying to get out of the loop, but nothing I did worked. Now I'm at work without sleeping, and I'm still really curious about what I could have done.
Go_Blue88 is offline   Reply With Quote
Old 04-24-2012, 05:13 PM   #63
adept
 
Join Date: Feb 2011
Posts: 864
Re: Masters In Comp Sci With No Prior Experience

Post your code that you were having trouble with and I'm sure someone here can help.
derada4 is online now   Reply With Quote
Old 04-24-2012, 05:35 PM   #64
Pooh-Bah
 
Join Date: Jan 2005
Location: Chicago
Posts: 4,412
Re: Masters In Comp Sci With No Prior Experience

Sure...keep in mind I only have one month of experience. I am completely new to strings...I started to get the hang of numbers, but I have a harder time picturing the conditional statement with strings.

(30 points) Write a program which receives two inputs: a string and a character. The program counts and then prints how many times the character appears in the string.

This is what I turned in:

Code:
import java.util.*;
public class COUNTING_CHARACTERS
{
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 
    String inputString;

    String theCharacter;
    char theChar;
    int count = 0;
    
    inputString = input.next();
    theCharacter = input.next ();
    theChar = theCharacter.charAt(0);

    
    System.out.println("Enter a word and a letter");
   

   int i=0; 
   int j=inputString.length()-1;
    
    while ( i<j ) {
        if (theCharacter.charAt(i)== theChar) {
        count = count + 1;
        j=j-1;
        i=i+1;
        return;
    }
       else { 
        count =0;
        }
    
   System.out.println("The the number of times this letter appears in the word you entered is" + count); 
    }
}
}

Last edited by _dave_; 04-24-2012 at 06:22 PM. Reason: [code] tags pls :)
Go_Blue88 is offline   Reply With Quote
Old 04-24-2012, 06:22 PM   #65
adept
 
Join Date: Apr 2011
Posts: 812
Re: Masters In Comp Sci With No Prior Experience

You have a whole bunch of errors.
1. Print statement in the wrong spot.
2. Incrementing both i and j.
3. Wrong starting position for j
4. Else statement unneeded -- sets the count to zero whenever a non-matching character is found.
5. Return statement ends program without printing anything anytime a matching character is found
6. Input not verified to be correct.
7. The loop doesn't always change i -- program runs forever on some input
etc.
au4all is offline   Reply With Quote
Old 04-24-2012, 06:44 PM   #66
_Pooh_Bah_
 
Join Date: Feb 2005
Location: UK
Posts: 9,146
Re: Masters In Comp Sci With No Prior Experience

Post edited to include [code][/code] bbcode tags, as you can see they make code sample much easier to read by retaining formatting

OK, now I preface this by stating I do not know java, and can not even test your code.

I guess maybe it's waiting for input before displaying it's message "Enter a word and a letter", since the input.next() commands come before the user is prompted for input. I found this tutorial that does it prompt first then input.next(), from a local uni: http://www.csc.liv.ac.uk/~frans/OldL...ek3/input.html

Looks like the infinite loop is perhaps caused by mixing up inputString and theCharacter in the while loop's if statement - I guess:
Code:
 if (theCharacter.charAt(i)== theChar)
should be
Code:
 if (inputString.charAt(i)== theChar)
But I also guess that after one match on first pass through the loop, the theCharacter.charAt(i) should resolve to theCharacter.charAt(1) - which should be null. so it'll execute the else block - in which count is set to zero (why? this will happen on every letter not matching theChar, no matter if matches have been detected in prioor parts of the input word - not I think what is intended) - and i and j, the conditionals for exiting the loop are not incremented / decremented. So once your code path fails the if(test), and execution gets in to this else block - it's never coming out, since it will just repeat the same test over and over never advancing along the input word.

At least I hope that made sense!

Last edited by _dave_; 04-24-2012 at 06:48 PM. Reason: omg I took a while to "learn Java" (read my link) and type this post, ninja'd by 20+ minutes!
_dave_ is offline   Reply With Quote
Old 04-24-2012, 07:50 PM   #67
Pooh-Bah
 
Join Date: Jan 2005
Location: Chicago
Posts: 4,412
Re: Masters In Comp Sci With No Prior Experience

Ug so I think I made the misake of trying to mimic examples from class without fully understanding the implications of what I was doing. For some reason, Strings really throw me off when it comes to loops. In order to have a condition that is true or false, don't you have to store the strings in variables?

I ended up re-working this one so many times that it seems I made things worse rather than better after a while. I just couldn't figure out why the condition while (i<j) would cause an infinite loop when I say below j=j-1...eventually this should cause j to no longer be >i right?
Go_Blue88 is offline   Reply With Quote
Old 04-24-2012, 07:55 PM   #68
Pooh-Bah
 
Join Date: Jan 2005
Location: Chicago
Posts: 4,412
Re: Masters In Comp Sci With No Prior Experience

Quote:
Originally Posted by au4all View Post
You have a whole bunch of errors.
1. Print statement in the wrong spot.
2. Incrementing both i and j.
3. Wrong starting position for j
4. Else statement unneeded -- sets the count to zero whenever a non-matching character is found.
5. Return statement ends program without printing anything anytime a matching character is found
6. Input not verified to be correct.
7. The loop doesn't always change i -- program runs forever on some input
etc.
So I should increment one or the other? I based this one on an example of a function I wrote where you enter 2 integers that produce an output x^y. I got it to work with:

while (y>0)
{
y = y-1;

i=i*x;
}

Anyway, clearly I need to sit down this week and get a better feel for loops.
Go_Blue88 is offline   Reply With Quote
Old 04-24-2012, 07:56 PM   #69
_Pooh_Bah_
 
Join Date: Feb 2005
Location: UK
Posts: 9,146
Re: Masters In Comp Sci With No Prior Experience

it's because you drop in to the else on any character non-match, once in there i and j are no longer updated and just loops e.g:

is i < j? no, well let's set count to zero. ok then is i < j yet? no? well then count=0... back to check if i<j, uh not changed yet, best set count to zero... and so on
_dave_ is offline   Reply With Quote
Old 04-24-2012, 08:01 PM   #70
_Pooh_Bah_
 
Join Date: Feb 2005
Location: UK
Posts: 9,146
Re: Masters In Comp Sci With No Prior Experience

for/while are kinda interchangeable depending on how you write it out, but a for loop may be more easily understandable and appropriate to this situation. though I find myself use a for loop almost all the time. it's easier to get right since the inrementor and the exit condition are all in the initialization at the top imo. something like:

Code:
count = 0;
for (i = 0; i < inputString.length(); i++)
{
  if (inputString.charAt(i)== theChar)
  {
    count++;
  }
}
System.out.println("The the number of times this letter appears in the word you entered is" + count);
_dave_ is offline   Reply With Quote
Old 04-24-2012, 09:27 PM   #71
old hand
 
checkm8's Avatar
 
Join Date: Dec 2007
Posts: 1,498
Re: Masters In Comp Sci With No Prior Experience

^ yep I agree (disclaimer, second year CS student here)

I try to stick to for loops (readability ftw) unless it's a situation where you don't know how many times the loop will execute, in which case the while loop is appropriate.

A for loop in actuality is a while loop with variable incrementation.

Take these two examples which will do the exact same thing:

Code:
while(true)
{
}

for(;;)
{
}
checkm8 is offline   Reply With Quote
Old 04-24-2012, 10:11 PM   #72
grinder
 
Join Date: Jan 2009
Posts: 436
Re: Masters In Comp Sci With No Prior Experience

Wow that looks like my !!@#$ code about 4 years ago. Welcome to coding!

What ide are you using? Do you know how to debug/code trace with it? This will make your job 1000 times easier.
Hypersion is offline   Reply With Quote
Old 04-25-2012, 12:45 AM   #73
Carpal \'Tunnel
 
Join Date: Sep 2002
Posts: 15,498
Re: Masters In Comp Sci With No Prior Experience

I have a bachelors degree in Computer Engineering and have a lot of experience as a software engineer. I can tell you for certain that as a software engineer I've been involved with OS design; electronics; hardware-software interfaces; algorithm design and effeciency; low level software; code optimization; etc. In short I've written a lot of code on a lot of different and varied projects. I won't quibble too much with language design and compiler writing being more in the realm of Computer Science but I know there are other people that could argue very persuasively that these are also included in the realm of software engineering

The reality is that the dividing lines between the two are pretty blurry especially in the working world. Post graduation the college courses taken matter for the first job or two but after that it's not that relevant.
adios is online now   Reply With Quote
Old 04-25-2012, 12:50 AM   #74
Carpal \'Tunnel
 
Join Date: Sep 2002
Posts: 15,498
Re: Masters In Comp Sci With No Prior Experience

Quote:
Originally Posted by checkm8 View Post
^ yep I agree (disclaimer, second year CS student here)

I try to stick to for loops (readability ftw) unless it's a situation where you don't know how many times the loop will execute, in which case the while loop is appropriate.

A for loop in actuality is a while loop with variable incrementation.

Take these two examples which will do the exact same thing:

Code:
while(true)
{
}

for(;;)
{
}
This is very good in my view. Also the do-while loop can be applied to accomplish certain things that are more awkward when using the loop constructs you mention.
adios is online now   Reply With Quote
Old 04-25-2012, 02:02 AM   #75
Carpal \'Tunnel
 
Ryanb9's Avatar
 
Join Date: Aug 2006
Location: NEVA!
Posts: 6,369
Re: Masters In Comp Sci With No Prior Experience

Quote:
Originally Posted by adios View Post
This is very good in my view. Also the do-while loop can be applied to accomplish certain things that are more awkward when using the loop constructs you mention.
I thought the only difference was that a do while loop does one loop even if the condition is false to begin with.
Ryanb9 is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -4. The time now is 03:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Copyright © 2008-2010, Two Plus Two Interactive