8 Replies - 4457 Views - Last Post: 05 May 2011 - 04:52 AM Rate Topic: -----

#1 Javanewbie83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-May 11

Help with simple Java division program

Posted 03 May 2011 - 05:12 PM

Hi All,

This is my first post on Dream.In.Code so please let me know if there is anything I should know regarding my questions/posts.

The question I have is regarding a very simple Java division program. The program is meant to take as input two numbers, check that the second number is not zero, calculate the quotient of dividing the second number by the first number, then output the result. I would also like the program to allow the user to exit at anytime by typing the word "STOP". I can get the input, do the calculations and print the result. The problem I have is with the second part of allowing the end user to exit AT ANY TIME by typing the word "STOP". I have tried putting the code in a while loop that would check for that but the problem I have is with capturing the input from the user. The code compiles without errors but it does not execute as it should. Please help!

import java.util.Scanner;
public class Divide
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		
		System.out.println("This program calculates the quotient between two numbers input. ");
		System.out.println("To stop the program please print STOP at any time. ");

			double first;
			double second;
			double quotient;
			String stop;
			boolean keepGoing = true;
			
			while(keepGoing)
			{
				System.out.println("\n" + "Enter two integers, the second must be non-zero: " + "\n");
				first = in.nextDouble();
				second = in.nextDouble();
				stop = in.next().trim();
				quotient = (double)((int)(first/second*100))/100;	
		
				if (second !=0)
				{
					System.out.println("\n" + first + " divided by the " + second + " is " + quotient);
					System.out.println(stop);
				}
				else if (second == 0)
					System.out.println("\n" + "Second number must be non-zero.");
				else if (stop.equalsIgnoreCase("stop"))
				{
					keepGoing = false;
					System.out.println("\n" + "Thank you for using this program.");	
				}
			}			
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with simple Java division program

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7643
  • View blog
  • Posts: 12,889
  • Joined: 19-March 11

Re: Help with simple Java division program

Posted 03 May 2011 - 08:19 PM

To stop "at any time" by entering "STOP" you would have to check for "STOP" each time you accept input (there isn't going to be any other time, from the user's point of view). That means, when you get "first", instead of accepting a double, you would accept a String and check it - if it's "STOP", you stop, if it's a number you parse it as a double and proceed to accept "second".

Since you're doing the same thing twice, there should be a voice in your ear saying "that's a method". That voice is correct: write a method which accepts input, checks it to see if it's the word "STOP", etc.
Hope that helps!
Was This Post Helpful? 1
  • +
  • -

#3 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Help with simple Java division program

Posted 03 May 2011 - 09:39 PM

I suggest you use a BufferedReader and receive input in the form of a String and then parse the input to appropriately store the correct data type, then the first thing you check in your while loop is if the String is equal to "Stop", here is an example:
 BufferedReader inKb = new BufferedReader (new InputStreamReader (System.in));  
String input  = ""; 
System.out.println ("Enter your first value");
 input  = inKb.readLine();
if (first.equals("STOP") {
System.exit(0);
}
else {
first = Double.parseDouble(input);
}
//etc...


You then check for each input if the input is equal to "STOP" otherwise you cast the variable appropriately.
Was This Post Helpful? 1
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Help with simple Java division program

Posted 03 May 2011 - 11:14 PM

You should instead use nextLine() and then split the String into tokens. Then you check if the first token equals "stop"
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7643
  • View blog
  • Posts: 12,889
  • Joined: 19-March 11

Re: Help with simple Java division program

Posted 03 May 2011 - 11:20 PM

So we're all agreed, then? :)
Was This Post Helpful? 0
  • +
  • -

#6 Javanewbie83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-May 11

Re: Help with simple Java division program

Posted 04 May 2011 - 07:19 PM

Thank you to everyone for the excellent suggestions! It seems there are several valid solutions to this programming problem. The solution that worked best for me was the solution posted by vOrtex, so a special thank you to you!

Below is my completed code, which compiles and executes perfectly now.

Thank you again to all for the quick responses :^: ! :)

import java.util.Scanner;
import java.io.*;
public class Divide
{
	public static void main(String[] args) throws IOException
	{
		Scanner in = new Scanner(System.in);
		
		BufferedReader inKb = new BufferedReader (new InputStreamReader (System.in)); 
		
		System.out.println("This program calculates the quotient between two numbers input. ");
		System.out.println("To stop the program please print STOP at any time. ");

			String first = " ";
			String second = " ";
			double divisor = 0;
			double dividend = 0;
			double quotient = 0;
			boolean keepGoing = true;
			
			while (keepGoing)
			{
				System.out.println("\n" + "Enter the first integer: " + "\n");
				first = inKb.readLine();
				if (first.equalsIgnoreCase("STOP"))
				{
					System.out.println("\n" + "Thank you for using this program.");
					keepGoing = false;
					System.exit(0);
				}
				else 
					dividend = Double.parseDouble(first);
				
				System.out.println("\n" + "Enter the second integer: " + "\n");
				second = inKb.readLine();
				
				if (second.equalsIgnoreCase("STOP"))
				{	
					System.out.println("\n" + "Thank you for using this program.");
					keepGoing = false;
					System.exit(0);
				}
				else
					divisor = Double.parseDouble(second);
					
				quotient = (double)((int)(dividend/divisor*100))/100;	
				System.out.println("\n" + "The quotient of dividing " + dividend + " by " + divisor + " is: " + quotient);
			}
	}
}



Was This Post Helpful? 0
  • +
  • -

#7 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Help with simple Java division program

Posted 04 May 2011 - 09:51 PM

You do not need the scanner package nor this
   Scanner in = new Scanner(System.in);

If you are using BufferedReader, cool you got it working.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

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

Re: Help with simple Java division program

Posted 05 May 2011 - 04:45 AM

and no need to initialize first and second with " "
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7643
  • View blog
  • Posts: 12,889
  • Joined: 19-March 11

Re: Help with simple Java division program

Posted 05 May 2011 - 04:52 AM

Also, no need for "keepGoing" since you do a System.exit immediately on getting "STOP"

Also, repeated code should be a method. Never write the same code twice.

Also, there's not a lot of difference between BufferedReader and Scanner for your purposes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1