I was working on Problem 17 on Project Euler:
http://projecteuler.net/index.php?section=problems&id=17
and I'm stuck; my answer is close, but not correct. I got the answer 21108 with the following code:
I do realize the code can be optimized, but I would like to simply get this to work, and not try to make this as efficient as possible.

public class Problem17 {
	String[] ones = {"one","two","three","four","five","six","seven","eight","nine"};
	String[] tens = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
	String[] hundreds = {"onehundred","twohundred","threehundred","fourhundred","fivehundred","sixhundred","sevenhundred","eighthundred","ninehundred"};
	String onethousand = "onethousand";
	public int totalValue = 0;

	public static void main(String[] args) {
		Problem17 one = new Problem17();
		one.compute();
	}
	public void compute()
	{
		for(int x=0;x<=999;x++)
		{
			System.out.println(x);
			getNumberValue(x);

		}
		totalValue+=11; /// this is for one thousand
		System.out.println(totalValue);
	}

	public void getNumberValue(int number)
	{
		int originalnumber = number;
		int ones = 0;
		int tens = 0;
		int hundreds = 0;
		int numbervalue = 0;
		while(number!=0)
		{
			// If the number >= 100 that means that it has "hundreds" in it.
			if(number>=100)
			{
				number-=100;
				hundreds++;
			}
			//This is for numbers that are less than one-hundred and greater than or equal to ten
			if(number<100&&number>=10)
			{
				number-=10;
				tens++;
			}
			if(number<10&&number>0)
			{
				number-=1;
				ones++;
			}
		}
		if(originalnumber>100){
			totalValue+=3;
			numbervalue+=3;
		}
		if(hundreds!=0){
			totalValue+= this.hundreds[hundreds-1].length();
			numbervalue+= this.hundreds[hundreds-1].length();
		}
		if(tens!=0){
			totalValue+= this.tens[tens-1].length();
			numbervalue+= this.tens[tens-1].length();
		}
		if(ones!=0)
		{
			totalValue+= this.ones[ones-1].length();
			numbervalue+= this.ones[ones-1].length();
		}
	}
}

Any help would be appreciated!

You seem to be going from 0 to 999 instead of 1 to 1000.

Right under my loop I commented a line of code that represented 1000.
(One Thousand) which is 11 letters.

You are correct, I didn't notice it :)

I see your problem. The numbers from 11-19 aren't being converted correctly (they are "eleven", "twelve", etc. not "one ten", "two ten", "three ten").

THERE WE GO!
I completely neglected that set..
I feel so silly! Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.