• 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.

RedDragonXXX

Senior RAM Pornographer
Joined
Mar 3, 2005
Location
Jacksonville, FL
Hey guys I'm working on this project that's supposed to take integer input from the user and store it in array one integer at a time since the integer will be more then 20 digits. What I did is take users input as a String then used charAt to segment each character one by one.

The problem is when I stored it in array it gets stored as ASCII char code and I need it to be integer. Here is the code for my method:

Code:
public void inputHugeIntegere()
	{
		int stringLength;
		
		
		Scanner scan = new Scanner (System.in);
		System.out.print("Please enter your huge integer: ");
		input = scan.next();
		
		stringLength = input.length();
		stringLength--;

		for (int index = 0; index <= stringLength; index++)
		{
			
			
			list[index] = input.charAt(index);
			System.out.print(input.charAt(index) + "\t");
			System.out.println(list[index]);
		}
	}


So for example if I input 12345678, I get this:

1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56

Left side being string printed character at a time and right side I want it to be same as the left side but they need to be integers. If anyone knows how to deal with this is would be great help. I'm just not sure how to convert chartAt(index) to int and then stored it in my array.

Thanks again.
 
I haven't done any java programming for going on 8 years now (freshman year of college), but decided to toss my hat into the ring.

Gotcha, so what we really need here is just a way to convert a char to an int and get the proper value for that number that you are using.

What if you did a calculation converting the char to an Int and subtracting the static number to give the true value?
http://www.asciitable.com/

Subtract 48 from the value? (according to that table) will give you (char0)=48-48 = 0, (char9=57)-48 = 9
 
^ That's the idea Janus. When the character '1' is stored as a char, it has an ASCII value that's not equal to 1. It's an ASCII encoding thing.

The right way to handle it is to do it encoded value agnostic way. There's shouldn't be a "48" anywhere in the code because it's confusing for the reader and requires you to know that 48 just happens to be the ASCII code for '1'.

I would do it this way (you don't need to make a function):
Code:
public int fromChar(char c)
{
    return c - '0';
}
You should make sure there are no non-number characters in the string. You can use Character.isDigit(c) for that if you want.

There are a few Java library classes that will do this for you, but it's just as easy to do it this way.
 
I haven't done any java programming for going on 8 years now (freshman year of college), but decided to toss my hat into the ring.

Gotcha, so what we really need here is just a way to convert a char to an int and get the proper value for that number that you are using.

What if you did a calculation converting the char to an Int and subtracting the static number to give the true value?
http://www.asciitable.com/

Subtract 48 from the value? (according to that table) will give you (char0)=48-48 = 0, (char9=57)-48 = 9

That's a really good idea in theory but we were strictly advised to convert the char code.


^ That's the idea Janus. When the character '1' is stored as a char, it has an ASCII value that's not equal to 1. It's an ASCII encoding thing.

The right way to handle it is to do it encoded value agnostic way. There's shouldn't be a "48" anywhere in the code because it's confusing for the reader and requires you to know that 48 just happens to be the ASCII code for '1'.

I would do it this way (you don't need to make a function):
Code:
public int fromChar(char c)
{
    return c - '0';
}
You should make sure there are no non-number characters in the string. You can use Character.isDigit(c) for that if you want.

There are a few Java library classes that will do this for you, but it's just as easy to do it this way.

We will not be using any non numbers in the string, I was assure of that. It will just be string of digits up to 25 digits long.

Also I'm little confused as to what that method will do?

I need a way to convert the char to an int and after that store it in the array which all needs to be done inside that method. I've tried casting it and no luck, keeps returning ASCII char value code.
 
The code I gave you takes a char called "c" and converts it to its int value. So the character '1' will become the int 1. It does it in the same way Janus recommended - essentially subtracts 48 from the char code.

If you *REALLY* don't want to use the char value (which is the correct way to do it IMO) then you can have a 10-way if or switch statement.

if (c == '0') return 0;
if (c == '1') return 1;
if (c == '2') return 2;

etc, but that seems silly to me.

Code:
public void inputHugeIntegere()
	{
		Scanner scan = new Scanner (System.in);
		System.out.print("Please enter your huge integer: ");
		input = scan.next();

		for (int index = 0; index < input.length(); index++)
		{
			char c = input.charAt(index);
			int charValue = c - '0';
			list[index] = charValue;
			System.out.print(input.charAt(index) + "\t");
			System.out.println(list[index]);
		}
	}
 
Last edited:
So its been awhile since I've done this as well. I kinda got out of the whole programming thing.

Soo.. I might be talking out my butt here, but this looks fairly solid, and looks to me like it solves your problem.

Just use this method instead of charAt()

Code:
int intAt(int num, int index)
{
	String s = Integer.toString(num);
	int r = Integer.parseInt(s.substring(index, index+1));
	return r;
}

Source

I remember having this same issue, can't remember how I solved it then.

Reference:
String
Integer
 
^ This will work as well, but it's a little odd (in my opinion) to continually substring the string when you can just grab the character out of it directly. parseInt is really useful in cases where you want to process the entirety of a string as a number. Internally, parseInt uses Character.digit(), which likely does the same "c - '0'" thing above.

I say that because substring is somewhat expensive and I like to avoid string manipulation when possible.
 
You know, your whole c - 0 thing didn't make sense to me till just now.

I kind of wish I would have kept at this, but the data structures class scared me away. (I got a B though)
 
The code I gave you takes a char called "c" and converts it to its int value. So the character '1' will become the int 1. It does it in the same way Janus recommended - essentially subtracts 48 from the char code.

If you *REALLY* don't want to use the char value (which is the correct way to do it IMO) then you can have a 10-way if or switch statement.

if (c == '0') return 0;
if (c == '1') return 1;
if (c == '2') return 2;

etc, but that seems silly to me.

Code:
public void inputHugeIntegere()
	{
		Scanner scan = new Scanner (System.in);
		System.out.print("Please enter your huge integer: ");
		input = scan.next();

		for (int index = 0; index < input.length(); index++)
		{
			char c = input.charAt(index);
			int charValue = c - '0';
			list[index] = charValue;
			System.out.print(input.charAt(index) + "\t");
			System.out.println(list[index]);
		}
	}

That worked great. Reason I needed int to be stored in the array is because I'll need to add two of the numbers.
 
You know, your whole c - 0 thing didn't make sense to me till just now.

I kind of wish I would have kept at this, but the data structures class scared me away. (I got a B though)
I've worked through this problem a few times, and helped teach classes where students had to solve similar character issues, so it's sort of a well-worn path at this point. :) It's probably not the very first thing I would have thought of if I'd never encountered it before.
 
Ok so I'm at a brick wall again. This has been by far the most confusing project I have ever had.

We are required to add two of the numbers. I know it sounds simple but it really is not. First of all the previous method stored one number at a time into 25 element array and I have instantiated two object that will represent users input for two numbers.

Problem One; It is not possible two add two objects
Problem Two; It is not possible two add two numbers of that size and store them as int

I'm just lost as how we are supposed to add these two numbers? Like how can i add something that can't be stored into any primitive data type?
 
So to understand correctly, it wants to add together the two 25digit numbers?
 
So to understand correctly, it wants to add together the two 25digit numbers?

Yes. The numbers might not be 25 digits exactly but they will be big enough so that they can't be stored as int.

He wants us to add two arrays together or the two numbers that get stored inside the array.
 
I would do what you normally do when you add two numbers - long addition.

Suppose you have these two arrays:

arrayOne = [1, 5, 6]
arrayTwo = [4, 3, 9]

Normally you would add the 6 and 9 to get 15, carry the one to the next column, and repeat including the carried number. The 'repeat' aspect makes this sound a little like a loop problem to me...
 
I would do what you normally do when you add two numbers - long addition.

Suppose you have these two arrays:

arrayOne = [1, 5, 6]
arrayTwo = [4, 3, 9]

Normally you would add the 6 and 9 to get 15, carry the one to the next column, and repeat including the carried number. The 'repeat' aspect makes this sound a little like a loop problem to me...

That's what I was thinking to be only possible solution for this. I'll try to work out a loop to add numbers one by one and use mod to carry the remainder.
 
I was thinking the same thing as johan, somehow carrying the 10s digit over to the next set of arrays. Sounds fun!
 
Back