picking up

let's say for example you want to input a number

containing 5 digits

well i used the scanner class for this one

say: 12345

how are you going to ensure that the user inputs

will not exceed 5 digits or less than 5 digits?

[257 byte] By [nemo666a] at [2007-11-27 11:45:44]
# 1

1. Make sure it is greater than 9999 and less than 100000

2. Read value in as a String and checks its length.

floundera at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 2

is there a particular function or method for determining the

size or length of the string or integer?

if in PHP there is a function such as size_of ( )

well how bout in java?

nemo666a at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 3

An int is a primitve and primitives do not have methods.

String does, check the API.

http://java.sun.com/j2se/1.5.0/docs/api/

floundera at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 4

> if in PHP there is a function such as size_of ( )

> well how bout in java?

PHP is (or can be) relaxed about the type of a variable, Java never is.

ints are ints, and Strings are Strings. ints are used to represent integer numbers and, as such, do not have a size. What has a size (length) is some particular String representation of a number (for instance as a base 10 numeral). So, you either do as flounder says and read it in as a String - and if you want the corresponding number use Integer.parseInt(). Or you read it in as an int - and if you want to check the length of some corresponding String numeral use Integer.toString().

pbrockway2a at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 5

I have found the solution to my code, with regards to the if else

but what if I wanna do something exploit to my code such as

lets say, i wanna input a single number with 5 digits

and satisfies the condition whether it exceeds or not

"then it will print seperated outpout"

sample:

I input : 12345

then it prints 1 2 3 4 5 how's that? obviously the condition should be at the bottom of this thing right? so what solution am i gonna do so that the input will be printed like that

nemo666a at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 6

I'd suggest reading the input as a String. That way you can use it's methods to see if it has the correct numbers of digits. If so, use another method inside a loop that can extract each digit out one at a time.

Make an attempt yourself. If you have troubles, post your code (using code tags), ask a specific question and include error messages in full.

floundera at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 7

actually this thing is about a machin problem from an e-book that i downloaded

NOTE: this is not a homework ok..im just practicing to shift career as a software dev.

here's the problem

http://img267.imageshack.us/img267/2831/230ay4.jpg

nemo666a at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 8

here's my own code

kindly fix it so that it will print the number input as 1 2 3 4 5

import java.util.Scanner;

public class _2_30

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

System.out.print("ENTER NUMBERS: ");

int num = input.nextInt();

System.out.println();

if(num<= 99999 && num>9999)

{

System.out.print("your input is exactly 5 digits");

}

else if(num > 99999)

{

System.out.print("your input is exceeds 5 digits");

}

else

System.out.print("your input is lesser than 5");

//end if

}//end method

}//end class

nemo666a at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 9

> kindly fix it so that it will print the number input as 1 2 3 4 5

Nope! It is not my problem. If I do it for you, what would you learn? You will learn more by doing yourself.

As I said in previous reply, I'd use a String, it will be easier. Are you doing any math with the input? If no, then why does it have to be an int.

If you insist on going down the int path, then think about the following lines of code.

int number = 4321;

System.out.println(number % 10);

System.out.println(number / 10);

floundera at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 10

> As I said in previous reply, I'd use a String, it

> will be easier. Are you doing any math with the

> input? If no, then why does it have to be an int.

>

> If you insist on going down the int path, then think

> about the following lines of code.

> > int number = 4321;

> System.out.println(number % 10);

> System.out.println(number / 10);

>

I made it int for the int num = input.nextInt()

nemo666a at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 11

> I made it int for the int num = input.nextInt()

Yes I saw that. Do you have a question?

floundera at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 12

if it's ok..kindly show me your technique the string thing you're talkin about.

im about to nosebleed now

nemo666a at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 13

Whilst int doesn't have methods, Integer does have a lot of static methods that can be use to do useful things to ints.

RedUnderTheBeda at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 14

I don;t get why a String will be easier... it's easier with an int I think.

Validation

if int <10000 or int > 99999 = invalid

And printing isn't bad either with a clever for loop and / and%.

?

cotton.ma at 2007-7-29 18:03:24 > top of Java-index,Java Essentials,New To Java...
# 15

> if it's ok..kindly show me your technique the string

> thing you're talkin about.

> im about to nosebleed now

String s = get input;

loop (number of chars in s) {

print current char

print space

}

floundera at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 16

or a nice regular expression :)

jwentinga at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 17

Oh come on! Do you really think OP can manage a regex?

floundera at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 18

public class Test{

public static void main(String args[]){

int x = 123456789;

for(int n=8;n>=0;n--){

int divisor = (int) Math.pow(10,n);

System.out.println(x/divisor);

x = x % divisor;

}

}

}

cotton.ma at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 19

> I don;t get why a String will be easier... it's

> easier with an int I think.

>

> Validation

>

> if int <10000 or int > 99999 = invalid

>

> And printing isn't bad either with a clever for loop

> and / and%.

>

> ?

can you show me your version of coding with regards to my code above? ^ ^

nemo666a at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 20

> can you show me your version of coding with regards

> to my code above? ^ ^

Are you really that lazy?

CaptainMorgan08a at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 21

> > I don;t get why a String will be easier... it's

> > easier with an int I think.

> >

> > Validation

> >

> > if int <10000 or int > 99999 = invalid

> >

> > And printing isn't bad either with a clever for

> loop

> > and / and%.

> >

> > ?

> can you show me your version of coding with regards

> to my code above? ^ ^

So you mean can I rewrite the code I wrote so it works for yours?

I gotta tell you I don't see that happening.

Why don't you try and figure out how mine works and change it so that it works with yours. It's not rocket science.

cotton.ma at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 22

> can you show me your version of coding with regards to my code above? ^ ^

Every heard of the phrase "Don't burn your bridges"? Well all I see is a smouldering pile of rubble.

NO HELP FOR YOU!

floundera at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 23

turn this into your teacher. im sure theyll get a good laugh.

public class Splitter{

public static void main(String[] args){

int number = 1234;

int numDigits = (int)((Math.log(number) / Math.log(10)) + .00000001) + 1;

int[] digits = new int[numDigits];

for(int i = numDigits; i > 0; i--){

digits[i - 1] = number % 10;

number /= 10;

}

for(int i = 0; i < numDigits; i++){

System.out.print("" + digits[i] + " ");

}

}

}

TuringPesta at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 24

> int numDigits = (int)((Math.log(number) / Math.log(10)) + .00000001) + 1;

TuringPest, are you high again? ;-)

CaptainMorgan08a at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 25

>> int numDigits = (int)((Math.log(number) /

> Math.log(10)) + .00000001) + 1;

> TuringPest, are you high again? ;-)

Its totally Javas fault, lol. Log(num)/Log(10) fails for boundary

numbers like 10,100,1000 because of accuracy problems so

you have to add the .00000001.

Also, theres no "log to the base of" function.

Otherwise it would look quite nice! : )

numDigits = log(num, 10) + 1;

and while not high, its almost 3 am and im bouncing

off the walls on lemon lime gatorade, lol.

TuringPesta at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 26

i noticed that most of you here are veteran and you don't use

the printf? that came from jdk 5.0? I always see that + sign whenever

you system.out. ting

nemo666a at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...
# 27

printf? I prefer format.

BigDaddyLoveHandlesa at 2007-7-29 18:03:29 > top of Java-index,Java Essentials,New To Java...