• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Java Help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
You're right, I tested indexes that don't get anything stored inside them and they're all returning 0's. So the numbers are getting stored as you described, 12 gets stored as [1][2][0][0][0]...
 
Made some progress. I had it store numbers from right to left:

Code:
	public void inputHugeIntegere()
	{
		Scanner scan = new Scanner (System.in);
		System.out.print("Please enter your huge integer: ");
		input = scan.next();
		
		for (int index = input.length() -1; index >= 0; index--)
		{
			char c = input.charAt(index);
			int charValue = c - '0';
			list[index] = charValue;
		}
	}

So now if I add:

1111
11
2211 (equals)

but, if I add:

11
1111
22 (equals)

Also if I add:

9
8
7 (equals)
 
I'm printing from inside my add method:

Code:
	public HugeInteger add(HugeInteger i)
	{

		HugeInteger result = new HugeInteger();
		int remainder = 0;
		for (int index = input.length(); index >=0; index--) 
		{
			result.list[index] = list[index] + i.list[index] + remainder;
			if(result.list[index] < 10) 
			{
				remainder = 0;
			}
			else 
			{
			    remainder = 1;
			    result.list[index] %= 10;
			}
		}
		
		System.out.println();
		System.out.print("Your Result is: ");
		
		for (int index = 0; index < input.length(); index++)
		{
			System.out.print(result.list[index]);
		}
		return result;
	}
 
Looks like I got it working.

I had to change the remainder else condition to:

Code:
else if (remainder >= 1)
			{
	 
			    result.list[index] %= 10;
			}

Think my problems is obvious, I can fix the problem but I have hard time finding where the problem is :D Thanks to you guys for pointing stuff out.
 
Ah. So here's the problem: for (int index = input.length() -1; index >= 0; index--)

You're using the length of the input to determine where to start putting numbers into the array. So if the input is "15", it has length 2 and you'll start adding numbers at index 1, going right to left.

You're going to have to add some logic or play around with the numbers a bit to be sure you always start at the last index of the list (list.length - 1) instead of the last index of the string.

Does that make sense?
 
Instead of creating new thread I'll just ask here. Does anyone know if there is way to center string output?

To be more clearer is there a predefined method (static perhaps) that is part of Java Class Library that would let you do this?

I know how to do it with padding through String.format but that takes a lot of work. I just wanted to know if there is an easier way to align output.
 
Not that I'm aware of. Centering is relative to your column width. If you know your column width, then centering is just adding (column width - length of output) / 2 spaces before the output. I remember that because I learned to type on a typewriter. ;)
 
:-/ bummer

I found something called StringUtils but can't figure out which java library it belongs to since it has method center that's part of it.
 
StringUtils is probably in the apache-commons-lang package. Lots of great stuff in the Apache Commons.
 
Then that's definitely out of the question.

I just can't believe that there isn't a method that's part of String that lets you align it without padding manually through format.
 
It would have to take a parameter for column width, which doesn't fit very nicely in the format syntax. Also...

Code:
public static String center(String s, int columnWidth)
{
  for (int i = 0; i < (columnWidth - s.length()) / 2; i++)
  {
    s = ' ' + s;
  }
  return s;
}

It takes just a moment to implement. Grossly inefficient (should be a stringbuilder) and no right padding, but that'll do what you want, I think.
 
I had similar method written for alignment but decided to scrap it since it might be confusing to person reading it. Think I'll just stick with String.format inside toString unless I come across something more functional.
 
Back