I need help with a java program!!?

I need help with a java program!!?
I am writing a program that is taking a five digit number, reversing it and displaying one digit at a time. I am suppose to be using method extractNextDigit() to display the numbers one at a time. I am getting the error cannot find symbol method extractNextDigit(). I am new to programming, and my teacher is not helping at all. Here is what I have so far.

import java.util.Scanner; // program uses class Scanner

public class Project2
{

// main method begins execution of Java application
public static void main(String[] args)
{
// create Scanner to obtain imput from command window
Scanner input = new Scanner( System.in );

int number1;
int number2;
int number3;
int number4;
int number5;

System.out.print( “Number5: ” );
number5 = input.extractNextDigit();

System.out.print( “Number4: ” );
number4 = input.extractNextDigit();

System.out.print( “Number3: ” );
number3 = input.extractNextDigit();

System.out.print( “Number2: ” );
number2 = input.extractNextDigit();

System.out.print( “Number1: ” );
number1 = input.extractNextDigit();

} // end main method

} // end class Project2

Best answer:

Okay, your problem is that extractNextDigit() method doesn’t exists. This is a the method that your instructor wants you to write.

Input is an object of type Scanner and only has the scanner methods available. Look at the link bellow for documentation on all the classes and methods that you have available in java. Go to the class Scanner, read it and understand how to use it to get input from the user. According to your specs the user should input the 5 digit number all at once.

Now you need to write the method extractNextDigit() I would start this way:

public int extractNextDigit (int number) {
//check that number isn’t null
//get the next digit from number
//remove this digit from number
//return the next digit
}

This method should be called inside a while loop in main that keeps going until number is null/empty.
Also you prob have to pass number to extractNextDigit() by reference.

Tags: , , ,

Under Forum

1 Comment for I need help with a java program!!?

  • 1. synf  |  November 13th, 2007 at 11:55 pm

    The reason you are getting this error is because Java does not actually have a method called extractNextDigit()
    You can find out the methods which java offers in the Java API, this can be found by doing a search for “java api 1.6″ You have to write this method yourself.

    try:

    public static void extractNextDigit(String dig) {
    for (int x=dig.length()-1; x>=0; x–) {
    System.out.println(dig.charAt(x));
    }
    }

    for your extractNextDigit method and create a buffered reader and do:

    public static void main (String args[]) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(“Please enter a 5 digit number:”);
    String digit = in.readLine();
    extractNextDigit(digit);
    }

Leave a Comment for I need help with a java program!!?

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories