prompt user for a number

need help immediately

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 748 Views - Last Post: 22 November 2009 - 08:18 PM Rate Topic: -----

#1 josephtang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-November 09

prompt user for a number

Post icon  Posted 22 November 2009 - 05:40 PM

import java.util.Scanner;
public class help {
public static void main(String[]args)
{
	int product = 0;
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Enter a number ");
	for (int i=1; i<=2000; i++ ) {
		if (keyboard.nextInt() != 999)
			System.out.println("Enter a number ");
		if(keyboard.nextInt()==999)
			product*= keyboard.nextInt();
	}
	System.out.println(product);
}
}


i suppose to keep enter a number until the number is 999. then display the product of the number i have entered except 999
here is my code but it is not working please help

This post has been edited by josephtang: 22 November 2009 - 05:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: prompt user for a number

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10549
  • View blog
  • Posts: 39,044
  • Joined: 27-December 08

Re: prompt user for a number

Posted 22 November 2009 - 05:44 PM

I would use a while loop to check for two things- one, that there is another int and two, that the nextInt isn't 999. Here's what I mean:
Scanner scan = new Scanner(System.in);
int val = 0;
int prod = 1;
while(scan.hasNextInt() && (val = scan.nextInt()) != 999)
   prod *= val;



The second condition: (val = scan.nextInt()) != 999) assigns the value of scan.nextInt() to val, then compares val to 999. This way, if the user enters a non-int value or 999, nothing happens.
Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: prompt user for a number

Posted 22 November 2009 - 05:48 PM

I would recommend using a do...while() or a while() construct rather than a for(), simply due to the fact that that is what the while() loop is built for.

So if I understood you right, you're looking for something more along these lines:
int i = keyboard.nextInt();
while(i != 999)
{
	   product *= i;
	   System.out.println("Enter a number ");
	   i = keyboard.nextInt();
}


Also, I'd just like to remind you, next time, please be more descriptive with your title.

This post has been edited by erik.price: 22 November 2009 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 josephtang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-November 09

Re: prompt user for a number

Posted 22 November 2009 - 05:54 PM

Can u guys do it in for loop cuz i haven't learn while loop yet
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: prompt user for a number

Posted 22 November 2009 - 05:58 PM

Yes you could, it's just not the prettiest thing in the world:
//I just took your original code and changed a few things
for (int i=keyboard.nextInt();;) {
		if (i != 999)
			System.out.println("Enter a number ");
		if(i == 999)
		{
			//I'm pretty sure what you have here isn't the right answer, but I'll leave it just in case.
			product*= i;
			break;
		}
		i = keyboard.nextInt();
	}

This post has been edited by erik.price: 22 November 2009 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10549
  • View blog
  • Posts: 39,044
  • Joined: 27-December 08

Re: prompt user for a number

Posted 22 November 2009 - 05:58 PM

You haven't learned what a while loop is yet?! :blink:

Alright, let's start with the structure of a for loop then:
for(var-declarations and/or initializations; boolean-conditions; var-modifications){
  ..code..
}



A while loop is essentially a for loop withot the formal var-declarations and var-modifications sections built in. It looks something like:
while(boolean-conditions){
   ..code..
}



And otherwise, works identically to the for-loop.
Was This Post Helpful? 0
  • +
  • -

#7 josephtang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-November 09

Re: prompt user for a number

Posted 22 November 2009 - 06:01 PM

public static void main(String[]args)
{
	int b =0;
	int product=1;
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Enter a number ");
	//int a = keyboard.nextInt();

	for(int i=0;i<=100000;i++)
	{
		if(keyboard.nextInt()!=999)
		{
			System.out.println("Enter a number "); 
			b = keyboard.nextInt();
			product *=b;
		}
		else
		{
			break;
		}
		
	}
	System.out.print("the product is "+(product/999));
	
}


i just fixed i again
there are some other problem
Was This Post Helpful? 0
  • +
  • -

#8 josephtang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-November 09

Re: prompt user for a number

Post icon  Posted 22 November 2009 - 07:22 PM

import java.util.Scanner;
public class asknumber {
public static void main(String[]args)
{
	int b =1;
	int product=1;
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Enter a number ");
	int a = keyboard.nextInt();
	if(a==999)
		{
			System.out.println("the product is "+(product/999));
			
		}
	for(int i=0;i<=100000;i++)
	{
		if(a!=999)
		{
			System.out.println("Enter a number "); 
			product *= keyboard.nextInt();
			System.out.println(product/999);
	
		}
	}

	}

}


the program will not enter the first if statement even if i entered 999
please help
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: prompt user for a number

Posted 22 November 2009 - 07:41 PM

View Postjosephtang, on 22 Nov, 2009 - 06:22 PM, said:

import java.util.Scanner;
public class asknumber {
public static void main(String[]args)
{
	int b =1;
	int product=1;
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Enter a number ");
	int a = keyboard.nextInt();
	if(a==999)
		{
			System.out.println("the product is "+(product/999));
			
		}
	for(int i=0;i<=100000;i++)
	{
		if(a!=999)
		{
			System.out.println("Enter a number "); 
			product *= keyboard.nextInt();
			System.out.println(product/999);
	
		}
	}

	}

}


the program will not enter the first if statement even if i entered 999
please help

What are you trying to do ?
If the user enter someting else than 999
you will loop 100000 times and

System.out.println("Enter a number ");
product *= keyboard.nextInt();
System.out.println(product/999);

your user has to be patient to enter 100000 numbers at the console

If the user enter 999
you just loop for noting 100000 times
Was This Post Helpful? 0
  • +
  • -

#10 josephtang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-November 09

Re: prompt user for a number

Posted 22 November 2009 - 07:45 PM

View Postpbl, on 22 Nov, 2009 - 06:41 PM, said:

View Postjosephtang, on 22 Nov, 2009 - 06:22 PM, said:

import java.util.Scanner;
public class asknumber {
public static void main(String[]args)
{
	int b =1;
	int product=1;
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Enter a number ");
	int a = keyboard.nextInt();
	if(a==999)
		{
			System.out.println("the product is "+(product/999));
			
		}
	for(int i=0;i<=100000;i++)
	{
		if(a!=999)
		{
			System.out.println("Enter a number "); 
			product *= keyboard.nextInt();
			System.out.println(product/999);
	
		}
	}

	}

}


the program will not enter the first if statement even if i entered 999
please help

What are you trying to do ?
If the user enter someting else than 999
you will loop 100000 times and

System.out.println("Enter a number ");
product *= keyboard.nextInt();
System.out.println(product/999);

your user has to be patient to enter 100000 numbers at the console

If the user enter 999
you just loop for noting 100000 times

I am trying to prompt user for a number. Continue to ask the user for a number until the user enters 999. then display the product ofhe number, not including 999
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: prompt user for a number

Posted 22 November 2009 - 07:50 PM

int a;
do{
	a = keyboard.nextInt(); 
		//display product num or whatever
}while(a!=999);


This post has been edited by KYA: 22 November 2009 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#12 josephtang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-November 09

Re: prompt user for a number

Posted 22 November 2009 - 07:53 PM

can u do it in for loop i haven't learn while
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: prompt user for a number

Posted 22 November 2009 - 08:02 PM

View Postjosephtang, on 22 Nov, 2009 - 06:53 PM, said:

can u do it in for loop i haven't learn while

int a;
for(;;) {
	a = keyboard.nextInt(); 
	if(a == 999)
	  break;
}


Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,028
  • Joined: 15-July 08

Re: prompt user for a number

Posted 22 November 2009 - 08:02 PM

josephtang...this is your third post now about the same topic, why don't you go look at your old posts for some solutions...thanks
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10549
  • View blog
  • Posts: 39,044
  • Joined: 27-December 08

Re: prompt user for a number

Posted 22 November 2009 - 08:08 PM

View PostDogstopper, on 22 Nov, 2009 - 11:02 PM, said:

josephtang...this is your third post now about the same topic, why don't you go look at your old posts for some solutions...thanks


@OP: Is there something specific about the concept of getting input using Scanners in a loop that you don't understand? We want to help you understand this concept so that you won't need to open more and more threads for each new problem that applies this concept.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2