Results 1 to 4 of 4
  1. #1
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Question Basic Program Please Help!!

    Basically I am to create a program that does the following.

    Prompt a user to input a 5 digit value.

    When the user inputs a value such as 12345 and hits enter

    the program should put tabs inbetween each individual digit

    such as 1 2 3 4 5


    I cannot find this out for the life of me......heres sort of what I have I know its wrong but if someone could please edit my code and bold what they've changed I WOULD APPRECIATE IT!!


    heres the only encouragement my teacher has provided...


    There are two ways to do it, either treat user input as string and use substring method to get individual digits.



    Other way is to treat user input as a number, and perform division and remainder operations on it to get required digits




    Heres is my code

    Java Code:
    //********************************************************************
    //  Five.java      Author: 
    //  Program breaks apart a five-digit number.
    //********************************************************************
    
    package assignment2;
    
    import java.util.Scanner;
    
    public class Five 
    {
        //-----------------------------------------------------------------
        //  Prompts user to enter a five-digit number then outputs tab spaces between each digit.
        //-----------------------------------------------------------------
    	public static void main( String args[] )
    	{
    		String digits;
    		 
    		Scanner scan = new Scanner (System.in);
    		
    		System.out.print ("Please enter a five digit number: ");
    	  
    		digits = scan.nextLine();
    		
    		System.out.println (n1+ "\t" + n2+ "\t" + n3+ "\t" n4+ "\t"n5+ "\t");
    		
    		
    
    	} // end main
    } // end class Five
    Last edited by VinceGuad; 02-01-2008 at 09:10 AM.

  2. #2
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    this should help you to get going

    Java Code:
    		String digits;
    
    		Scanner scan = new Scanner (System.in);
    
    		System.out.print ("Please enter a five digit number: ");
    
    		digits = scan.nextLine();
            String n1 = digits.substring(0,1) ;
            String n2 = digits.substring(1,2) ;
    
            System.out.println (n1+ "\t" + n2+ "\t" );
    the parameters for substring can be confusing at first. The first parameter is the index of the string that you want to start from, the second parameter is the end index and the string will be substringed up to the endindex -1, or another way of looking at it. The first parameter is the index of the string that you want to start from, and the second parameter - the first parameter gives the length of the substring
    -- Hope that helps

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Since you number is in a string, the most simple way is use the charAt() as follows.

    Java Code:
    System.out.println(digits.charAt(0)); // First digit

  4. #4
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    OMG I GOT IT!! I LOVE YOU!! this has given me such as headache I would've never thought of that!

Similar Threads

  1. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 05:44 PM
  2. Basic Applet
    By jkswebsite in forum Java Applets
    Replies: 4
    Last Post: 01-13-2008, 09:14 PM
  3. Basic Graphic
    By jkswebsite in forum Java 2D
    Replies: 6
    Last Post: 11-26-2007, 02:19 AM
  4. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 PM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •