-3

I have been working on this project for a week, and can not figure out what I have done wrong. It is supposed to accept a five digit number, then separate the number with three spaces between each number. So far all it does is not work, then spit out a line of red expletives basically saying that it doesn't work.

Can anyone point me in the right direction?

import java.util.*;

public class program1
{
    static Scanner input = new Scanner (System.in);
    public static void main (String [] args)
    {
        // variables and stuff
        int num = 00000;

        getnum(); 

        separate_number(num);  

    }
    // methods are here
    public static void getnum()
    {
        int num;
        do{
            System.out.println("Please enter a five digit number");
            num = input.nextInt();
            if (num < 10000 || num > 99999)
            {
                System.out.println("The number is not five digits");
            }
        }while (num < 10000 || num > 99999);
    }

    public static int separate_number(int num)
    {
        int digit5, digit4, digit3, digit2, digit1;
        digit5 = num % 10;
        num = num / 10;
        digit4 = num % 10;
        num = num / 10;
        digit3 = num % 10;
        num = num / 10;
        digit2 = num % 10;
        num = num / 10;
        digit1 = num % 10;
        num = num / 10;
        return Integer.parseInt(+ digit1 + "   " + digit2 + "   " + digit3 + "   " + digit4 + "   " + digit5);
    }
}
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
  • Indent your code so it's not completely unreadable. (And why do you think an integer with spaces in it is a thing?) – Wooble Apr 20 '14 at 16:39
  • "It doesn't work" is not a helpful description. Please at least give a sample input plus observed output. – Oliver Charlesworth Apr 20 '14 at 16:41
  • Are you trying to get the numbers separated as a String? Integer.parseInt will not work with the input in your code. Another problem I see is your getNum() method doesn't actually return the users input so it can be passed to the `separate_number` method. – Jeff Ward Apr 20 '14 at 16:44
  • Why do you cal this? `return Integer.parseInt(+ digit1 + " " + digit2 + " " + digit3 + " " + digit4 + " " + digit5);` – Mohsen Kamrani Apr 20 '14 at 16:45
  • Yep, your `separateNumber(...)` method should return a **String**, not an impossible to parse number. – Hovercraft Full Of Eels Apr 20 '14 at 16:45

3 Answers3

0

The function parseInt parses a string that is a number.

Integer.parseInt(+ digit1 + "   " + digit2 + "   " + digit3 + "   " + digit4 + "   " + digit5)

No matter what your digits are, "# # # # #" is not a number.

Also, you have an extra plus-sign at the beginning which, although legal, is useless. It just declares that the first digit is positive.

To separate a number into pieces, convert it to a string

(new Integer(theNumber)).toString()

and manually split the string, use String.format(s,o...), or, probably best of all:

System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));

Output: 35,634,646

From this answer

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

You had several problems:

  1. getnum does not do anything. While it does get input from the user, it never does anything with it, so isn't actually helpful. Instead, you should make it return an int, which you'll store in a variable in name.
  2. Integer.parseInt is meant to accept a string as input, and try and convert it into an integer. It will throw an error if you try feeding it an invalid number. 1 2 3 4 5 is never going to be a number, so that's why your program isn't working. In any case, you want to go the other way around -- you want to convert a number into a String.
  3. You never do anything with the string you return from separate_number.

Taken all together, a corrected version of your code might look like this:

import java.util.*;

public class program1
{
    static Scanner input = new Scanner (System.in);
    public static void main (String [] args) {
        int num = getnum(); 
        System.out.println(separate_number(num));
    }

    public static int getnum() {
        int num;
        do {
            System.out.println("Please enter a five digit number");
            num = input.nextInt();
            if (num < 10000 || num > 99999) {
                System.out.println("The number is not five digits");
            }
        } while (num < 10000 || num > 99999);
        return num;
    }

    public static String separate_number(int num) {
        int digit5, digit4, digit3, digit2, digit1;
        digit5 = num % 10;
        num = num / 10;
        digit4 = num % 10;
        num = num / 10;
        digit3 = num % 10;
        num = num / 10;
        digit2 = num % 10;
        num = num / 10;
        digit1 = num % 10;
        num = num / 10;
        return digit1 + "   " + digit2 + "   " + digit3 + "   " + digit4 + "   " + digit5;
    }
}
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
0

You can simply extract the digits of an integer in java like this and do whatever you want with them:

public static void getnum()
{
    int num;
    do{
        System.out.println("Please enter a five digit number");
        num = input.nextInt();
        if (num < 10000 || num > 99999)
        {
            System.out.println("The number is not five digits");
        }
    }while (num < 10000 || num > 99999);
}

public static int separate_number(int num)
{
    String numStr = num +"";
    String[] digits = numStr.split("");
    String separatedDigits = "";
    for (String s: digits) {
        separatedDigits += s + " ";
        System.out.println(s);
    }
    // you can return separatedDigits just change the type of the method to String
    return Integer.parseInt(numStr);//Very nonesense!
}
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66