Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2005, 12:07 PM   #1
Zett
Newbie
 
Zett's Avatar
 
Join Date: Jan 2005
Location: Canada
Posts: 2
Rep Power: 0 Zett is on a distinguished road
Send a message via MSN to Zett
Help plz

Hi, I'm new to Java but that's what I'm learning now and I've got a question. My task is to generate a random phone number of format xxx-xxx-xxxx. The first part can not contain 8 or 9, and the second part has to be less than 742, nothing special about the third part. I only have to generate and output it but I want to make it so that the program asks user if he wants to generate another number or not. My question is below the code.
Here's what I came up with
//****************************************
// phonenumber.java
// This program generates a random
// phone number of the format xxx-xxx-xxxx
//****************************************

import java.util.Scanner;
import java.util.Random;
public class phonenumber {
	public static void main (String[] args) {
	int numb1, numb2, numb3, num2, num3, ans;
	Random number = new Random();
	numb1 = number.nextInt(8);
	numb2 = number.nextInt(8);
	numb3 = number.nextInt(8);
	num2 = number.nextInt(742);
	num3 = number.nextInt(10000);
	System.out.println ("A random phone number is: " + numb1 + numb2 + numb3 + "-" + num2 + "-" + num3);
	Scanner answer = new Scanner(System.in);
	
	System.out.print ("Would you like to generate another phone number? Enter 1 to continue, 0 - to quit: ");
	ans = answer.nextInt();	
	while (ans != 0 && ans == 1)
		{
		numb1 = number.nextInt(8);
		numb2 = number.nextInt(8);
		numb3 = number.nextInt(8);
		num2 = number.nextInt(742);
		num3 = number.nextInt(10000);
		System.out.println();
		System.out.println ("A random phone number is: " + numb1 + numb2 + numb3 + "-" + num2 + "-" + num3);
		System.out.print ("Would you like to generate another phone number? 0/1: ");
		ans = answer.nextInt();
		}
	}
}

Where can I insert an if-statement (or if-else) so that if the answer isn't 0 or 1 it will say Something like "Error, try again" and will promt to enter 0 or 1 (I'm not using y/n , because 0 and 1 will do)?
I tried but the if-statement is always executed and as a result I get a prompt twice.

Any suggestions? Also, if you can help me to find an easier way of writing the code plz tell me. I repeat, I'm only in the beginning of my way in programming . Thanks in advance.
__________________
M.A.C.I.N.T.O.S.H. - Machine Always Crashes, If Not, The Operating System Hangs.
Zett is offline   Reply With Quote
Old Feb 17th, 2005, 12:16 AM   #2
Zett
Newbie
 
Zett's Avatar
 
Join Date: Jan 2005
Location: Canada
Posts: 2
Rep Power: 0 Zett is on a distinguished road
Send a message via MSN to Zett
never mind, I solved it.
Thanks for 29 views at least...
__________________
M.A.C.I.N.T.O.S.H. - Machine Always Crashes, If Not, The Operating System Hangs.
Zett is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC