strcat in java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: strcat in java

  1. #1
    Join Date
    Apr 2008
    Location
    Finland
    Posts
    52

    strcat in java

    I need to write data to a file (add to string) like this:
    1 data new_data
    2 data2 new_data2

    I'm trying to do it with "strcat" but since java is not my strongest language I'm not getting it right. I have a variable String s; but I don't know how to get the contents on "data" in it. I'm trying to write "new_data" like this:
    outPfile.write(s += new_data);
    Any ideas?

    This is what I'm trying to do:

    #include <iostream>
    #include <string>
    #include <fstream>

    using namespace std;

    ofstream save;

    int main (void)
    {
    char filename[101];
    cout << "Give filename ";
    cin.get(filename,101).ignore(1000,'\n');

    save.open(filename);
    if(save.fail())
    {
    cout << "Could not open";
    cin.get();
    return 0;
    }

    string lastname = "Mouse";
    string firstname;
    cout << "Give first name ";
    cin >> firstname;

    lastname += firstname;
    save << lastname << endl;

    cin.ignore(1000,'\n').ignore(1000,'\n');
    return 0;
    }
    Last edited by sypress; 09-16-2008 at 05:40 AM.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    You missed your turn. This is a java forum.

  3. #3
    Join Date
    Apr 2008
    Location
    Finland
    Posts
    52
    I want to do the c++ program in java.

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    So, you need the c++ code you posted rewritten in Java??
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  5. #5
    Join Date
    Apr 2008
    Location
    Finland
    Posts
    52
    Not all of it, just how to put the surname from the file and the firstname together. String s needs to read the surname but HOW??!?
    And does this work?
    outPfile.write(s += firstname);
    Last edited by sypress; 09-16-2008 at 09:16 AM.

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    I was going to post an example, but I thought something like this might be a better learning tool.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  7. #7
    Join Date
    Apr 2008
    Location
    Finland
    Posts
    52
    Thanks!
    But not quite there yet.
    I read the surname from the file with: String s = inPfile.readLine(); // s = null???????????
    Then I read the firstname from the user with: firstname = reader.readLine();
    And write them both back in the file with: outPfile.write(s + firstname);

    BUT there is a '\n' at the end of the file so the names end up in different rows. How can I read only the name and not the '\n'???
    Last edited by sypress; 09-16-2008 at 12:50 PM.

  8. #8
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    What classes are inPfile and reader?
    The readLine method I'm familiar with doesn't return a newline char.
    Where is the "end of the file" that you mention?
    Is the newline part of s?
    Can you post a bit more code to show what is happening?

  9. #9
    Join Date
    Apr 2008
    Location
    Finland
    Posts
    52
    Well, I could be wrong about the '\n' but the file looks like this:
    Mouse
    nullMinnie


    This is the code:

    import java.net.*;
    import java.io.*;
    import java.util.*;

    public class FileNames
    {
    public static final int ADD_NAME = 1;
    }
    ...
    String names = "Names.dat";
    ...
    String firstname = reader.readLine();
    String s = inPfile.readLine();
    outPfile.write(s + firstname);
    outPfile.flush();

  10. #10
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    You've left off all the object definitions. Your code as posted does NOT compile.
    ... is not legal code. The compiler isn't smart to fill it in and I don't have my crystal ball.

    Where does the null come from in your output?

Similar Threads

  1. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 05:58 AM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  3. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  4. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 04:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center