Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to save user input in a file.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to save user input in a file.

    I have been trying to solve this for about a week, but, couldn't do it.Can any please help me on this...


    import java.io.*;
    import java.util.List;
    import java.util.Scanner;

    public class Sstudent
    {

    private String studentName;
    private String studentBirthday;
    private String studentGPA;
    private String studentAddress;

    public Sstudent()
    {
    }
    public Sstudent(String nName, String nBirthday, String nGPA, String nAddress)
    {
    this.studentName = nName;
    this.studentBirthday= nBirthday;
    this.studentGPA = nGPA;
    this.studentAddress= nAddress;
    }

    public void setStudentName(String newStudentName)
    {
    studentName = newStudentName;
    }

    public void setStudentBirthday(String newStudentBirthday)
    {
    studentBirthday = newStudentBirthday;
    }

    public void setStudentGpa(String newStudentGPA)
    {
    studentGPA = newStudentGPA;
    }

    public void setStudentAddress(String newStudentAddress)
    {
    studentAddress = newStudentAddress;
    }



    public String getStudentName()
    {
    return studentName;
    }

    public String getStudentBirthday()
    {
    return studentBirthday;
    }

    public String getStudentGPA()
    {
    return studentGPA;
    }

    public String getStudentAddress()
    {
    return studentAddress;
    }


    public static void main(String[] args)
    {

    try{
    BufferedWriter br = new BufferedWriter(
    new FileWriter("C:\\Users\\User\\Desktop\\Project\\inp ut.txt"));


    Scanner input = new Scanner(System.in);


    Sstudent student=new Sstudent();
    System.out.println("Enter student details\n");



    System.out.println("\nEnter student name");
    student.setStudentName(input.nextLine(studentName) );


    System.out.println("\nEnter student Birth Day");
    student.setStudentBirthday(input.next());

    System.out.println("\nEnter student GPA");
    student.setStudentGpa(input.next());

    System.out.println("\nEnter student Address");
    student.setStudentAddress(input.next());


    br.close();


    }catch(Exception e){
    System.out.println("Error!");
    }

    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to save user input in a file.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Please explain what your problems are. What does the program do when it is compiled and executed? If there are error messages, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to save user input in a file.

    You might try to actually use br before posting your errors.

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to save user input in a file.

    thanks for the response bro....i wanted to save the inputs of the user in a text file in a folder i created in my desktop. when i compile the programe and responded, the text file was created, but, the file was empty nothing inside....there was no error........i just wanted to create a text file and save the user inputs in it....
    thanks.

    --- Update ---

    thanks buddy....i am not still good in java....beginning stage...could you please show me how, if u could....
    thanks...

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to save user input in a file.

    You must call some methods of the BufferedWriter class that will write to the file. Look at the API doc for the BufferedWriter class to see what method(s) to use.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 19
    Last Post: March 15th, 2013, 03:11 PM
  2. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 02:21 PM
  3. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum Object Oriented Programming
    Replies: 3
    Last Post: August 12th, 2011, 04:42 AM
  4. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 10:23 AM
  5. Help with making the user input to store in file
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 06:52 AM