# Rather Basic Question

• March 3rd, 2013, 04:53 PM
ashl7
Rather Basic Question
Hi all, I'm new to Java programming(and programming itself) and was looking for a website such this, to get some help once in a while and find some answers to my questions...I read the forum's principles, won't ask u to write my programs at all lol, just need to make myself better.

anyway, for a start, I have a question about manipulating the numbers of a long int...like how do I choose to work with the second number from right of a 10 digits number?
I have arrays in mind...manipulating the numbers would have been easy if I choose each number go to one slot of my array ,but I don't like it this way, cuz when I run my program, it asks from the user to to put number 1, press enter, then put number 2, press enter and so on, as I wrote it here:
Code :

```Scanner x = new Scanner(System.in);   int num[] = new int[10];   for( int counter=1; counter<num.length; counter++){ num[counter] = x.nextInt();```

I wanted the user to put the whole number in, and then I manipulate for example 4th digit of the number from left!

And also, needed to know how I can make it to accept exactly between 8-12 digits, nothing more or nothing less...

hope I'm not breaking any rules :D, if I am, let me know!
• March 3rd, 2013, 06:17 PM
Zyrion
Re: Rather Basic Question
You could use string manipulation and parse methods from the Wrapper class.

Wrapper Class
• March 3rd, 2013, 06:57 PM
Norm
Re: Rather Basic Question
Quote:

make it to accept exactly between 8-12 digits, nothing more or nothing less...
You can check if the number entered by the user is in the desired range and if not ask the user again for a number in the desired range. The code would be in a loop that wouldn't exit until the used entered a number in range.
• March 4th, 2013, 01:57 AM
ashl7
Re: Rather Basic Question
Quote:

You could use string manipulation and parse methods from the Wrapper class.

I guess what I actually need is a method that gets a long integer, and changes it into an array of integers :/
what Parse methods do is changing between strings and integers!
• March 4th, 2013, 06:35 AM
Starstreak
Re: Rather Basic Question
Quote:

Originally Posted by ashl7
Hi all, I'm new to Java programming(and programming itself) and was looking for a website such this, to get some help once in a while and find some answers to my questions...I read the forum's principles, won't ask u to write my programs at all lol, just need to make myself better.

anyway, for a start, I have a question about manipulating the numbers of a long int...like how do I choose to work with the second number from right of a 10 digits number?
I have arrays in mind...manipulating the numbers would have been easy if I choose each number go to one slot of my array ,but I don't like it this way, cuz when I run my program, it asks from the user to to put number 1, press enter, then put number 2, press enter and so on, as I wrote it here:
Code :

```Scanner x = new Scanner(System.in);   int num[] = new int[10];   for( int counter=1; counter<num.length; counter++){ num[counter] = x.nextInt();```

I wanted the user to put the whole number in, and then I manipulate for example 4th digit of the number from left!

And also, needed to know how I can make it to accept exactly between 8-12 digits, nothing more or nothing less...

hope I'm not breaking any rules :D, if I am, let me know!

Our numbering system uses base 10, so the second number from the right is the 100's column. Modulus divide the long integer by 100. {n%100, where n is the number} Then normal '/' divide by 10 to get that digit.
You then have the second to right digit without using Strings.

--- Update ---

Quote:

Originally Posted by ashl7
I guess what I actually need is a method that gets a long integer, and changes it into an array of integers :/
what Parse methods do is changing between strings and integers!

If you really need to use Strings then convert the long to a String with the toString() method. From there you can create a character array with toCharArray() or split the string using split(). (The latter is a bit more tricky)
• March 4th, 2013, 07:05 AM
Norm
Re: Rather Basic Question
Quote:

check if the number entered by the user is in the desired range
The range of numbers I was talking about is 10000000 to 999999999999 ( lowest 8 digit number to highest 12 digit number)
• March 4th, 2013, 01:46 PM
ashl7
Re: Rather Basic Question
Quote:

Originally Posted by Starstreak
Our numbering system uses base 10, so the second number from the right is the 100's column. Modulus divide the long integer by 100. {n%100, where n is the number} Then normal '/' divide by 10 to get that digit.
You then have the second to right digit without using Strings.

--- Update ---

If you really need to use Strings then convert the long to a String with the toString() method. From there you can create a character array with toCharArray() or split the string using split(). (The latter is a bit more tricky)

That is great...thanks a lot, I don't think I even need an array anymore!!! . I'm not really good working with string methods...not that professional yet lol

but let's say I made a Char array...I want to add or subtract some of the numbers in this char array together, is that possible?! I mean those numbers are not integer anymore, they are characters!

--- Update ---

btw, how can I get a long int?

for an int is the method below, but I'm not sure I can use it for 16 digit integer
Scanner x = new Scanner(System.in)
int num = x.nextInt()
• March 4th, 2013, 02:23 PM
Norm
Re: Rather Basic Question
Quote:

how can I get a long int?
long is a primitive data type.
See the tutorial: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
• March 4th, 2013, 03:26 PM
ashl7
Re: Rather Basic Question
so, it means we can't get a long int from the user?! and we can't use it as an object?
In C it was easy we just used scanf, but in java...

basically I wanted to the user put in a 15 digit number, now I'm not sure it would be easier working with arrays, or long int...cuz later on I want to work with each digit of that number!
• March 4th, 2013, 03:37 PM
Norm
Re: Rather Basic Question
There is no long int. There is an int and there is a long. See the link in post#8
Also read the API doc for the Scanner class to see what its methods can read.

You can read a 15 digit number and you can create an object with it.
• March 4th, 2013, 04:36 PM
ashl7
Re: Rather Basic Question
thanks, didn't know about API doc!!!
what's the difference of using nextLong(radix) and nextLong()?
it says in the description at the bottom that : radix - the radix used to interpret the token as an int value
so if i don't type radix in parenthesises, it will interpret the token as float or something else?!

--- Update ---

Quote:

Originally Posted by Norm
The range of numbers I was talking about is 10000000 to 999999999999 ( lowest 8 digit number to highest 12 digit number)

when I use it in my loop it says : The literal 1000000000000 of type int is out of range!!!!!
• March 4th, 2013, 04:42 PM
Norm
Re: Rather Basic Question
You need to learn how to use google to research terms you don't understand like radix.

Here is another link for the API doc:
Java Platform SE 7
• March 5th, 2013, 08:02 PM
ashl7
Re: Rather Basic Question
ok I used your help to do my program...I wrote it, but there is some problem in it that I can't figure out...
here is the summary of the problem:

let's say we have a 15 digits number ; we'll do the following to it:

Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.

Now add all single-digit numbers from above and show the result
-----------------------------------------

I made a small program to test it and see if it works
I assumed my number is 12523574
step one should be : 7*2=14>>5 , 3*2=6 , 5*2=10 >>1 , 1*2=2
step 2: 5+6+1+2 =14

now my code is below:

Code :

```import java.util.*; public class Test {     public static void main(String[] args) {     long cc=12523574;   long i,j,u; long sum = 0;   for( long q = 100; q < 100000000; q*=100){   long p = q/10; i = cc%q; j = i/p; u =2*j;   if(j>=10 && j<100){ long k= j%10; long y= j/10; long r= k + y;   sum+= r;   } else if(j<10){ sum += u; }       }         System.out.println("\nsum " + sum);   }```

but it keeps getting me 30!!! can u pls give me hint where my problem is? I'm out of ideas!
• March 5th, 2013, 08:22 PM
Norm
Re: Rather Basic Question
Try printing out the results at each step and compare them to the results you get manually to see where the program is going wrong.
• March 5th, 2013, 09:02 PM
ashl7
Re: Rather Basic Question
Got it, I'm so stupid and thanks much :D
it was in my if loop...I should have had variable u instead of j!
• March 6th, 2013, 02:20 AM
Starstreak
Re: Rather Basic Question
Quote:

Originally Posted by ashl7
I'm done with the program that I was working on,it was a part of writing a program to validate credit card numbers
now I'm off to start writing another program :))) which mostly has to do with strings, which I kind of suck at!!

if I get a string from a user, how can I separate the digits and characters of it?! like if I want him to enter a name, and he enters something like jo7n, how can can I tell that's not a name?

I browsed javadoc, but it's a huge list of methods and classes, and for me, a beginner, I don't really know how to centralize my search in it...or where to look for specifically in all that information! any hint on where to start with?[-O<

Start a new thread for that. Meanwhile look in the API under either:
Matcher (Java Platform SE 7 )
or
String (Java Platform SE 7 )