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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Rather Basic Question

  1. #1
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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[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 , if I am, let me know!


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Rather Basic Question

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

    Wrapper Class

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

    ashl7 (March 4th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,117
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #4
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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!

  6. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Rather Basic Question

    Quote Originally Posted by ashl7 View Post
    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[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 , 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 View Post
    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)

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

    ashl7 (March 4th, 2013)

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,117
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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)
    If you don't understand my answer, don't ignore it, ask a question.

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

    ashl7 (March 4th, 2013)

  10. #7
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Rather Basic Question

    Quote Originally Posted by Starstreak View Post
    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()

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,117
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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)
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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!

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,117
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    ashl7 (March 4th, 2013)

  15. #11
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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 View Post
    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!!!!!

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,117
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Rather Basic Question

    You need to learn how to use google to research terms you don't understand like radix.

    Also reading the link I posted would help.

    Here is another link for the API doc:
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  17. #13
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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!
    Last edited by Norm; March 5th, 2013 at 08:20 PM. Reason: change quote tags to code

  18. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,117
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    ashl7 (March 5th, 2013)

  20. #15
    Member ashl7's Avatar
    Join Date
    Mar 2013
    Posts
    32
    My Mood
    Mellow
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default 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!

  21. #16
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Rather Basic Question

    Quote Originally Posted by ashl7 View Post
    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 )

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

    ashl7 (March 6th, 2013)

Similar Threads

  1. Basic Question Need Help
    By Graser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 21st, 2012, 10:27 AM
  2. basic java question
    By jim213mm in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2012, 12:30 PM
  3. Basic Java question
    By fred2028 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 13th, 2011, 03:54 PM
  4. Basic java question
    By erosgol in forum Java Theory & Questions
    Replies: 5
    Last Post: September 2nd, 2011, 05:24 PM
  5. [SOLVED] Asking what I suspect to be a very basic question
    By Noobert in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 24th, 2010, 07:42 AM