This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

compiles but doesn't work

 
markus psmith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
Help! This code for a homework project compiles but when run the variable "count" remains unchanged.
Markus


/*
* Counter
* Smith_Ch4_5
* Mark Smith
* msmith0266@email.vccs.edu
* version 1.0 2010/1/7
*/


public class counter
{
private int count = 0;


public int increaseByOne()
{
count++;
System.out.println("counter test");
System.out.println();
return count;
}

public int decreaseByOne()
{
count-- ;
if ( count < 0 )
{
count = 0;
System.out.println("Negative Values Are Not Allowed");
}
System.out.println();
return count;
}

public int setToZero()
{
count = 0;
System.out.println();
return count;
}

public void viewCount()
{
System.out.println("The count is at "+ count);
System.out.println();
}

}

/*
* Counter
* Smith_Ch4_5
* Mark Smith
* msmith0266@email.vccs.edu
* version 1.0 2010/1/5
*/
import java.util.Scanner;
public class counterDriver
{
public static void main(String[] args)
{


String alphaCode;
while (true)
{

Scanner keyboard = new Scanner(System.in);

System.out.println("Counter Program");
System.out.println("Enter i to increase, d to decrease,");
System.out.println("z to set to zero, v to view,");
System.out.println("or q to quit");

alphaCode = keyboard.next();

counter chalk = new counter();

if (alphaCode.equals("i"))
{
System.out.println("driver test");
chalk.increaseByOne();
}
if (alphaCode.equals("d"))
{
chalk.decreaseByOne();
}
if (alphaCode.equals("z"))
{
chalk.setToZero();
}
if (alphaCode.equals("v"))
{
chalk.viewCount();
}
if (alphaCode.equals("q"))
{
System.out.println("as you wish");
System.out.println();
System.exit(0);
}
}

}
}
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please learn to use code tags.

Declare the counter object before the while(true) loop. You are making a new one on each time through the loop.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Take a close look at where you've declared and initialized the counter instance.

Edit: Ugh, too slow.
 
markus psmith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
Many Thanks!
Markus
 
markus psmith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please forgive my ignorance, but what are code tags and what's too slow?
thanks,
Markus
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Report post to moderator
markus psmith wrote:Please forgive my ignorance, but what are code tags and what's too slow?
thanks,
Markus


When you post, if you look just above the message body box you will see a button named Code. When you post code, highlight the code and press that button. It will show your code as:



It maintains your code indentaiton and formatting, making it much easier to read.

I believe that the "too slow" comment was because they posted the answer just a little after the first answer was already posted.
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Report post to moderator
By the way, welcome to Java Ranch! I'm a big fan your relative Ronald's adventures as documented by P.G. Wodehouse!

If you stick around the Ranch for awhile, and we hope you do, you'll find a common pattern is for a new user to post a question with a lot of code and not use code tags. (Generally, they also pick a user name that doesn't conform to board standards either, so you're doing better than many.) The first response tends to be "use code tags" and then there's a lag time until you get a useful answer. I decided to combine both into one. Sorry for the confusion!
 
    Bookmark Topic Watch Topic
  • New Topic