help

please help me with this:(

write a program that will ask the user two inputs(num and n) that will take as parameters. The program will return the nth digit in num. The rightmost digit of the number is the first digit. If n is invalid, return -1

ex. if num is 147852 and n is 2 then the return value is 5

thank you!!!

[341 byte] By [pink-firea] at [2007-11-27 11:17:14]
# 1

What have you tried?

Do you have a specific problem?

No one here will do your work for you.

dwga at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 2

'New -To-Java' doesn't mean 'Do-It-For-You'

if you (genuinely) want help, post what you've tried,

and explain what it should do Vs what it does that is wrong.

Michael_Dunna at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 3

oh ok. i'm sorry for that..

this is what i've tried.

import java.util.*;

public class Hirap

{

public static void main(String[] args)

{

double dNth;

Scanner r=new Scanner(System.in);

System.out.println("Enter Number");

double dNum=r.nextDouble();

System.out.println("Enter n");

double dN=r.nextDouble();

dNth=Math.nextAfter(dNum,dN)

System.out.print(dNth);

r.close();

}

}

i don't know what method to use:( i know though that it has to come from the math class

pink-firea at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 4

> oh ok. i'm sorry for that..

>

> this is what i've tried.

>

When posting code, please use code tags, it makes your code so much easier to read.

See: http://forum.java.sun.com/help.jspa?sec=formatting

There is no nextAfter(...) method in Java's Math class: you will have to write your own. Here's a start:

import java.util.*;

public class Hirap

{

public static void main(String[] args)

{

double dNth;

Scanner r=new Scanner(System.in);

System.out.println("Enter Number");

double dNum=r.nextDouble();

System.out.println("Enter n");

double dN=r.nextDouble();

dNth = nextAfter(dNum,dN);

System.out.print(dNth);

r.close();

}

private static double nextAfter(double num, double dn)

{

// your code here

}

}

prometheuzza at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 5

> There is no nextAfter(...) method in Java's

> Math class

There is in Java 6.

But it doesn't do what the OP is after, from the API:

"Returns the floating-point number adjacent to the first argument in the direction of the second argument."

dwga at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 6

um yea i know i think i used the wrong method do you know what i should use?

pink-firea at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 7

> ...

> There is in Java 6.

> But it doesn't do what the OP is after, from the

> API: "Returns the floating-point number adjacent to the

> first argument in the direction of the second

> argument."

Oh man, you're like my old elementary-school teacher breathing down my neck! This is the second time today.

; )

prometheuzza at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 8

> Oh man, you're like my old elementary-school teacher

> breathing down my neck! This is the second time

> today.

> ; )

I aim to please, now where's my apple.

dwga at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 9

can any of you guys answer what method i should use pleeeeeassssssseee:(( i've been trying to solve this problem for hours :((

pink-firea at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 10

> um yea i know i think i used the wrong method do you

> know what i should use?

You'll have to roll your own method.

As far as I can tell you have two options.

1. Parse the first number into a String and use it's charAt method to get the correct digit.

2. Do some division and bit manipulation exercises to extract the correct digit.

Some advice:

Don't use double, use int.

You're not checking if n is valid (the assignment states that your program should output -1 if n is invalid).

dwga at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 11

> can any of you guys answer what method i should use

> pleeeeeassssssseee:(( i've been trying to solve this

> problem for hours :((

Like I said: write you own method.

Use integers, not doubles.

prometheuzza at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 12

c'mon now break it down into small steps and solve each step:

1. Get two numbers (you've been told what kind go get)

2. Check that these are valid numbers first and return -1 if not.

3. You've been told what to do to one of the numbers that will allow you to easily extract a single digit out of it. You will first convert it into something (look at the messages above).

4. Look up the API for the object that you are converting your number to. You will find a function that lets you extract the single digit.

5.Put it all together.

Piece of cake.

petes1234a at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...
# 13

> can any of you guys answer what method i should use

> pleeeeeassssssseee:(( i've been trying to solve this

> problem for hours :((

Try some more... good exercise for your brain. As someone alredy said, you have to make your own method. Some help:

alternative 1

Use integers instead

This facts will help you:

-Integer Division (/) with 10 makes you lose your last digit (199/10=19)

-The modulus operator (%) gives you the remainder

(hint: 12%10=2)

alternative 2

Convert to a String and use the appropriate method in the String class

Message was edited by:

Ja_Lava_Java

Ja_Lava_Javaa at 2007-7-29 14:23:46 > top of Java-index,Java Essentials,New To Java...