validate length of long user input(no arrays or strings)

Problem:Finding the amount of digits enterd by user

Page 1 of 1

6 Replies - 3526 Views - Last Post: 01 October 2009 - 07:59 PM Rate Topic: -----

#1 sotexpb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-October 09

validate length of long user input(no arrays or strings)

Post icon  Posted 01 October 2009 - 01:25 PM

I have a quick question I need some help with, I am trying to validate a long data type by the number of digits inputted by the user. I am not allowed to use strings or arrays to find the length of the value inputted.
Ex of problem, validate the users input to verify the length of numbers entered is between 15-20. Also if the value entered is not between 15-20 digits in length it should output the amount of digits inputted.

This post has been edited by sotexpb: 01 October 2009 - 01:27 PM

Is This A Good Question/Topic? 0
  • +

Replies To: validate length of long user input(no arrays or strings)

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: validate length of long user input(no arrays or strings)

Posted 01 October 2009 - 02:14 PM

For decimal (base 10) values in long, use a counter variable and the "/= 10" operation in a loop. If we could see your current effort, we could help you more.
Was This Post Helpful? 0
  • +
  • -

#3 sotexpb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-October 09

Re: validate length of long user input(no arrays or strings)

Posted 01 October 2009 - 02:21 PM

View Postmacosxnerd101, on 1 Oct, 2009 - 01:14 PM, said:

For decimal (base 10) values in long, use a counter variable and the "/= 10" operation in a loop. If we could see your current effort, we could help you more.

import java.util.*;

public class Main {

	public static void main(String[] args) {
		long number=0;
		Scanner input= new Scanner(System.in);
  
		//Prompt user to enter value
		
		System.out.println("Enter your number to validate it (10 to  15 digits): ");
		number= input.nextLong();


I need to validate the input to make sure the user enters a a value that contains inbetween 10 and 15 digits, and if they do not it should notify the user the amount of digits they entered is invalid.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: validate length of long user input(no arrays or strings)

Posted 01 October 2009 - 02:31 PM

I don't see that you have attempted to solve the digit counting, so I will help you out by providing an algorithm. If you still have problems from here, show us an attempt at implementing it in Java.

0. num <-- Get number to test
1. counter <-- 0
2. temp <-- num
3. while temp > 0
4.	 Divide temp by 10
5.	 Increment counter by 1
6. end while
7. end


Was This Post Helpful? 0
  • +
  • -

#5 sotexpb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-October 09

Re: validate length of long user input(no arrays or strings)

Posted 01 October 2009 - 03:26 PM

thanks I got it to count the total number here is the code i used.
ccnum= input.nextLong();
		int count=0;
		long temp=ccnum;
		while (temp>0){
			temp=temp/10;
			count++;
		}

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: validate length of long user input(no arrays or strings)

Posted 01 October 2009 - 03:40 PM

That looks good. Since counter is the length of your long, you now just have to use some if statements for your output.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: validate length of long user input(no arrays or strings)

Posted 01 October 2009 - 07:59 PM

Quote

I need to validate the input to make sure the user enters a a value that contains inbetween 10 and 15 digits, and if they do not it should notify the user the amount of digits they entered is invalid.


Without uysing String you will have to verify that the long entered is
> 9999999999L
and < 999999999999999999L
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1