# "Separate Numbers"-Problem:Assigning Variables to User Inputs.

• February 10th, 2012, 01:30 AM
SaltSlasher
"Separate Numbers"-Problem:Assigning Variables to User Inputs.
I gave up asking my teacher and classmates because they refuse to give me an exact answer, and my Google searches have left me dry since yesterday, and I just can't figure this out.

I need to have the user input a 5 digit number like "12345" or "11111".

I then need to print out just the "1" and on a separate line the "5". The problem is, I can only get "12345" to print, and not just the "1" let alone the "5".

Then I need to add "1" and "5" together on a separate line.

Steps one and two are done, and the code is below, but-
*The third step, I am stuck on is: "Create five variables digit1-digit5, assign them the individual digits of the number entered. hint:use integer division and modulus operator % to isolate the numbers.

import java.util.Scanner;
public class Number
{
public static void main(String[] args)
{

Scanner input = new Scanner(System.in);

System.out.printf("Enter a 5 Digit Number: ");

int Number = input.nextInt();

}
}

Here are the exact instructions from the paper:

-Create a file called Number.java. It includes one single method: main. Include a comment with your name and assignment number on the top of the line.

Steps 1-4:
1) Read in a 5 digit number from the user (one single number --do not read in 5 digits)

2)Create a variable called number to store the input entered by the user. No need for input validation. In this assignment you can assume that the user enters a number in the right range.

3)Create five variables digit1-digit5, assign them the individual digits of the number entered. hint:use integer division and modulus operator % to isolate the numbers

4)Print the following info including a descriptive labels as descibed by the output below.

-the first digit
-the last digit
-the sum of the first and last digit*Pass the appropraite expression as an arguement to the printf statment, don't create a variable that stores the sum**
-The whole 5-digit number but seperate the individual digits with 2 spaces(blanks)
• February 10th, 2012, 06:09 AM
Norm
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.
Quote:

I then need to print out just the "1" and on a separate line the "5".
Are you trying to get the first letter and the last letter from a String?
Look at using the String class's substring method. It will allow you to get any character in a String.

If you are working with an int value you will need to use division (/) and modulus/remainder(% ) operators.
If you're not sure how they work, write a small simple program, use one of the operators and print out the results. Do it for many different values to see what happens. For exampl:
int x = 10;
System.out.println("div by 4=" + (x/4)); // show results when divided by 4
System.out.println("rem for 4" + (x%4)) ; // show results with modulus operator

Change the 4 and the 10 to many different values
• February 10th, 2012, 12:45 PM
SaltSlasher
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.
It had crossed my mind to use String, I found tutorials that showed me how, but my teacher insisted that I use int.

I am pretty sure the code that I wrote was what is suppose to be right.

I figured it out last night, the digit1 line is-

Enter a 5 digit number: 12345

digit1 = number / 10000; // Now I can use digit1, if I want to use the "1" out of 12345 I divide by 10,000.

//Which makes sense, because its a 5 digit number, so to grab one of each, I have to do a "math" problem to get separate numbers out of 12345, and by dividing it by 10,000, I get 1.2345, and since I am not doing a double, the value turns into "1", so digit1 == 1.

The point of the assignment was to be clever enough to figure out that the division will allow me to do what I needed.

BY having the digit1 statement, I can now plug it into a printf, System.out.printf("First Digit: %d", digit1);
• February 10th, 2012, 01:11 PM
Norm
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.
Have you figured it out now?
• February 10th, 2012, 04:31 PM
SaltSlasher
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.
Quote:

Originally Posted by Norm
Have you figured it out now?

import java.util.Scanner;
public class Number
{
public static void main(String[] args)
{

Scanner input = new Scanner(System.in);

System.out.printf("Enter a 5 Digit Number: ");

int Number = input.nextInt();

int digit1 = Number / 10000;

int digit2 = Number % 10;

System.out.printf("The First Digit: %d\n", digit1);

System.out.printf("The Last Digit: %d\n", digit2);

System.out.printf("The Sum of the First and Last Digit: %d\n", digit1 + digit2);

}

}
• February 10th, 2012, 04:32 PM
Norm
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.

• February 10th, 2012, 05:39 PM
SaltSlasher
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.
Like this?

Code java:

```import java.util.Scanner; public class Number { public static void main(String[] args) {   Scanner input = new Scanner(System.in);   System.out.printf("Enter a 5 Digit Number: ");   int Number = input.nextInt();   int digit1 = Number / 10000;   int digit2 = Number % 10;   System.out.printf("The First Digit: %d\n", digit1);   System.out.printf("The Last Digit: %d\n", digit2);   System.out.printf("The Sum of the First and Last Digit: %d\n", digit1 + digit2);   }   }```
• February 10th, 2012, 05:43 PM
SaltSlasher
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.
oh nice, that is cool, thanks. I just finished my first month of Java Programming, so I ma new to this.

Did I need to do < > ??
• February 10th, 2012, 05:58 PM
Norm
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.
Next step in posting code is to properly indent. The code in each nesting level of {} should be indented.
Something like this:
Code java:

```import java.util.Scanner; public class Number { public static void main(String[] args) {   Scanner input = new Scanner(System.in);   System.out.printf("Enter a 5 Digit Number: ");   int Number = input.nextInt();   ...   } // end main()   } // end class```
• February 10th, 2012, 06:08 PM
SaltSlasher
Re: "Separate Numbers"-Problem:Assigning Variables to User Inputs.
This is how I turned in my code. She likes things in "paragraphs", and indented.

Code java:

```import java.util.Scanner; public class Number { public static void main(String[] args) {   Scanner input = new Scanner(System.in);   System.out.printf("Enter a 5 Digit Number: ");   int Number = input.nextInt(); int digit1 = Number / 10000; int digit2 = Number % 10;   System.out.printf("The First Digit: %d\n", digit1); System.out.printf("The Last Digit: %d\n", digit2); System.out.printf("The Sum of the First and Last Digit: %d\n", digit1 + digit2);   }   }```