Valhalla Legends Forums Archive | Java Programming | BASIC PROGRAM PLEASE HELP!!

AuthorMessageTime
ViLeNT
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

[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

[/CODE]
February 1, 2008, 8:11 AM
St0rm.iD
treat it as a string and use substring() for each index.
February 1, 2008, 8:35 AM
ViLeNT
thank you ;) I got it to work just fine

[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();

  String n1 = digits.substring(0,1) ;
      String n2 = digits.substring(1,2) ;
      String n3 = digits.substring(2,3) ;
      String n4 = digits.substring(3,4) ;
      String n5 = digits.substring(4,5) ;

  System.out.println (n1+ "\t" + n2+ "\t" + n3+ "\t" + n4+ "\t" + n5+ "\t");



} // end main
} // end class Five


[/CODE]
February 1, 2008, 2:44 PM
warz
You might want to, at the very least, check to make sure they did indeed input a number with at least 5 digits.
February 2, 2008, 12:00 AM
LW-Falcon
[quote author=betawarz link=topic=17292.msg176066#msg176066 date=1201910419]
You might want to, at the very least, check to make sure they did indeed input a number with at least 5 digits.
[/quote]
He probably hasn't learned exceptions and error checking stuff in java yet.
February 2, 2008, 2:40 AM
ViLeNT
correct ;) we haven't got that far yet
February 7, 2008, 5:08 AM

Search