# While loop program

• 11-03-2008, 08:03 PM
DevMcg123
While loop program
Plz help. I am a rookie in Java programming and have a question on how to go by doing a while loop program.

Write a program which reads in the base and altitude of a triangle, computes the area, and prints the base, altitude and area of each in good form. Continue until the user enters a "-1" for the base.

I know the output is suppose to look like this:
Tri base altitude area
1 ## ## ##
2 ## ## ##

Thanks.
• 11-03-2008, 08:37 PM
Norm
Break the project up into small easy steps:
First don't worry about getting the input, just hard code it in variables:
int base = 10;
int altitude = 20;
etc
Then compute the area and print it out

Next write a program that gets those values from the user, one at a time.
• 11-10-2008, 12:29 PM
Hack
Welcome to DevX :WAVE:

We are more than happy to help you help yourself. What code do you have so far that doesn't seem to be working for you?
• 11-10-2008, 04:37 PM
DevMcg123
Here is what ive done so far
Code:

``` import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class triangles {         public static void main(String[] args )throws IOException {                                 InputStreamReader reader = new InputStreamReader (System.in);                 BufferedReader buffer = new BufferedReader(reader);                                 int area;                 int base;                 int altitude;                 int triangle;                                 System.out.print("Enter base of triangle:\n");                 base = Integer.valueOf(buffer.readLine()).intValue();                                 System.out.print("Enter number triangle:\n");                 triangle = Integer.valueOf(buffer.readLine()).intValue();                                                 System.out.print("Enter altitude of triangle:\n");                 altitude = Integer.valueOf(buffer.readLine()).intValue();                                                 area = 1/2 * base * altitude;                                 while(base <= 1){                         System.out.println("trangle      base    altitude  area ");                         System.out.println("-------      ----    --------  ----");                         System.out.println(+ triangle + "" + base + "" + altitude + "" + area);                                                 base = base + 1;                                                                         }                 }                        }```
• 11-11-2008, 07:24 AM
Hack
What kind of output are you getting?

Are you receiving any errors?
• 11-13-2008, 09:12 PM
Clarox
Quote:

Originally Posted by DevMcg123
Here is what ive done so far
Code:

```System.out.print("Enter base of triangle:\n"); base = Integer.valueOf(buffer.readLine()).intValue(); System.out.print("Enter number triangle:\n"); triangle = Integer.valueOf(buffer.readLine()).intValue(); System.out.print("Enter altitude of triangle:\n"); altitude = Integer.valueOf(buffer.readLine()).intValue();```

You probably want to put that inside the while loop. Also, you can use a counter for the triangle # instead of asking the user to enter it.

Quote:

Code:

`area = 1/2 * base * altitude;`

Your area, base, and altitude variables are integers. Are you sure you don't want floats or longs? Also, I'd rewrite that as
Code:

`area = base * altitude / 2`
• 11-14-2008, 07:27 AM
Hack
Quote:

Originally Posted by Clarox
you can use a counter for the triangle # instead of asking the user to enter it.

I'm guessing that rather than 0 he would want a user input starting point.
• 11-18-2008, 07:35 AM
harshira
its better to use do - while that while.
then your code will look like :
Code:

``` do{ if(base == -1 || altitude == -1 ||  height == -1) break; // find area }while(true)```