• Post Reply Bookmark Topic Watch Topic
  • New Topic

need some help please with project  RSS feed

 
Cal Mil
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone, I am currently doing a project and have run into a brick wall.

The project starts with 3 stacks of "toothpicks"
A B C
11 7 6

In my project, I am supposed to allow the user to "move" toothpicks from 1 stack to another, however, I can only move the amount of toothpicks from 1 stack to another that is in the destination stack. So for instance, I can't move stack B to A because B only has 7 and A has 11. I can however move B to C, which would give B 1 toothpick and C 12 toothpicks.

I have declared my integers (starting values)
A = 11;
B = 7;
C = 6;

I also declared: Scanner keyboard = new Scanner( System.in); // to scan for user input

String userInput = ""; //reads first letter
String userInput2= ""; //reads second letter
char userResponse = ' '; // reads user input


so I start with a println that says "Enter stack from"
userInput = keyboard.next();
then a println statement that says "Enter stack to:"
userInput2= keyboard.next();

now my problem is combining the stacks which are "strings" but hold integers from the letter entered and adding them together to create the new value. As well as subtracting the the old value from what was moved. My first move would be moving stack B to C. Giving C, 12 toothpicks and B, 1 toothpick.

I know this is a lot but I really need help! I would appreciate any help.. Thank you..
 
ryan williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I can try to help. Lets start off at the beginning. Why did you make the stacks strings?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Your user input is Strings, but you can get ints or chars from these if needed. See the API documentation for Integer's parse methods, or String's charAt method.

One way to approach the logic would be to write a method, move(). It would probably take 2 parameters: The "from" stack and the "to" stack. The method should first determine whether the move is legal. If not, tell the user they can't do that. If so, then perform the move.

Experiment along these lines, and show us the code you come up with.

 
Cal Mil
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc, where can I get these documents from? It has been so long since I wrote methods, I sort of forgot how to be honest. I am in CS 102, I took 101 last semester, but the teacher was just awful.. I didn't learn much and this guy is much more difficult.

To Ryan: I wrote it in strings because I thought I would have to enter it that way in order to collect the alphabet value from the user. The alphabet value which represents a number. That's where I am having trouble. I understand the logic of what I am supposed to do but I am completely lost on how to go about writing it. Here is my code:




import java.util.Scanner;

public class ToothpickPuzzle {

/**
* @param args
*/
public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);



int A,B,C;
A = 11;
B= 7;
C= 6;

String userInput, userInput2;

System.out.println("Welcome to the Toothpick Puzzle.Think of having 3 piles of toothpicks in front of you, where there are 24 toothpicks total:"); // intro
System.out.println(" Stack: A B C");
System.out.println("Number of Toothpicks: 11 7 6 ");
System.out.println("The goal is to create 3 piles of 8 toothpicks in exactly 3 moves.");
System.out.println("A move consists of moving toothpicks from one stack to a second stack, where the number of toothpicks moved is exactly the number that is in the destination stack.");
System.out.println("In other words, to move from stack B (7 toothpicks) to stack C (6) as shown above, we would move 6 from B to C, leaving us with 1 in B and 12 in stack C.");
System.out.println("Here we go...");

System.out.println(" Stack: A B C");
System.out.println("Number of Toothpicks: 11 7 6 ");

System.out.print("1. Enter stack from: ");
userInput = "keyboard.next()";

System.out.print(" Enter stack to: ");
userInput2= "keyboard.next()";

}

}
 
Cal Mil
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and that's where I am.. :/// lol
 
ryan williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like putting in some time at the sun tutorials would be good. Here is the section on methods.

sun tutorial
 
Cal Mil
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cal Mil, please UseAMeaningfulSubjectLine next time.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cal Mil wrote:marc, where can I get these documents from? ...

Bookmark the API Documentation for Java 6.

In the lower left pane is a listing of all classes. If you look up the class Integer, for example, you will see that it has a static method called "parseInt" that takes a String as an argument and returns an int. So you can do things like this...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!