Why is my program throwing an exception? - java

Whats happening is when I try to add a new class to my student, I have to check to make sure that the class time I am trying to add doesn't conflict with my students other class times, but for some reason when the code gets into the loop to check the other course times stored in an array list, there is an exeption called when their shouldn't be. For example, I could put in 5:00p-10:00p for one course, then 1:00p-2:00p for the second course and it will throw the exception like there is a conflict there when there isn't. please check the comment to see where the problem occurs. any ideas?
package myschool;
import java.util.ArrayList;
import java.util.Scanner;
public class MySchool {
private static Exception e;
public static void main(String[] args) {
ArrayList<Student> listStudent = new ArrayList<>();
ArrayList<Integer> listCourseStart = new ArrayList<>();
ArrayList<Integer> listCourseEnd = new ArrayList<>();
boolean continueLoop = true;
boolean addFirstCourse = true;
boolean addACourse = false;
Scanner userInput = new Scanner(System.in);
int option;
do{
try {
System.out.println(" What would you like to do?");
System.out.println(" 1) Add a student");
System.out.println(" 2) View students");
System.out.println(" 3) Remove a student");
System.out.println(" 4) Exit");
System.out.print("--> ");
option = userInput.nextInt();
switch( option ){
case 1:
Scanner inputs = new Scanner(System.in);
String fName, lName;
int sID;
double sGPA;
System.out.print(" First Name:");
fName = inputs.nextLine();
System.out.print(" Last Name:");
lName = inputs.nextLine();
System.out.print(" ID Number:");
sID = inputs.nextInt();
System.out.print(" GPA:");
sGPA = inputs.nextDouble();
Student newStudent = new Student(fName, lName, sID, sGPA);
listStudent.add(newStudent);
inputs.nextLine();
while (true) {
try {
System.out.println("Would you like to add a course? Y/N");
String shouldAddCourse = inputs.nextLine();
if( "N".equals(shouldAddCourse.toUpperCase()))
break;
System.out.print(" CourseName:");
String cName = inputs.nextLine();
System.out.print(" Instructor:");
String instructor = inputs.nextLine();
System.out.print(" CourseID:");
int cID = inputs.nextInt();
System.out.print(" CourseCredit:");
int cCred = inputs.nextInt();
inputs.nextLine();
System.out.print(" StartTime:");
String cStart = inputs.nextLine();
System.out.print(" AM or PM ?");
String startAMorPM = inputs.nextLine();
System.out.print(" EndTime:");
String cEnd = inputs.nextLine();
System.out.print(" AM or PM ?");
String endAMorPM = inputs.nextLine();
String cStartRemove = cStart.replace(":","");
int startInt = Integer.parseInt( cStartRemove );
String cEndRemove = cEnd.replace(":","");
int endInt = Integer.parseInt( cEndRemove );
if( "PM".equals(startAMorPM) || "pm".equals(startAMorPM) || "P".equals(startAMorPM) || "p".equals(startAMorPM) )
startInt = startInt + 1200;
if( "PM".equals(endAMorPM) || "pm".equals(endAMorPM) || "P".equals(endAMorPM) || "p".equals(endAMorPM) )
endInt = endInt + 1200;
if( addFirstCourse ){
Course newCourse = new Course( cName, instructor, cCred, cStart, cEnd, cID );
newStudent.listCourse.add(newCourse);
listCourseStart.add( startInt );
listCourseEnd.add( endInt );
addFirstCourse = false;
}else{
for( Integer r: listCourseStart ) {
if( startInt >= listCourseStart.get(r) && startInt <= listCourseEnd.get(r) /*|| endInt >= listCourseStart.get(r) && endInt <= listCourseEnd.get(r)*/ ) //the problems happens hear on the first listCourseStart.get(r)
throw e;
else
addACourse = true;
}
if( addACourse == true ){
listCourseStart.add( startInt );
listCourseEnd.add( endInt );
Course newCourse = new Course( cName, instructor, cCred, cStart, cEnd, cID );
newStudent.listCourse.add(newCourse);
addACourse = false;
}
}
} catch (Exception e) {
System.out.println("You have already added a class at that time!");
}
}
break;
case 2:
if(!listStudent.isEmpty()){
for(Student l:listStudent) {
System.out.println(l);
for(Course n:l.listCourse) {
System.out.println(n);
}
System.out.println();
}
}else
System.out.println("There are no students to view\n");
break;
case 3:
Scanner removeChoice = new Scanner(System.in);
try {
if(!listStudent.isEmpty()){
int j = 0;
System.out.println("Which student do you want to remove?");
for(Student l:listStudent) {
System.out.print(j+1 + ")");
System.out.println(l);
j++;
}
int remove = removeChoice.nextInt();
listStudent.remove( remove - 1 );
System.out.println("Student has been removed\n");
}else
System.out.println("There are no students to remove\n");
} catch (Exception e) {
System.out.println("There are no students to remove\n");
}
break;
case 4:
continueLoop = false;
break;
}
} catch (Exception e) {
System.out.println("That is not a valid option!!!");
continueLoop = false;
}
}while( continueLoop );
}
}

You're throwing an exception in your for loop.
for( Integer r: listCourseStart ) {
if( startInt >= listCourseStart.get(r) && startInt <= listCourseEnd.get(r) /*|| endInt >= listCourseStart.get(r) && endInt <= listCourseEnd.get(r)*/ ) //the problems happens hear on the first listCourseStart.get(r)
throw e;// <-- This guy is the culprit, but I'm guessing you already knew that...
else
addACourse = true;
}
It's fairly difficult to say exactly where it's going wrong (it's a bit hard to read your code, but it should be behaving as you've stated).
You may want to use some breakpoints, if you're using an IDE, and check the values of your input variables before they get put into the list, or have it spit them back out at you on the command line, before putting them in the list.

You are misusing the list listCourseStart in the for loop.
for( Integer r: listCourseStart ) {
if( startInt >= listCourseStart.get(r) && startInt <= listCourseEnd.get(r) )
throw e;
else
addACourse = true;
}
listCourseStart is a list of intergers and with
for( Integer r: listCourseStart ) {
// ...
}
you iterate over the list elements. So in the first iteration r will be the first list element, in the second iteration the second list element and so on.
But inside your loop you call listCourseStart.get(r). The list's get() method retrieves the list element at the given position. So if the first list element is 5 then with listCourseStart.get(5) you get the fifth list element. I'm sure, this is not really what you want.
Why didn't you use a debugger? You can run your program step by step, it shows you the actual variable values so you can see what's going on in detail.

I think the problem is your loop
for( Integer r: listCourseStart ) { // r ist the value in listCourseStart
// you use the value 'r' as index
// you have to use for( int i = 0; i.....) or 'r' is the the right value
if( startInt >= listCourseStart.get(r) && startInt <= listCourseEnd.get(r) )
throw e;
else
addACourse = true;
}
Try this
for( Integer r: listCourseStart ) {
if( startInt >= r && startInt <= r )
throw e;
else
addACourse = true;
}
EDIT:
Yout are right in your comment. Your condition is not necessary. You need to store the start and end time together. Here is an exsample:
List<Integer[]> times = new ArrayList<>();
times.add(new Integer[]{900,1100});
times.add(new Integer[]{1300,1400});
for( Integer[] time : times ){
if( startInt >= time[0] && startInt <= time[1]
|| endInt >= time[0] && endInt <= time[1] ){
throw e;
}
}
A little Hint: Your Exception e was never initiate - you will get a NullPointerException

Related

Simple Seat Reservation in Java

package Demo;
import java.util.Scanner;
public class seat_reservation{
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
// Initialization
final int ROWS = 2;
final int COLS = 3;
char [][] seats = new char [ROWS][COLS];
int i, j, seatNum, counter = 0;
char seatLetter = 'A';
int choice = 0;
String seatEnter;
boolean cont = true; // loops of running the program
while( choice != 4 ){
System.out.print( "1. Assign Seats" );
System.out.print( "2. Exit" );
System.out.print( "Select your choice: " );
choice = read.nextInt();
switch( choice ){
case 1:
//Set the value.
for (i=0; i < seats.length; i++) {
for (j=0; j < seats[i].length; j++)
seats[i][j] = seatLetter++;
seatLetter = 'A'; // to reset the value to A for the new loop
}
//To display the list of seats
for (i=0; i < seats.length; i++) {
System.out.print((i+1)+" ");
for (j=0; j < seats[i].length; j++)
System.out.print(seats[i][j]+" ");
System.out.println();
}
//condition
while (counter < 6 && cont) {
do {
System.out.print("Please type the chosen seat(starts with row and column,e.g:2A):" + "");
seatEnter = (read.nextLine()).toUpperCase(); //covert to Upper case
seatNum = Integer.parseInt(seatEnter.charAt(0)+"");
if (seatNum != 0)
seatLetter = seatEnter.charAt(1);
i++;
//if user enters wrong input, error message will appear.
if (seatLetter!='A'){
if (seatLetter!='B'){
if(seatLetter!='C'){
if(seatLetter!='D')
System.out.println ("Invalid! Please enter the correct seat:");
}
}
}
}
//continue to loop until the condition true
while (seatNum < 0 || seatNum > 7 || seatLetter < 'A' || seatLetter > 'D');
if (seatNum == 0) {
cont = false;
} else {
if (seats[seatNum-1][seatLetter-65] == 'X')
System.out.println("Seat have been taken.Please choose another seat:");
else {
seats[seatNum -1][seatLetter-65] = 'X';
counter++;
}
// To display updated lists of seats
for (i=0; i < seats.length; i++) {
System.out.print((i+1)+" ");
for (j=0; j < seats[i].length; j++)
System.out.print(seats[i][j] + " ");
System.out.println();
}
System.out.println(" ") ;
//}
//}
// displays fully booked message
if (counter == 6)
System.out.println("All seats are now fully-booked.");
break;
}
}
case 2://syntax error here
if (counter == 6)
System.out.println( "All seats are now fully-booked." );
System.out.println( "End of Program" );
System.exit(0);
break;
default:
System.out.println("Error input");
break;//syntax error here as well.
}
}
}
}
The problem is caused due to:
choice = read.nextInt();
The scanner.nextInt() only takes the next token from the input. Rest are ignored by it.
So when you're trying to take the next input from this line and process it, the error occurs:
seatEnter = (read.nextLine()).toUpperCase(); //covert to Upper case
seatNum = Integer.parseInt(seatEnter.charAt(0) + "");
As the previous read.nextInt() left the rest except first token, when you hit enter after giving 1 as input, it took only the 1 and the enter or newline token was captured by the read.nextLine(). That is why it got no charAt(0) and thus thrown StringIndexOutOfBoundException.
Try:
choice = Integer.parseInt(read.nextLine());
or,
choice = read.nextInt();
read.nextLine(); // this will capture the residue

Why is this infinte loop happening?

Guys I really need some help here :(
I need to create a contact list in which I need to be able to create, edit, delete, show and search contacts.
But after I enter number 1 (to include a new number) and type the name and number, it goes to an infinite loop and I was wondering if you guys could help me figure out why it's happening and how to fix it.
I'm pretty sure it has something to do with this block at the end of the code:
while (op!=6)
System.out.println();
But when I remove it, the loop just doesn't happen. Instead of an infinite loop it just doesn't loop at all. I've been trying for literally hours now and I can't seem to figure it out at all.
(also I'm not allowed to use array list here)
Sorry for my english and thank you already!
import java.util.Scanner;
public class Vetor45 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] name = new String[1000];
String nname, auxname;
int[] tel = new int[1000];
int ntel, op, cont, i, k, auxtel;
cont = 0;
k = 0;
for (i = 0; i < 1000; i++) {
name[i] = "Empty";
tel[i] = 0;
}
{
System.out.println("contact list:");
System.out.println("1. include a new number");
System.out.println("2. edit a number");
System.out.println("3. delete a number");
System.out.println("4. print all numbers");
System.out.println("5. search by name");
System.out.println("6. exit");
System.out.println("Option:");
op = scanner.nextInt();
System.out.println("");
if (op == 1) {
if (k <= 999) {
while (name[k] != "Empty") {
k++;
}
System.out.println("Enter a name:");
name[k] = scanner.next();
System.out.println("Enter a number:");
tel[k] = scanner.nextInt();
k++;
}
else {
System.out.println("complete");
}
}
else {
if (op == 2) {
i = 0;
System.out.println("enter a name:");
nname = scanner.next();
while (nname != name[i] && i < k - 1) {
i++;
}
if (nname == name[i]) {
System.out.println("enter the new number:");
ntel = scanner.nextInt();
tel[i] = ntel;
}
else {
System.out.println("name not registred");
}
}
else {
if (op == 3) {
k--;
i = 0;
System.out.println("enter a name:");
nname = scanner.next();
while (nname != name[i] && i < k) {
i++;
}
if (nname == name[i]) {
name[i] = "Empty";
tel[i] = 0;
} else {
System.out.println("name not registred");
}
}
else {
if (op == 4) {
for (i = 0; i <= k - 2; i++) {
for (cont = i + 1; cont <= k - 1; cont++) {
if (name[i] == name[cont]) {
auxname = name[i];
name[i] = name[cont];
name[cont] = auxname;
auxtel = tel[i];
tel[i] = tel[cont];
tel[cont] = auxtel;
}
}
}
System.out.println("phone list:");
for (i = 0; i < 1000; i++) {
if (name[i] != "Empty") {
System.out.println("name: " + name[i]);
System.out.println("tel: " + tel[i]);
}
}
}
else {
if (op == 5) {
i = 0;
System.out.println("enter a name:");
nname = scanner.next();
while (nname != name[i] && i < k) {
i++;
}
if (nname == name[i]) {
System.out.println("name: " + nname);
System.out.println("Tel: " + tel);
} else {
System.out.println("name not registred");
}
} else {
if (op == 6) {
System.out.println("exiting");
} else {
System.out.println("option not available");
}
}
}
}
}
}
System.out.println();
}
while (op != 6)
System.out.println();
}
}
You don't even have a do/while loop in this code, here is what a do/while loop loops like:
do{
//loop
}while(op != 6); //don't forget semi colon
You need to develop some standard for braces, and such. As it is right now, it's really hards to read this code.
Yes, as surmised by both you and #Amadan, the while (op != 6) is the problem. In general, when you're looping, something in the loop has to modify something in the condition you're looping on or something in the loop has to modify the normal control flow (so a return, break, etc.), so when you see while (op != 6), you should think "op is the only variable in the condition, so something inside the loop has to modify op or there has to be some other way to get out of the loop."
I think what you want to do is put a do before the big block before the while and then have the while at the end of the block, so you're doing this:
do
{
...
op = scanner.nextInt();
...
} while (op != 6);
That will read an int into op, do some stuff with it, and then bail out if the user entered 6.
The loop while (op!=6) is being run without any changes to op being allowed inside it.
//Rest of the code
System.out.println();
}
while (op!=6)
System.out.println();
}
}
The loop should be evealuated at the beginning of what you need to execute and have everything you want to do in the loop (including changing op) inside it:
for (i=0; i<1000; i++)
{name[i]="Empty"; tel[i]=0;}
while (op!=6)
{
System.out.println("contact list:");
//Rest of the code
You will probably have to move some braces round when you do this also. But I'm not going to go through the whole thing/ that's outside the scope of the question.

How to go back into a while-loop, from an if statement?

Here's what i've been working on. I'm trying loop this while method, using booleans. (My teacher is incompetent, so i've been learning out of textbook.)
else { System.out.println("Do you want to restart? Y/N");
string answer = scn.next();
return;
if (scn.hasNext() && !no)) {
System.out.println("end");
} else{
continue;
}
/*if (repeat) {
continue;
} else {
System.out.println("End");
break;
}*/
}
This is nested in a while loop like so ....
import java.util.Scanner; import java.lang.String;
public class booleanvariables {
public static void main (String[] args){
Scanner scn = new Scanner(System.in);
int score1, score2;
String answer, e;
boolean bothHigh, atLeastOneHigh, atLeastOneModerate, noLow, tooLow, repeat;
while (true) {
System.out.print("Enter the first test score:\t");
score1 = scn.nextInt();
System.out.print("Enter the second test score:\t");
score2 = scn.nextInt();
answer = null;
e = "n";
bothHigh = (score1 >= 90 && score2 >= 90);
atLeastOneHigh = (score1 >= 90 || score2 >= 90);
atLeastOneModerate = (score1 >= 70 || score2 >= 70);
noLow = !(score1 < 50 || score2 < 50);
tooLow = (score1 <= 50 || score2 <= 50);
repeat = (answer == "yes" || answer == "y"); //|| answer == Y || answer == Yes);
if (tooLow)
System.out.println("Inputs are too low");
if (bothHigh)
System.out.println("Qualified to be a manager");
if (atLeastOneHigh)
System.out.println("Qualified to be a supervisor");
if (atLeastOneModerate && noLow)
System.out.println("Qualified to be a clerk");
/** NESTED WRONG I'M AWARE
*/
else { System.out.println("Do you want to restart? Y/N");
string answer = scn.next();
return;
if (scn.hasNext() && !no)) {
System.out.println("end");
} else{
continue;
}
/*if (repeat) {
continue;
} else {
System.out.println("End");
break;
}*/
}
}
}
}
This is much simpler than you think.
Just do it like this:
boolean stop = false;
while(!stop) {
//do whatever you want here
System.out.println("Do you want to quit?(yes or no");
String input = scan.nextLine();
if(input.equals("no")) {
stop = true;
}
}
That way, if you enter "no", it'll set the boolean to true, which then will make the condition for the while loop, !stop, equal to false.
answer == "yes"
You are checking if two objects are the same. You should use the equals method answer.equals("yes") || answer.equals("y")
Tested and Working to My Liking
I've reworked some branching. ( I use BlueJ as a compiler and it thinks this is an error without the input = scn.nextLine();
do {
//same booleans i've been using
if (!stop) {
System.out.print("Do you want to quit? (yes or no):\t");
//String input;
input = scn.nextLine();
}
//String input;
input = scn.next();
if(input.equals("yes")) {
stop = true;
System.out.println("Goodbye");
return;
}
} while (!stop);
I really don't know why blue J doesn't like it when initialize input from within the if statement

Java scanner in if functions not working

Selecting each option on the menu now works fine (thank you for the help Tarek Salah and dasblinkenlight). The problem now being that when I select an option that requires me to enter a new word (for example option 3 the user must enter the name of a song) it skips over that and goes back to the menu. Does anyone know how to stop that from happening so that the user can actually enter something?
import java.util.Scanner;
public class JukeboxApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Jukebox jb = new Jukebox();
boolean check = false;
System.out.println("Please enter the corresponding number to perform said action.");
while ( check == false ) {
System.out.println("1: Add a song to the JukeBox\n" +
"2: Remove a song from the JukeBox\n" +
"3: Search for a specific song\n" +
"4: Display total price to play all songs\n" +
"5: Display the most expensive song\n" +
"6: Display the shortest song\n" +
"7: Display the most played song\n" +
"8: Display all songs in the JukeBox\n" +
"9: Display all songs by a specific artist\n" +
"10:\n" +
"11: Exit the JukeBox");
int num = sc.nextInt();
if ( num == 1 ) {
System.out.println("Please enter the name of the artist");
String artist = sc.next();
sc.nextLine();
System.out.println("Please enter the title of the song");
String title = sc.nextLine();
System.out.println("Please enter the price of the song");
double price = sc.nextDouble();
System.out.println("Please enter the length of the song");
double length = sc.nextDouble();
Song s1 = new Song(artist, title, price, length);
jb.addSong(s1);
} else if ( num == 2 ) {
System.out.println("Please enter the title of the song you would like to remove");
sc.nextLine();
jb.removeSong(sc.nextLine());
} else if ( num == 3 ) {
System.out.println("Enter the title of the song you are searching for");
jb.searchSong(sc.nextLine());
} else if ( num == 4 ) {
System.out.println(jb.calcTotal());
} else if ( num == 5 ) {
System.out.println(jb.showMostExpensive());
} else if ( num == 6 ) {
System.out.println(jb.showShortest());
} else if ( num == 7 ) {
System.out.println(jb.mostPlayed());
} else if ( num == 8 ) {
jb.displaySongs();
} else if ( num == 9 ) {
System.out.println("Please enter the artist you are searching for");
System.out.println(jb.searchArtist(sc.nextLine()));
} else if ( num == 10 ) {
System.out.println("");
} else if ( num == 11 ) {
System.out.println("Thank you for using my JukeBox.");
check = true;
}
}
}
}
The reason this does not work is that you may be calling sc.nextInt() multiple times, when you expect your user to enter only one value.
You should store the result in a variable, and use that variable in all your if statements. Alternatively, you could use a switch statement.
var userEntry = sc.nextInt();
sc.nextLine(); // skip to the end of the line
if ( userEntry == 1 ) {
...
} else if ( userEntry == 2 ) {
...
} else if ( userEntry == 3 ) {
...
} else ...
or
var userEntry = sc.nextInt();
sc.nextLine(); // skip to the end of the line
switch ( userEntry ) {
case 1:
...
break;
case 2:
...
break;
case 3:
...
break;
default:
...
break;
}
Change if to else if and take input nextInt() for one time in each loop
int num = sc.nextInt()
if (num == 1 ) {
}
else if ( num == 2 ) {
}
else if ( num == 3 ) {
}
....

Having an issueif else and if else statements.

I'm really new to java (third week of class), but I've been trying to work on this code for hours and I just can't seem to find an answer to what I'm doing. javac tells me I only have three errors, but I'm wondering if there's more than that.
Here's my code, and I know my average section still needs work but i just cant figure out what's going on with the middle section of if and else statements. Sorry if this is really dumb, and im sure my syntax is all over the place:
import java.util.Scanner;
public class Program1
{
static public void main( String args [ ] )
{
int grade;
int A,B,C,D,F;
A = 0;
B = 0;
C = 0;
D = 0;
F = 0;
System.out.println( "*************** Grade Computer *************");
// ********************** //
Scanner kbd = new Scanner (System.in);
System.out.println("Enter Students First Name: ");
String fname = kbd.next( );
System.out.println("Enter Students Middle Initial: ");
String mi = kbd.next( );
System.out.println("Enter Students Last Name: ");
String lname = kbd.next( );
System.out.println("Enter First Exam Grade: ");
int firstexam = kbd.nextInt( );
System.out.println("Enter Second Exam Grade: ");
int secondexam = kbd.nextInt( );
System.out.println("Enter Third Exam Grade: ");
int thirdexam = kbd.nextInt( );
System.out.println("Was the bonus done? [yes/no] : ");
boolean b = kbd.nextBoolean( );
boolean yes = true;
boolean no = false;
// *********************** //
if(true)
{
{
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 )));
{
System.out.println(firstexam);
}
else if((secondexam * 0.60) >= (thirdexam * 0.80));
{
System.out.println(secondexam * 0.60);
}
else {
System.out.println(thirdexam * 0.80);
}
}
if(true)
{
if((secondexam >= firstexam) & ((thirdexam * 0.80) >= secondexam));
{
if(secondexam >= (thirdexam * 0.80));
{
System.out.println(secondexam);
}
}
else {
System.out.println(thirdexam * 0.80);
}
}
else {
System.out.println(firstexam);
System.out.println(secondexam);
System.out.println(thirdexam);
}
}
// ********************** //
System.out.println(" **********Grade Summary********** ");
double average = calcAverage(firstexam, secondexam, thirdexam);
System.out.println("Grade Report For: " + fname);
if (true)
{
System.out.println("Bonus was done so grades are adjusted if appropriate.");
}
else
{
System.out.println("Bonus was not done.");
}
System.out.println("Exam 1: " + firstexam);
System.out.println("Exam 2: " + secondexam);
System.out.println("Exam 3: " + thirdexam);
System.out.println("The average is: " + average);
determineGrade(average);
}
public static double calcAverage(int firstexam, int secondexam, int thirdexam)
{
double average = (firstexam + secondexam + thirdexam) / 3.0;
return average;
}
public static void determineGrade(double average)
{
if (average>90)
{
System.out.println("Grade: A");
}
else if (average>=80)
{
System.out.println("Grade: B");
}
else if (average>=70)
{
System.out.println("Grade: C");
}
else if (average>=60)
{
System.out.println("Grade: D");
}
else if (average<60)
{
System.out.println("Grade: F");
}
}
// ************** //
}
Your if statements having ; in the end
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 )));
They are considering as statements and proceeding further.
Remove all of them in the end of each statement.
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 ))) (;)
The ; shouldn't be here.
Difference between & and && :
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
(x != 0) & (1/x > 1) <-- this means evaluate (x != 0) then evaluate (1/x > 1) then do the &. the problem is that for x=0 this will throw an exception.
(x != 0) && (1/x > 1) <-- this means evaluate (x != 0) and only if this is true then evaluate (1/x > 1) so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1).
An other thing :
if(true)
{
{
This should be deleted because it just adds more code , it will be executed every time so no need to add it.
Besides the colon the end of the if statement you also should keep in mind that if you use
if(true){
}else{
}
The else statement will never execute cos the if will always be true, so you should be using the yes/no variables as flags for your if statement instead of the "true" itself.
If your statements inside the if should always be executed then you don't need the conditions at all.

Categories