aspose file tools*
The moose likes Beginning Java and the fly likes compiles but doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

compiles but doesn't work

markus psmith
Greenhorn

Joined: Jan 15, 2010
Posts: 3
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

Joined: Oct 01, 2001
Posts: 2861
    
  11

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

Joined: Mar 10, 2008
Posts: 1818
    
    7

Take a close look at where you've declared and initialized the counter instance.

Edit: Ugh, too slow.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
markus psmith
Greenhorn

Joined: Jan 15, 2010
Posts: 3
Many Thanks!
Markus
markus psmith
Greenhorn

Joined: Jan 15, 2010
Posts: 3
Please forgive my ignorance, but what are code tags and what's too slow?
thanks,
Markus
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compiles but doesn't work