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 14 of 14

Thread: Help with my IO code

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with my IO code

    Hi can anyone please help me make a code for my programming class and at the same time help explain what are the codes and all thanks here is the problem


    Create a program that would accept personal details as shown below, save in a text file and display/load from the text file. You may use console or GUI base for the user input with exception handling. You may create save and load in one program or you may have multiple java files.

    Hint: Arrays

    Sample user input. (console)

    Name:

    Age:

    Course:

    Year Level:

    Specialization:


    That is what our teacher gave us. Thank you in advance

  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: Help with my IO code

    What have you tried?
    Do you have any specific java programming questions?

    Be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my IO code

    i didn't start anything yet. But I would want to see how you would code if you were given the problem.

  4. #4
    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: Help with my IO code

    I'd start by making a detailed design of what the steps the program needs to take to solve the problem. When the steps make sense, I'd try to write the code to implement the steps in the design.

    Be sure to compile often to catch errors and fix them before adding more code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my IO code

    well first is that it should ask the user the details which are name, age, year level in college, program taking in college. then after it should create a text file in which the data is saved then the program should be able to read the data in the txt file

  6. #6
    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: Help with my IO code

    Ok, try writing the code to ask the user for the data and to read that data into the program.
    Get that working first before writing any more code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my IO code

    here is what I have so far
    import java.io.FileOutputStream; 
    import java.util.*;
    public class IOData {
    public static Scanner input = new Scanner(System.in);
     
    	public static void main(String[] args) {
    		try {
    			String name, course, specialization;
    			int age, yearlvl;
     
    				System.out.println("Please Enter Your Name: ");
    				name = input.next();
    				System.out.println("Please Enter Your Course: ");
    				course = input.next();
    				System.out.print("Please Enter Your Specialization: ");
    				specialization = input.next();
     
    			FileOutputStream ybs=new FileOutputStream("testout5.txt");
    			byte b[]=name.getBytes(); 
                byte c[]=course.getBytes(); 
                byte d[]=specialization.getBytes(); 
                ybs.write(b);    
                ybs.write(c);
                ybs.write(d);
                ybs.close();   
     
    			System.out.println("Success");
    		}
    		catch (Exception e) {
    			System.out.println("Error");
    		}
    	}
    }

  8. #8
    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: Help with my IO code

    Does it work as desired?
    Do you have any specific questions?

    Why the unusual indentations? Normally code at the same logic level has the same amount of indentation.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my IO code

    it writes what ever I input in the text file but there are no spaces between them and I still need to read the text file after it writes the data in the text file

  10. #10
    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: Help with my IO code

    but there are no spaces between them
    Time for more design decisions:
    How many different users' information do you want to save in the file?
    How will the data for each user be separated from the data for the other users?
    How will the data fields for each user be separated? For example, a space or a ; or what?
    Are there any fields that could contain spaces or other special characters? For example first and last name or the name of a course

    I still need to read the text file after it writes the data in the text file
    Don't worry about that part until the first part is working as desired. Keep the confusion to a minimum.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my IO code

    Only one user. I need the full name of the use. I just need the program to be able to read after it writes

  12. #12
    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: Help with my IO code

    I just need the program to be able to read
    Does the current program work as desired? If not, wait until it is working before adding any more code.
    Keep the problems to a minimum. Do one thing at a time.

    Only one user
    That answers the first and second questions.
    What about the other questions?
    How will the data fields for each user be separated? For example, a space or a ; or what?
    Are there any fields that could contain spaces or other special characters? For example first and last name or the name of a course
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my IO code

    it works it can already write the data to the text file but I still don't know how to make the program read the text file

  14. #14
    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: Help with my IO code

    Ok, if the current version works as desired, you can try to read the data from the file.
    How are the data fields separated in the disk file? There needs to be way for the program to know when one field ends so it can keep the data for each field separate from the data for another field.
    One way is to have a special character between each field. Often that can be a space except for the case where a field can contain a space. If fields can have spaces then another separator character has to be chosen.
    Another solution is to have each field be fixed length and then the program can read the fields without looking for the separator character
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM