Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 11-16-2007, 08:39 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
question regarding exceptions..
Hey,

This might be a very simple question.
I'm writing a simple program where the user is asked to enter an integer.

System.out.println("Please enter an integer");
a = scanner.nextInt();

So i'm expecting him to enter an integer, but lets say that the user didnt enter an integer instead he entered a word or anything other than an int. I need to print him a message saying "Sorry... you have to enter an integer.." and then reprompt him again to enter an integer "Please enter an integer". Basically what should i do ???

Should I write an "if statement" or should i try to catch the exception ???
I hope somebody can clarify this point.. maybe with an example.

Any help would be appreciated. Thanks alot.


Heres the code:


public class DemoProgram2{


public static void main(String[] args){


Scanner scanner;

int x; //raw date information

//create scanner for reading user keyboard input
scanner = new Scanner(System.in);

//read and int representation of the date from the keyboard
System.out.print("Enter birthday as an DDMMYYYY int and [ENTER]: ");
x = scanner.nextInt();

//output the data collected
System.out.println("Your Data");
System.out.println("=========");
System.out.println("x=" + x);
System.out.println("Your Birthday");


int day = (x - x % 1000000) / 1000000;
int month = (x%1000000 - x % 10000)/10000;
int year = x % 10000;
System.out.println(day + "/" + month + "/" + year);

}


}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-16-2007, 10:16 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.util.Scanner; public class DP2 { public static void main(String[] args) { //create scanner for reading user keyboard input Scanner scanner = new Scanner(System.in); String line; do { //read and int representation of the date from the keyboard System.out.print("Enter birthday as an DDMMYYYY int and [ENTER]: "); line = scanner.nextLine(); } while(!process(line)); scanner.close(); } private static boolean process(String s) { char[] chars = s.toCharArray(); if(chars.length != 8) return false; for(int j = 0; j < chars.length; j++) { if(!Character.isDigit(chars[j])) return false; } int x = Integer.parseInt(s); //output the data collected System.out.println("Your Data"); System.out.println("========="); System.out.println("x=" + x); System.out.println("Your Birthday"); int day = (x - x % 1000000) / 1000000; int month = (x%1000000 - x % 10000)/10000; int year = x % 10000; System.out.println(day + "/" + month + "/" + year); return true; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-17-2007, 10:46 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
Thanks alot for your help.
Do you know where should I add the

System.out.println("Sorry, thats not acceptable.. enter an int");

Thanks again.. really appreciate it.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-17-2007, 11:31 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
String line; boolean okay = true; do { if(!okay) System.out.println("Sorry, thats not acceptable.. enter an int"); System.out.print("Enter birthday as an DDMMYYYY int and [ENTER]: "); line = scanner.nextLine(); } while(!(okay = process(line)));
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
How to use chained exceptions Java Tip java.lang 0 04-04-2008 04:50 PM
Defining own Exceptions JavaForums Java Blogs 0 11-12-2007 06:00 PM
Runtime Exceptions Java Tip Java Tips 0 11-12-2007 12:31 PM
how to handle exceptions paty Advanced Java 2 08-05-2007 06:17 AM
Question on Exceptions yelllow4u New To Java 6 07-27-2007 03:41 PM


All times are GMT +3. The time now is 09:38 PM.


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