0

I am trying to create a simple reservation system. I read lines from an input, before finish the project I write them to an output file.

My input is like

ADDTRAIN,Cumhuriyet,Regional,80,90,50,40

ADDTRAIN,Baskent,Express,80,90,1,2

ADDTRAIN,Baskent,Express,80,90,50,40

If one train has a same name with another I have to write "Command Failed... Train Name: ...."

I split them for comma then i create an Arraylist for Train.

File text = new File();
    PrintWriter writer = new PrintWriter();
    Scanner scnr = new Scanner(text);
  
    while(scnr.hasNextLine()){
       
        String line = scnr.nextLine();
        String[] atts = line.split(",");
        if(atts[0].equals("ADDTRAIN"))
        {
            train.add(new Train(atts[0],atts[1],atts[2],atts[3],atts[4],atts[5],atts[6]));
        }

    for(int i= 1; i<train.size(); i++)
    {
        writer.print(train.get(i-1).Writer());
        if(train.get(i).getTrainName().equals(train.get(i-1).getTrainName()))
        {
            writer.print(train.get(i).WrongWriter());
        }
    }
    
    scnr.close();
    writer.close();

I can only check for previous line. For example if my input is like

ADDTRAIN,Cumhuriyet,Regional,80,90,50,40

ADDTRAIN,Baskent,Express,80,90,1,2

ADDTRAIN,Baskent,Express,80,90,50,40

ADDTRAIN,Cumhuriyet,Regional,80,90,50,40

My program doesn't write a "Command Failed" line to output. How can i solve this problem?

3
  • 2
    At what point in your code are you writing Command Failed?! Commented Apr 25, 2014 at 9:21
  • @Bitmap I'm guessing train.get(i).WrongWriter() returns the string? Dunno... Commented Apr 25, 2014 at 9:23
  • yeah. train.get(i).WrongWriter() write "Command Failed... Train Name:" error to the output. But it only checks previous line, not all objects. For train.get(0).getTrainName I don't need to check anything. But train.get(1) I need to check train.get(0).getTrainName. And for train.get(2) I need to check both train.get(0).getTrainName and train.get(1).getTrainName Commented Apr 25, 2014 at 9:27

2 Answers 2

3

You should use HashMap to store your data: Key=TrainName, Value=TrainObject. Using a HashMap provides both an easy and efficient way to check/disallow duplicates

if(trainMap.contains(trainName){ System.out.println("Error - train already exists"); }

Sign up to request clarification or add additional context in comments.

3 Comments

This would require all fields to be the same (or a dodgy equals method). The OP seems only interested if the name is the same.
updated my answer, As a key OP can use the train name, and as a value - the train object. As far as i understood the question, this solves the problem
I'll try to use this.
1

If you don't want to use a HashMap (which is a good solution), you can use two loops.

for(int i = 0; i<train.size(); i++)
{
    writer.print(train.get(i).Writer());
    for(int j = 0; j<train.size(); ++j)
    {
        if (i==j) continue;
        if(train.get(i).getTrainName().equals(train.get(j).getTrainName()))
        {
            writer.print(train.get(i).WrongWriter());
            break;
        }
    }
}

3 Comments

Two for loops is inefficient and there is more to code :)
@SvetlinZarev I agree. It is just an alternative. I also prefer HashMap, but I think this option is worth mentioning anyway.
but i never used HashMap so that would be better for me :D thanks man

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.