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 18 of 18

Thread: Beginnner Java, Assignment Help (Ending a code)

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginnner Java, Assignment Help (Ending a code)

    So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data. Can anyone provide guidance here?


    assign1.jpg


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Here it is in text:


    public static void main(String[] args) {

    int number;

    Scanner input = new Scanner (System.in);
    System.out.print("Please enter a 5 digit number: ");

    number = input.nextInt();
    int myInteger = number;
    String myString = "";
    myString = Integer.toString(myInteger);


    {
    if (number < 10000)
    System.out.print("\t ***You have not entered enough digits. Please try again.");
    if (number > 99999)
    System.out.print("\t ***You have entered too many digits. Please try again.");
    System.exit(0);
    }

    if(number >= 10000)
    if(number <= 99999)

    System.out.print("\n");
    System.out.print("\n");
    System.out.println("The individual digits of this number are: ");

    System.out.println((number/10000) % 10);
    System.out.println((number/1000) % 10);
    System.out.println((number/100) % 10);
    System.out.println((number/10) % 10);
    System.out.println(number % 10);

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Did you have a question about the code? If its output is wrong, post the output and add some comments describing what the output should look like.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    I see where you are converting your integer to a string but not doing anything with it.
    One route you could go with strings:

    PsuedoCode
    //syso() = system.out.println()
     
    if myString.length is not 5 then {
      syso("RTFM");
      startOver();
    } else {
      for each char in myString {
        syso(char)
      }
    jobsDone();
    }

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Sorry, I forgot this code at the end.


    System.out.print("\n");
    System.out.println("Your number listed as a String: " + myString);

    The teacher wanted us to to display the numbers individually, as well as a string

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Untitled.jpg

    I have attached what happens when I run. The picture on the top is what its supposed to look like, when they enter the right amount of digits.
    The picture inside the bold box is what happens when they enter an incorrect number. Everything is right, except I can't figure out how to remove the text that says "your individual digits.. etc".

    If someone were to enter 3 digits, I want them to say they have an error and that is all.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Please copy and paste the console output here, not as an image.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Please enter a 5 digit number: 45632
    Thank you.

    The individual digits of this number are:
    1st Digit: 4
    2nd Digit: 5
    3rd Digit: 6
    4th Digit: 3
    5th Digit: 2

    Your number listed as a String: 45632
    _____________________________________

    Please enter a 5 digit number: 456
    ***You have not entered enough digits. Please try again.Thank you.

    The individual digits of this number are:
    1st Digit: 0
    2nd Digit: 0
    3rd Digit: 4
    4th Digit: 5
    5th Digit: 6

    Your number listed as a string: 456

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Is that the program's output? Can you explain what is wrong with it?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    I believe I had. The first part is correct (When a user enters 5 digits).

    Now when the user enters an incorrect number (anything other than 5 digits). I want an error message to occur, and solely the error message. It continues to express the individual digits despite the error. I almost want the program to stop when there is an incorrect input.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    What problems are you having with that?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    I tried system.exit(0), after the if statement's that determine the number is less or more than 5 digits. But that will end the program even if the user enters 5 digits. So the problem I am having is that I cannot stop the 'list of digits' from appearing if the user incorrectly enters data.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Look at using an if/else statement to control what happens. The pseudo code in post#4 showed an idea.

    Please post the current version of the code. Be sure to wrap it in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Hamenopi (September 16th, 2014)

  15. #14
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    import java.util.Scanner;
    public class Assignment1 {
     
    	public static void main(String[] args) {
     
    	int number;
    			Scanner input = new Scanner (System.in);
    			System.out.print("Please enter a 5 digit number: ");	
     
    	number = input.nextInt();
    	int myInteger = number;
     
    	String myString = "";
    	myString = Integer.toString(myInteger);
     
    	{
    	if (number < 10000)
    		System.out.print("\t ***You have not entered enough digits. Please try again. ");
    	if (number > 99999)
    		System.out.print("\t ***You have entered too many digits. Please try again. ");
     
    	}
    	if(number < 9999);	
    	if(number > 100000);
    		System.out.print("Thank you.");
     
     
    	System.out.print("\n");
    	System.out.print("\n");
    	System.out.println("The individual digits of this number are: ");
     
    	  System.out.println("1st Digit: " + (number/10000) % 10);
          System.out.println("2nd Digit: " + (number/1000) % 10);
          System.out.println("3rd Digit: " + (number/100) %  10);
          System.out.println("4th Digit: " + (number/10) % 10);
          System.out.println("5th Digit: " + (number % 10));
     
        System.out.print("\n");
        System.out.println("Your number listed as a string: " + myString);
     
    	}
     
     
     
     
    	}
    Last edited by Norm; September 16th, 2014 at 01:13 PM. Reason: Spaces removed from code tag

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    The code recognizes the out of range numbers and prints a message.
    What action do you want the code to take after the message is printed?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Methinks your logic is may be off.

    if(number < 9999);	//Then what?
    if(number > 100000); // Then what?
    System.out.print("Thank you.");

    low bounds : 10000
    high bounds : 99999
    && means and
    so if number is greater or equal to low bounds AND less or equal to highBounds...

    if(number >= lowBounds && number <= highBounds){
      keepGoing();
    } else {
      stop(hammerTime);
    }
    Last edited by Hamenopi; September 16th, 2014 at 02:24 PM. Reason: Forgot my }

  18. #17
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    This is my first time programming. And my first program. My logic will probably be off.. but I can't understand your post Hamenopi. Sorry

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginnner Java, Assignment Help (Ending a code)

    Have you read the tutorial about the if statement:
    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with Java Assignment (code partially complete)
    By forgiveme502 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2013, 07:39 PM
  2. no ending after decimal point.
    By miller4103 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 10th, 2013, 04:29 PM
  3. Having trouble ending a game
    By wakaxwaka in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2012, 06:33 PM
  4. program loops never ending
    By Ccogh05 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 24th, 2011, 01:42 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM

Tags for this Thread