0

I have a little problem with this thing. I tried to make an array where the user enters data, and the data needs to be saved in an object.obj file.

This is my code:

  import java.util.*;

  public class insertData implements java.io.Serializable 
{
      Scanner keyboard=new Scanner(System.in);
      private String One;
      private String Two;


      public void new()
      {
          System.out.println("How many lines do you want to insert?");
          int p=keyboard.nextInt();

          for (int i = 0; i < p; i++) 
          {
              One=keyboard.next();
              Two=keyboard.next();
          }
      }

      public String toString()
      {
        new();
        String text=One+"\t"+Two;
        return text;
      }

}

And the File class will be:

  import java.io.*;

  public class fileA 
  {
      insertData iD = new insertData()
      String a;
      ObjectOutputStream oos;
      ObjectInputStream ois;

      public void writeFile(File file)
     {
         try 
         {
              oos=new ObjectOutputStream(new FileOutputStream(file1));
              a=iD.toString();
              oos.writeObject(a);
              oos.close();

         } 
         catch (FileNotFoundException e) 
         {
             e.printStackTrace();
         } 
         catch (IOException e) 
         {
             e.printStackTrace();
         }
     }

     public void readFile(File file1)
     {
          try  
          {
                ois=new ObjectInputStream(new FileInputStream(file1));
                String str=(String)ois.readObject();
                System.out.println(str);
                ois.close();
          }
          catch (IOException ex) 
          {
            System.err.println(ex);
          }
          catch (ClassNotFoundException ex) 
          {
            System.err.println(ex);
          }

    }
  }

The problem is that this saves only the last line of data entered. The results should be:

Text1     Text2
aaaaa     bbbbb
ccccc     ddddd
5
  • new() is not a valid method. Commented May 16, 2013 at 0:39
  • Where is your main class ? Commented May 16, 2013 at 0:39
  • 1
    @Eng.Fouad is right. You can't use java reserved keywords as method name. Which raises a doubt on how you could execute the program in the first place. You must have got compiler errors. Commented May 16, 2013 at 0:49
  • 2
    (Off-topic, but some friendly Java advice: you should use UppercaseStartingCamelCase for class names (eg FileA not fileA and InsertData not insertData). Your method names follow the correct conventional already). Commented May 16, 2013 at 0:51
  • 1
    There is no array here. Not a real question. Commented May 16, 2013 at 1:48

1 Answer 1

1

The issue is that you only have one One and Two so when you go over this loop.

for (int i = 0; i < p; i++) 
{
    One=keyboard.next();
    Two=keyboard.next();
}

you are overwriting the values. So that is why you are only getting the last values.

What you need to do is either store these elements in a collection.

List<String> ones = new ArrayList<String>();
List<String> twos = new ArrayList<String>();
...
for (int i = 0; i < p; i++) 
{
    ones.add(keyboard.next());
    twos.add(keyboard.next());
}

or you can append to a buffer.

StringBuilder sb = new StringBuilder();
...
for (int i = 0; i < p; i++) 
{
    sb.append(keyboard.next()+"    " +keyboard.next()+"\n");
}

Then just write out to the file appropriately

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

2 Comments

Thanks for your answer @greedybuddha. I finally get to solve the problem. I was an "+=" problem for append the text. PS: main class was omitted on purpose because the problem couldn't be there and new() it was just an example... ;-) Thanks again for your help. Problem solved!
No problem, the += is a solution that works as well. It's doing the same thing as the StringBuilder but directy, (and a bit less efficiently ;) ).

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.