Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2008, 10:07 AM
Member
 
Join Date: Jan 2008
Posts: 5
VinceGuad is on a distinguished road
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

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 10:10 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-01-2008, 12:53 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
this should help you to get going

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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-01-2008, 01:01 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,512
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Since you number is in a string, the most simple way is use the charAt() as follows.

Code:
System.out.println(digits.charAt(0)); // First digit
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-01-2008, 04:35 PM
Member
 
Join Date: Jan 2008
Posts: 5
VinceGuad is on a distinguished road
OMG I GOT IT!! I LOVE YOU!! this has given me such as headache I would've never thought of that!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic question about EJB javaplus Enterprise JavaBeans 2 07-15-2008 06:44 PM
Basic Applet jkswebsite Java Applets 4 01-13-2008 10:14 PM
Basic Graphic jkswebsite Java 2D 6 11-26-2007 03:19 AM
Very basic question gvi New To Java 2 10-30-2007 07:30 PM
help with basic example fred New To Java 1 07-20-2007 06:45 PM


All times are GMT +3. The time now is 11:51 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org