Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

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

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "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)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

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

    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

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

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

    Have you figured it out now?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by Norm View Post
    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);



    }


    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

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

    Glad you got it working.

    In the future Please wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Like this?

    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);
     
    }
     
    }
    Last edited by SaltSlasher; February 10th, 2012 at 05:56 PM.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 < > ??

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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:
    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

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    This is how I turned in my code. She likes things in "paragraphs", and indented.


    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);
     
    	}
     
    }

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. using "user.dir" with mkdir
    By meisterluv in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 13th, 2010, 09:28 PM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM
  5. [SOLVED] Is "Public void closeFile()" a problem in the program for AS-Level computing project
    By muffin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2009, 09:12 PM