File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes do {} while statement illegal start of type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do {} while statement illegal start of type" Watch "do {} while statement illegal start of type" New topic
Author

do {} while statement illegal start of type

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I just inserted a do while loop, so after the code runs the user can choose to run the program again to choose another seat or reset the seating chart. I'm getting an illegal start of type error.import

java.io.*;
import java.util.*;

public class airplaneSeating2
{


static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
static PrintWriter screen = new PrintWriter(System.out, true);

do
{

static char [][] seatNumber =
{
{'*','*','X','*','X','X'},
{'*','X','*','X','*','X'},
{'*','*','X','X','*','X'},
{'X','*','X','*','X','X'},
{'*','X','*','X','*','*'},
{'*','x','*','*','*','X'},
{'X','*','*','*','X','X'},
{'*','X','*','X','X','*'},
{'X','*','X','X','*','X'},
{'*','X','*','X','X','X'},
{'*','*','X','*','X','*'},
{'*','*','X','X','X','X'},
{'*','*','*','*','X','*'},
};

//main method
public static void main(String args[]) throws IOException
{

String inputStr;
char smokingType = ' ';
char classType;
int rowStart = 0;
int rowEnd = 0;

System.out.println(" #############################");
System.out.println(" # #");
System.out.println(" # Airplane Seating Program! #");
System.out.println(" # #");
System.out.println(" #############################");
System.out.println();

do
{

System.out.print("Enter Class Preference E for Econonmy, F for First Class: ");
inputStr = keyboard.readLine();
inputStr = inputStr.toUpperCase();
classType = inputStr.charAt(0);
System.out.println();



switch (classType)
{
case 'E':

do
{
System.out.print("Enter Smoking Preference S for Smoking, N for Non-Smoking: ");
inputStr = keyboard.readLine();
inputStr = inputStr.toUpperCase();
smokingType = inputStr.charAt(0);
System.out.println();

switch (smokingType)
{
case 'S':
rowStart = 8;
rowEnd = 13;
break;
case 'N':
rowStart = 3;
rowEnd = 7;
break;
default:
System.out.println("Invalid Input, Enter N - non smoking or S - smoking!");
System.out.println();
break;
}

}

while (smokingType != 'N' && smokingType != 'S');

break;

case 'F':
rowStart = 1;
rowEnd = 2;
break;
default:

System.out.println("Invalid Input, enter E - Economy or F - First Class!");
System.out.println();
break;
}

}

while (classType != 'F' && classType != 'E');



printMatrix(seatNumber, rowStart, rowEnd);

chooseSeat(seatNumber, rowStart, rowEnd);

printMatrix(seatNumber, 1, 13);

}

public static void printMatrix(char[][] matrix, int row1, int row2)
{

int col;

System.out.println();
System.out.println("Available Seats");
System.out.printf("%11s %4s %4s %4s %4s %4s", "A", "B", "C", "D", "E", "F");
System.out.println();
for (row1 = row1 - 1; row1 < row2; row1++)
{

System.out.printf("Row %2d", (row1 + 1));
for (col = 0; col < matrix[row1].length; col++)
{
System.out.printf("%5c", matrix[row1][col]);
}
System.out.println();
}
System.out.println();
System.out.println(" * indicates the seat is available");
System.out.println(" X indicates the seat is not available");
System.out.println();
}

public static void chooseSeat(char[][] matrix, int row1, int row2) throws IOException
{
String inputStr;
String seat;
int row;
char col;
String colStr;
int colNo = 10;



do
{

System.out.println("To select a seat enter the row number: ");
inputStr = keyboard.readLine();
row = new Integer(inputStr);
System.out.println("Enter the column seat letter: ");
inputStr = keyboard.readLine();

colStr = inputStr.toUpperCase();
col = colStr.charAt(0);

System.out.println("Column: " + col);

switch(col)
{
case 'A': colNo = 0;
break;
case 'B': colNo = 1;
break;
case 'C': colNo = 2;
break;
case 'D': colNo = 3;
break;
case 'E': colNo = 4;
break;
case 'F': colNo = 5;
break;
default:
System.out.println("Invalid Input, Enter column A, B, C, D, E, F!");
System.out.println();

break;
}


}

while (col != 'A' && col != 'B' && col != 'C' && col != 'D' && col != 'E' && col != 'F');



System.out.println("Row: " + row);


seatNumber[row -1][colNo] = 'X';

seat = row + colStr;
System.out.println("You chose: " + seat);
System.out.println("Thank you");
System.out.println();


System.out.print("Enter B to reset or O to choose another seat: ");
inputStr = keyboard.readLine();
inputStr = inputStr.toUpperCase();
resetOrCA = inputStr.charAt(0);
System.out.println();

}

while (resetOrCA != 'B' && resetOrCA != 'E');


}
}
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

Welcome to JavaRanch! The first thing you need to do is investigate the use of code tags. That will make your posted code much easier for us to read.

I see a couple of problems. Your first line is just "java.io.*" with no import. That's probably where your error message comes from. Then you also have a do (with no while) outside of the main method, and not actually in any method at all, around the the initialization of your seatNumber array.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
Sorry the import statement must have gotten cut out somehow. I added some comments and I think I did some java docs not quite sure if they are
Correct or not. Now I get a message saying

//airplaneSeating2.java:38: illegal start of type
// do
// ^
//airplaneSeating2.java:287: <identifier> expected
// while (resetOrCA != 'B');
// ^
//2 errors

/*
* Author: Dustin Schreader
*
* Date: 05082010
*
* Course: comp 2243 spring
*
* Assignment: Ch. 9 exercise #12 p606
*
* Description: Prompts user for seating that they prefer
* The user may choose from economy or first class
* If economy is chosen the user may choose smoking
* Or non smoking then may choose a seat from an array
* The array will be used by picking row numbers and
* Column letters, the user may choose to reset the
* Program or continue to choose a seat.
*
*/

import java.io.*;
import java.util.*;

/**
*
* Have the user decide which seat they would like to use
* Can choose a seat between smoking and non and economy
* Or first classe
*
* @author Dustin Schreader
* @version 05082010
*/

public class airplaneSeating2
{

static char resetOrCA = ' ';

do
{

//^ This is the start of the do while loop
// in order for the user to choose to reset
// the program or choose another seat

static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
static PrintWriter screen = new PrintWriter(System.out, true);


static char [][] seatNumber =
{
{'*','*','X','*','X','X'},
{'*','X','*','X','*','X'},
{'*','*','X','X','*','X'},
{'X','*','X','*','X','X'},
{'*','X','*','X','*','*'},
{'*','x','*','*','*','X'},
{'X','*','*','*','X','X'},
{'*','X','*','X','X','*'},
{'X','*','X','X','*','X'},
{'*','X','*','X','X','X'},
{'*','*','X','*','X','*'},
{'*','*','X','X','X','X'},
{'*','*','*','*','X','*'},
};



/**
* prompts the user whether they want economy or first class
* then it prompts the user for smoking or non if economy
*
* @param args not used
*/

public static void main(String args[]) throws IOException
{



String inputStr;
char smokingType = ' ';
char classType;
int rowStart = 0;
int rowEnd = 0;



System.out.println(" #############################");
System.out.println(" # #");
System.out.println(" # Airplane Seating Program! #");
System.out.println(" # #");
System.out.println(" #############################");
System.out.println();

do
{

System.out.print("Enter Class Preference E for Econonmy, F for First Class: ");
inputStr = keyboard.readLine();
inputStr = inputStr.toUpperCase();
classType = inputStr.charAt(0);
System.out.println();



switch (classType)
{
case 'E':

do
{
System.out.print("Enter Smoking Preference S for Smoking, N for Non-Smoking: ");
inputStr = keyboard.readLine();
inputStr = inputStr.toUpperCase();
smokingType = inputStr.charAt(0);
System.out.println();

switch (smokingType)
{
case 'S':
rowStart = 8;
rowEnd = 13;
break;
case 'N':
rowStart = 3;
rowEnd = 7;
break;
default:
System.out.println("Invalid Input, Enter N - non smoking or S - smoking!");
System.out.println();
break;
}

}

while (smokingType != 'N' && smokingType != 'S');

break;

case 'F':
rowStart = 1;
rowEnd = 2;
break;
default:

System.out.println("Invalid Input, enter E - Economy or F - First Class!");
System.out.println();
break;
}

}

while (classType != 'F' && classType != 'E');



printMatrix(seatNumber, rowStart, rowEnd);

chooseSeat(seatNumber, rowStart, rowEnd);

printMatrix(seatNumber, 1, 13);




}

/**
* This method prints out the matrix for the user
* to view and see what seats are available to choose
* from
*
* @param args not used
*/

public static void printMatrix(char[][] matrix, int row1, int row2)
{

int col;

System.out.println();
System.out.println("Available Seats");
System.out.printf("%11s %4s %4s %4s %4s %4s", "A", "B", "C", "D", "E", "F");
System.out.println();
for (row1 = row1 - 1; row1 < row2; row1++)
{

System.out.printf("Row %2d", (row1 + 1));
for (col = 0; col < matrix[row1].length; col++)
{
System.out.printf("%5c", matrix[row1][col]);
}
System.out.println();
}
System.out.println();
System.out.println(" * indicates the seat is available");
System.out.println(" X indicates the seat is not available");
System.out.println();
}

/**
* @throws IOException if an input or output
* exception occured
*/

public static void chooseSeat(char[][] matrix, int row1, int row2) throws IOException
{
String inputStr;
String seat;
int row;
char col;
String colStr;
int colNo = 10;


do
{

System.out.println("To select a seat enter the row number: ");
inputStr = keyboard.readLine();
row = new Integer(inputStr);
System.out.println("Enter the column seat letter: ");
inputStr = keyboard.readLine();

colStr = inputStr.toUpperCase();
col = colStr.charAt(0);

System.out.println("Column: " + col);

switch(col)
{
case 'A': colNo = 0;
break;
case 'B': colNo = 1;
break;
case 'C': colNo = 2;
break;
case 'D': colNo = 3;
break;
case 'E': colNo = 4;
break;
case 'F': colNo = 5;
break;
default:
System.out.println("Invalid Input, Enter column A, B, C, D, E, F!");
System.out.println();

break;
}


}

while (col != 'A' && col != 'B' && col != 'C' && col != 'D' && col != 'E' && col != 'F');

// ^ lets the user choose seat row number and column letter until
// until user chooses correct letter, although if there is
// a number that is incorrect there is not a message sent out
// to user and the program exits with error

System.out.println("Row: " + row);


seatNumber[row -1][colNo] = 'X';

seat = row + colStr;
System.out.println("You chose: " + seat);
System.out.println("Thank you");
System.out.println();



}

System.out.print("Enter B to reset or O to choose another seat: ");
inputStr = keyboard.readLine();
inputStr = inputStr.toUpperCase();
resetOrCA = inputStr.charAt(0);
System.out.println();

// ^ This is used to prompt the user if they would like
// to choose another seat or reset and info s put into
// resetOrCA

}
while (resetOrCA != 'B');
//^ here is the while statement to decied whether
// to reset the seating assignment or choose another seat
// not sure how to reset the array matrix yet

}





Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Still no code tags in your code.
Please UseCodeTags next time, it's really difficult to read lines of code in that way.

Anyway, you begin a do-while loop outside the main method.

Here's the structure you could use:








Bye,
Nicola
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Sorry I'll do some searching for using code tags in my program, I did put the do while statement inside of a method and now it works... almost, I'm trying make a switch statement to make the user enter B to reset seating chart or o to continue and select another seat. Thank you both for the help!
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
For reading clarity.


-Nev
"It's about choosing your battles, not fighting somebody else's war"
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
oh ok wow that does look a lot better I'll use that from now on thank you!
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
You don't need a search. Just click on the link i left in my reply to investigate the meaning of code tags.

Well what is exactly the problem? I saw you made correct use of switch construct in your code.
Do you want to know how to get the user input?

In that case you could use java.io.Console class.
It has methods to read lines from console or even to read lines formatted in the way you need

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38856
    
  23
Nobody yet told you you aren't allowed to use static (or public, protected, private, etc) more than one { from the left of the page? Put some indentation in and you will see several statics inside {} inside your class where the compiler will complain about it.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I hade it sorta working the way I wanted to and then I tried moving the do while to main and it didn't work so to demonstrate how I wanted it to print out I put everything back the way it was but now i'm getting illegal character \160, I think the lack of sleep is getting to me lol


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do {} while statement illegal start of type