Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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:
```Scanner x = new Scanner(System.in);

int num[] = new int;

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 , if I am, let me know!  Reply With Quote

3. ## Re: Rather Basic Question

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

Wrapper Class  Reply With Quote

4. ## The Following User Says Thank You to Zyrion For This Useful Post:

ashl7 (March 4th, 2013)

5. ## Re: Rather Basic Question

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.  Reply With Quote

6. ## Re: Rather Basic Question

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!  Reply With Quote

7. ## Re: Rather Basic Question 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:
```Scanner x = new Scanner(System.in);

int num[] = new int;

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 , 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 --- 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)  Reply With Quote

8. ## The Following User Says Thank You to Starstreak For This Useful Post:

ashl7 (March 4th, 2013)

9. ## Re: Rather Basic Question

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)  Reply With Quote

10. ## The Following User Says Thank You to Norm For This Useful Post:

ashl7 (March 4th, 2013)

11. ## Re: Rather Basic Question 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()  Reply With Quote

12. ## Re: Rather Basic Question

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)  Reply With Quote

13. ## 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!  Reply With Quote

14. ## 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.  Reply With Quote

15. ## The Following User Says Thank You to Norm For This Useful Post:

ashl7 (March 4th, 2013)

16. ## 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 --- 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!!!!!  Reply With Quote

17. ## 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  Reply With Quote

18. ## 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:

```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!  Reply With Quote

19. ## 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.  Reply With Quote

20. ## The Following User Says Thank You to Norm For This Useful Post:

ashl7 (March 5th, 2013)

21. ## Re: Rather Basic Question

Got it, I'm so stupid and thanks much it was in my if loop...I should have had variable u instead of j!  Reply With Quote

22. ## Re: Rather Basic Question 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? Start a new thread for that. Meanwhile look in the API under either:
Matcher (Java Platform SE 7 )
or
String (Java Platform SE 7 )  Reply With Quote

23. ## The Following User Says Thank You to Starstreak For This Useful Post:

ashl7 (March 6th, 2013)