views:

426

answers:

3

so if the user types down seven with Scanner, @@@@@@@ (7) will be the output. I'll have to use a for loop statement but I can at least figure out that much. Just need help with figuring out a way to convert word numbers into a numerical number and finally into a random character.

A: 

I'll have to use a for loop statement but I can at least figure out that much.

I don't see much of anything here about an object-oriented approach. I hate seeing people take an object-oriented language and write Fortran with it.

Start with an object and break this problem up into parts:

  1. Converting words into numbers and back, without regard for input or output.
  2. Reading in a string, parsing it into words, and validating, because you can't accept all strings.
  3. Your special output needs.

Don't think on such an atomic level in terms of numbers, strings, and scanners. Create an abstraction; imagine an object and start decomposing.

duffymo
A: 

You should have been more specific about the question. Here is a very basic solution to the problem as I understood it (a very restricted solution).

Scanner in = new Scanner(System.in);
Random rand = new Random();

String word = in.nextLine().toLowerCase();
int number = 0;

// Convert a word number into a numerical number.
if (word.equals("one"))
    number = 1;
else if (word.equals("two"))
    number = 2;
else if (word.equals("three"))
    number = 3;
else if (word.equals("four"))
    number = 4;
else if (word.equals("five"))
    number = 5;
else if (word.equals("six"))
    number = 6;
else if (word.equals("seven"))
    number = 7;
else if (word.equals("eight"))
    number = 8;
else if (word.equals("nine"))
    number = 9;
else
    System.out.println("Number does not exist. ");

// Convert the number into a random character.
char character = (char) (33 + rand.nextInt(94));

// Print the character as many times as the number implies.
for (int i = 0; i < number; i++)
    System.out.print(character);
nalo
You'd want to use equalsIgnoreCase() so you don't have to worry about "Seven", "SEven", etc.
OMG Ponies
Didn't know that, ty. :)
nalo
cheaper to just downcase the word before the if block
Martin DeMello
+1  A: 

The first thing you'll need to do is convert a valid number string "ten", "twenty five", etc into a number, then pass that number into the loop.

So, you basically need to write a token parser for converting the word into a number. Defining tokens will then allow you to easily convert to an int. (I am only going to deal with ints in this example).

Obvious tokens:

  1. one, two, three, etc (one to nineteen). (Call this the digit).
  2. twenty, thirty, forty, etc (twenty to ninety). (call this tens)
  3. hundred, thousand, etc (call this power)

use " ", "and" and "-" as delimiters (common way of writing one hundred and six, or twenty-five).

You can then parse your string into tokens, and that should give you your rules to converting to an int. Starting from the right and work to the left, you follow certain rules. Recursively work with two tokens at a time. Adding along the way. Powers would be a special case where you would multiply by the preceding digit.

For example, "One hundred and twenty seven" would convert to the tokens:

DIGIT POWER TENS DIGIT

That would give use the rules (DIGIT + TENS) + (DIGIT * POWER)

or

(7 + 20) + (1 * 100) = 127

After that conversion, you can easily convert to a repeated string using your for loop.

I hope that gets you started on the right direction.

MarkPowell