Question

Here is my code:

while (scan.hasNext()) 
    {
        if(scan.next().toLowerCase().equals("car") )
        {
            carsdata[count++] = new Car(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.next(), scan.next(), scan.next());
        }
    }

    while (scan2.hasNext()) 
    {
        if(scan2.next().toLowerCase().equals("motorcycle"))
        {
            motorcyclesdata[count++] = new Motorcycle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.next(), scan.next(), scan.next());
        }
    }
    while (scan3.hasNext()) 
    {
        if(scan3.next().toLowerCase().equals("van"))
        {
            vansdata[count++] = new Van(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.nextInt(), scan.next());
        }
    }
    while (scan4.hasNext()) 
    {
        if(scan4.next().toLowerCase().equals("pickup"))
        {
            pickupsdata[count++] = new Pickup(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.nextInt(), scan.nextInt(), scan.next());
        }
    }

For some reason lost to me, the motorcycle part isnt executed. I have checked by putting message boxes into that loop. Doesnt work. However the Car one works perfectly.

Here is the code im using to output into a textarea:

if (e.getSource() == btnMotorcycles)
        {
            vehicleTextArea.setText("");
            int i =0;

            while (motorcyclesdata[i] != null)
            {

                vehicleTextArea.insert("\t" + motorcyclesdata[i].seatMaterial, 1);
                vehicleTextArea.insert("\t" + motorcyclesdata[i].seatColor, 1);
                vehicleTextArea.insert("\t" + motorcyclesdata[i].mainColor, 1);
                vehicleTextArea.insert("\t" + motorcyclesdata[i].price, 1);
                vehicleTextArea.insert("\t" + motorcyclesdata[i].passengers, 1);
                vehicleTextArea.insert("\t" + motorcyclesdata[i].model, 1);
                vehicleTextArea.insert("\t" + motorcyclesdata[i].brand, 1);
                vehicleTextArea.insert("\t" + motorcyclesdata[i].cc, 1);
                vehicleTextArea.insert("\t" + motorcyclesdata[i].engineCap, 1);
                vehicleTextArea.insert("" + motorcyclesdata[i].licenseNo, 1);
                vehicleTextArea.insert("\n", 1);
                i++;
            }
            vehicleTextArea.insert("Licence No \t Engine\t CC \t Brand \t Model \t Passengers \t Price \t Material \t Paint \t Main Color \t Seat Color \t Seat Material", 0);
        }//end if

What am i overlooking, besides possibly the effectiveness of the code?

Was it helpful?

Solution

you should try something like following

while (scan.hasNext()) 
    {
     String scanStr = scan.next();
        if(scanStr.toLowerCase().equals("car") )
        {
            carsdata[count++] = new Car(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.next(), scan.next(), scan.next());
        }

        if(scanStr.toLowerCase().equals("motorcycle"))
        {
            motorcyclesdata[count++] = new Motorcycle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.next(), scan.next(), scan.next());
        }

        if(scanStr.toLowerCase().equals("van"))
        {
            vansdata[count++] = new Van(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.nextInt(), scan.next());
        }

        if(scanStr.toLowerCase().equals("pickup"))
        {
            pickupsdata[count++] = new Pickup(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.nextInt(), scan.nextInt(), scan.next());
        }
    }

also following is an observation : Just check if you want to have it the same way as you are implementing

    while (scan2.hasNext()) //here you are checking for Sacnner Object 2
        {
            if(scan2.next().toLowerCase().equals("motorcycle"))
            {
//here you are using Scanner Object 1 only make sure you intended to implement it to read it from scanner Object 1 only
                motorcyclesdata[count++] = new Motorcycle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.next(), scan.next(), scan.next()); 
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top