Hi , my friend pass this coding to me , to change it on few things , i try to change it first , to make a while looping ... loop for once but i get error , then change the input from number+letter to letter+number , still got no changes , here example of the code , can you help me ? .

import java.util.Scanner;

public class Seating {

        /**
        *       Program:        Seating.java
        *       Purpose:        unknown
        *       Creator:        Naga Hantu 
        *       Created:        22.09.2013
        */

        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);
                int rows, seatsPerRow, r, c;
                String choice;

                System.out.print("How many rows? ");
                rows = scan.nextInt();
                System.out.print("How many seats per row? ");
                seatsPerRow = scan.nextInt();
                boolean[][] seats = new boolean[rows][seatsPerRow];
                displaySeating(seats);

                while (true) {
                        System.out.print("Choose a seat (eg \"1C\") or Q to quit: ");
                        choice = scan.next();
                        if (choice.equals("Q") || choice.equals("q")) break;
                        r = Integer.parseInt(choice.substring(0, 1)) - 1;
                        c = (int) (choice.charAt(1) - 'A');
                        seats[r][c] = true;
                        displaySeating(seats);
                } // end while
        } // end main()

        public static void displaySeating(boolean[][] seats) {
                System.out.print("\t");
                for (int i=0; i<seats[0].length; i++) {
                        System.out.print((char)('A' + i)+"\t");
                } // end for
                System.out.println();
                for (int r=0; r<seats.length; r++) {
                        System.out.print((r+1)+"\t");
                        for (int s=0; s<seats[0].length; s++) {
                                if (seats[r][s]) {
                                        System.out.print("X\t");
                                } else {
                                        System.out.print("O\t");
                                } // end if
                        } // end for s
                        System.out.println();
                } // end for r
        } // end displaySeating()
} // end class Seating

Recommended Answers

All 2 Replies

For input letter+number:

r = Integer.parseInt(choice.substring(1, 2)) - 1;
c = (int) (choice.charAt(0) - 'A');

and for the part "loop for once" i didn't understand but as far as i know, there's no such a thing, you want to loop once; just write the code without a loop.
hope this helps.

thanks bro , now its work , my problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.