Wating for input.

I want my program to wait for my input, then go on.

Page 1 of 1

5 Replies - 334 Views - Last Post: 27 April 2009 - 06:28 PM Rate Topic: -----

#1 And3rs87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-April 09

Wating for input.

Posted 27 April 2009 - 12:59 PM

Hello!

I have a little problem, I dont know if it's my compiler or if it's something else that is wrong.
I want my program, to show one question, and then after I have answered it, the program will display my other question.

import java.io.*; 

class BallsPerKid { 
	public static void main(String args[]) 
	{ 
		InputStreamReader reader = new InputStreamReader(System.in); 
		BufferedReader buf = new BufferedReader(reader); 
		double balls; 
		double Kids; 
		double ballsPerKid; 
		 
		try{ 
			System.out.print("How many kids?"); 
			Kids = Integer.parseInt(buf.readLine()); 
		 
			System.out.print("How many balls?"); 
			balls = Integer.parseInt(buf.readLine()); 
		 
			ballsPerKid = balls / Kids; 
		 
			System.out.println("Each kid gets:"); 
			System.out.print(ballsPerKid); 
			System.out.print(" balls."); 
		}catch(Exception e){ 
			e.printStackTrace(); 
		}		 
	} 
} 



I have asked some of my friends about this as well, and some say that it works with one question at a time and some get the problem I have.
Im running Windows xp and i'm using the compiler j2se,JCreator 4.5.

Is This A Good Question/Topic? 0
  • +

Replies To: Wating for input.

#2 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 661
  • Joined: 20-March 09

Re: Wating for input.

Posted 27 April 2009 - 01:03 PM

I would suggest switching from BufferedReader to Scanner.

Scanner scan = new Scanner(System.in);
System.out.print("How many kids: ");
int kids = scan.nextInt();
System.out.print("How many balls: ");
int balls = scan.nextInt();


Was This Post Helpful? 1
  • +
  • -

#3 And3rs87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-April 09

Re: Wating for input.

Posted 27 April 2009 - 01:05 PM

View Post333OnlyHalfEvil, on 27 Apr, 2009 - 12:03 PM, said:

I would suggest switching from BufferedReader to Scanner.

Scanner scan = new Scanner(System.in);
System.out.print("How many kids: ");
int kids = scan.nextInt();
System.out.print("How many balls: ");
int balls = scan.nextInt();





and the scan.nextInt(); does not work, how do I get scan to work?

EDIT: fixed the scan with Scanner scan = new Scanner(System.in);
But, my compiler still show both questions at the same time.

"How many balls? How many kids? 20 5
Each kid gets:
4 balls.
Process completed."

how many balls and how many kids is coming up at the same time, I have to answer them both, at the same time..

This post has been edited by And3rs87: 27 April 2009 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 And3rs87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-April 09

Re: Wating for input.

Posted 27 April 2009 - 01:54 PM

fixed it with using eclipse instead of my old compiler.

thx for the answer 333OnlyHalfEvil =)
Was This Post Helpful? 0
  • +
  • -

#5 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 661
  • Joined: 20-March 09

Re: Wating for input.

Posted 27 April 2009 - 01:57 PM

No problem
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is online

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

Reputation: 8032
  • View blog
  • Posts: 31,185
  • Joined: 06-March 08

Re: Wating for input.

Posted 27 April 2009 - 06:28 PM

If Kids and balls are double why do you parse them as int ?
Actually I cannot imagine 3.14159 kids or balls so why don't you make these int ?
Then your Integer.parseInt() would be in the good contyext
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1