0

So i tried making a simple java reservation system but the problem is that the scanner functions won't work so the program is not taking any inputs. I tried putting the scanners but it shows me an warning about the local variables not being used when i tried to run the program and hence the scanners aren't working so the whole program is stuck on the input part and not doing anything else. Im pretty new to java and is trying to learn by creating projects so i will appreciate any help and advices thank you. Here is the code: So the part or line of code that shows a warning local variable not being used are lines 8,9,16,34,36,46,48 so far those are the only problems that are showing as well as the input or the scanners not working when i run the program.

package random.java;
import java.util.Scanner;
public class AirlineReservation {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);    
        int planeclass[] = new int[10];
        boolean available = false;
        boolean y=true;
        int number = 0, Premium = 1, Regular = 6;
        String x = null;
        
        while(Premium<=5&&Regular<=10)
        {
            System.out.print("Enter 1 for Premium class or 2 for Regular or -1 to exit");
            Scanner in1 = new Scanner(System.in);
            
                if(number == -1);
                break;
                
        }
        if(number == 1 && Premium <= 5) {
            System.out.println("You are assigned to First class\t"+"Seat#" + Premium);
            Premium++;
        }
        else if(number == 2 && Regular <=10) {
            System.out.println("You are assigned to Regular class\t" + Regular);
            Regular++;
        }
        else if(number == 1 && Premium >= 5) {
            System.out.println("First class full"+"  ");
            if(Regular <= 10) {
                System.out.println("Is it acceptable to be placed in the Regular class");
                Scanner in1 = new Scanner(System.in);
                in.close();
                if(x.equals("y")) {
                    System.out.print("Seat Reserved in Regular class\t" + Regular);
                }
                else {
                    System.out.println("Next Flight leaves in 3 hours.\t");
                }
            }
            
            else if(number == 2 && Regular == 10) {
                System.out.println("Regular class full\t");
                    Scanner in1 = new Scanner(System.in);
                    in.close();
                    if(x.equals("y")) {
                        System.out.println("You are assigned to Premium\t" + Premium);
                        
                    }
                    else if (Premium == 5 && Regular == 10) {
                        System.out.println("Plane is full, next flight is in 3");
                    }
                    else {
                        System.out.println("Next flight leaves in 3 hours\t");
                    }
                    for(int i=1; i<planeclass.length; i++) {
                        System.out.println(planeclass[Premium] + planeclass[Regular]);
                    }
            }
        }
    }           
}   
  • 3
    You don't call any method of `Scanner` except `close` – JCWasmx86 Aug 13 '20 at 20:01
  • Where are you using in.nextInt() or in.next() method? – SHASHI SHEKHAR Barnwal Aug 13 '20 at 20:04
  • How do i call the method create a new one for it? and where do i put it? – SirenHeadMan Aug 13 '20 at 20:07
  • 2
    Read up on how Scanner works. For instance: https://mkyong.com/java/java-scanner-examples/ – Tobb Aug 13 '20 at 20:07
  • You need to instruct Scanner to wait for an input from the user. This is done by calling the nextLine() or nextInt() method of Scanner. – Tobb Aug 13 '20 at 20:08
  • This question is a bit too broad for Stack Overflow IMO. You seem to misunderstand how variables are initialized and how you can ask the user for input. I'd advice to start small. Ask the user for one input and print it. If that works, use an if-statement to do something based on the value, etc. If your code is broken, fix that small portion. It doesn't make sense to continue writing your program if is already broken. – Ivar Aug 13 '20 at 20:19
  • You should just use a single Scanner instance for all your console input. And never close it. – WJS Aug 13 '20 at 20:28
  • 1
    Does this answer your question? [How can I read input from the console using the Scanner class in Java?](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) – Strikegently Aug 13 '20 at 20:33
  • Please put a comment on each of the lines with an error. Your code isn't numbered, and it is difficult to find the specific lines. – NomadMaker Aug 13 '20 at 20:41
  • For lines 8 and 9, you create two variables, "available" and "y", but never use them. – NomadMaker Aug 13 '20 at 20:42
  • One problem is that you create a Scanner in, then later another Scanner in1, and then you close in. The problem is that if you close any scanner created with System.in, then System.in is closed, and after that no scanner created on System.in will work. There is no point to creating multiple scanners on System.in. Just reuse the first one. – NomadMaker Aug 13 '20 at 20:45
  • How do i reuse the first one on the other conditional statements?. i put one on the first line but the conditions doesn't work – SirenHeadMan Aug 13 '20 at 21:14

1 Answers1

1

If you want to take user input then you should create an object and then use that object to take input.

Example:


Scanner input = new Scanner(System.in); //here input is the name of object int

x = input.nextInt(); //now the above expression take an integer as a input from user
                       //nextInt() is method of class Scanner

Use this tutorial to see all the methods of Scanner class

potame
  • 6,684
  • 4
  • 20
  • 30