2 Replies - 225 Views - Last Post: 02 October 2012 - 04:34 PM Rate Topic: -----

#1 optimum2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-October 12

Showing Arrays

Posted 02 October 2012 - 01:45 AM

Is there a way to show Specific arrays with the value of 1 or 0?

import javax.swing.JOptionPane;
import java.io.*;
public class Javax
{
public static void main(String[]args)throws IOException
{
String name = "";
String pt = "";
int[]Room = {1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,1,0,1,0,1,1,1,1,1,0};
int rph = 500;
int hs = 0;


BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String main = JOptionPane.showInputDialog("Please Enter Your Name: ");
	name = String.valueOf(main);
JOptionPane.showMessageDialog(null,"Welcome to ______ "+name);

String main2 = JOptionPane.showInputDialog("How long do u wish to stay at our HOTEL" +name);
	hs = Integer.parseInt(main2);
JOptionPane.showMessageDialog(null,"You Entered "+hs);
JOptionPane.showMessageDialog(null,"Here a the Currently Available Rooms")
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Showing Arrays

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Showing Arrays

Posted 02 October 2012 - 02:08 AM

Do you mean to ask how to show which elements of the array have the value 1 or 0? Use a loop and find the elements that have the desired value. If you just have to report a number, as in "The number of available rooms are:", then count the number of array elements with the desired value as you go through the loop.

A more OO design would be to have a class called HotelRoom that included a roomNumber and vacant properties, methods to report/change vacancy and other relevant characteristics, and then a Hotel class that returned the number of vacant rooms with room numbers.
Was This Post Helpful? 2
  • +
  • -

#3 UNIcoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 30-September 12

Re: Showing Arrays

Posted 02 October 2012 - 04:34 PM

You got the length of your array so u just say:
int k;
int []room = {.......};

for (int i = 0; i < room.length; i++) {
      k = room[i]  
      system.out.println("Location in Array: " +i+ ". Value at Location: " +k \n);
}



There you go. Have fun.

 int k = 0; 
of course.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1