7 Replies - 1041 Views - Last Post: 08 October 2012 - 07:22 AM Rate Topic: -----

#1 Quinn92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-September 11

Java I/O problem

Posted 08 October 2012 - 05:10 AM

Hi,

I have to create two classes, one to take user input(Name,DOB,Address e.t.c) and save to a file + another class to open this file and display the entered text.

This is what i have done so far....


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

public class StudentData {


 public static void main(String args[])
  {
  try{
 
	//needed for user to input selection
      Scanner input = new Scanner(System.in);

      //Request the Student Name
      System.out.println("Please enter your Name: ");
      String userInput = input.nextLine();

      //Request the Student's address 
      System.out.println("Please enter your address: ");
      userInput = input.nextLine();

      //Request for DOB
      System.out.println("Please enter your DOB: ");
      userInput = input.nextLine();
      
    //Request for Student ID
      System.out.println("Please enter your Student ID: ");
      userInput = input.nextLine();
	  
      
	  
	  

	  
  
  BufferedWriter out = new BufferedWriter(new FileWriter("studentData.txt"));
  
  
  out.write(userInput);//Write out the specfied string to the file
  out.newLine();
  out.close();//flushes and closes the stream

  
  
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}


and

import java.io.*;

public class DataReader {
    BufferedReader in;
    String read;
 
    public DataReader(){
        try {
            //open a bufferedReader to file studentData.txt
            in = new BufferedReader(new FileReader("studentData.txt"));

            //read the text from studenData.txt and save into a string
            read = in.readLine();

            //print out the text
            System.out.println("file output: " + read);

            //safely close the BufferedReader after use
            in.close();
        }catch(IOException e){
            System.out.println("There was a problem:" + e);
        }
    }

    public static void main(String[] args){
        DataReader DataReader = new DataReader();
    }
}




They both complie but only the last part of the needed information is displayed e.g the student ID...

I don't have alot of experience with I/O in java, any pointers as to where I am going wrong would be apperciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Java I/O problem

#2 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Java I/O problem

Posted 08 October 2012 - 05:20 AM

You're using the same string variable (userString) for all four inputs, assigning straight to it four times in a row without doing anything with the previous value. Is that really what you meant to do?
Was This Post Helpful? 1
  • +
  • -

#3 Quinn92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-September 11

Re: Java I/O problem

Posted 08 October 2012 - 05:32 AM

View PostMaynia, on 08 October 2012 - 05:20 AM, said:

You're using the same string variable (userString) for all four inputs, assigning straight to it four times in a row without doing anything with the previous value. Is that really what you meant to do?


No it's not...to fix that though will I have write to the text every time I have input from the user?Or is there a way of assigning different string variables and writing them to the text file at the same time?
Was This Post Helpful? 0
  • +
  • -

#4 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Java I/O problem

Posted 08 October 2012 - 05:40 AM

Oh, there are always options, this is a black art after all.

You could declare a new String each time, as you've done here:
String userInput = input.nextLine();

(using different names, obviously) and feed them all into the writer at the end.

You could call the writer immediately after every input, too.

Alternatively, you could look into appending the input strings together! Choices.

This post has been edited by Maynia: 08 October 2012 - 05:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 Quinn92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-September 11

Re: Java I/O problem

Posted 08 October 2012 - 05:49 AM

View PostMaynia, on 08 October 2012 - 05:40 AM, said:

Oh, there are always options, this is a black art after all.

You could declare a new String each time, as you've done here:
String userInput = input.nextLine();

(using different names, obviously) and feed them all into the writer at the end.

You could call the writer immediately after every input, too.

Alternatively, you could look into appending the input strings together! Choices.


Thanks for your advice, I followed it and the output in my second class, now displays as one long string...how can I display each individual string on its own line?
Was This Post Helpful? 0
  • +
  • -

#6 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Java I/O problem

Posted 08 October 2012 - 05:55 AM

Again, it's doing exactly what you told it to. And again, there are various methods for dealing with it. The most obvious, though?

Take a look at the BufferedWriter API - in fact, get into the habit of reading this stuff generally, it'll help you out a lot. There's often a predefined method right there to do what you want to do.

edit: you've already used the method you need, even! :bigsmile:

edit2: might need to do some adaptation to your reader too, but that's trivial.

This post has been edited by Maynia: 08 October 2012 - 06:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 Quinn92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-September 11

Re: Java I/O problem

Posted 08 October 2012 - 06:22 AM

View PostMaynia, on 08 October 2012 - 05:55 AM, said:

Again, it's doing exactly what you told it to. And again, there are various methods for dealing with it. The most obvious, though?

Take a look at the BufferedWriter API - in fact, get into the habit of reading this stuff generally, it'll help you out a lot. There's often a predefined method right there to do what you want to do.

edit: you've already used the method you need, even! :bigsmile:

edit2: might need to do some adaptation to your reader too, but that's trivial.



Thanks for all your help got everything running the way I want it! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: Java I/O problem

Posted 08 October 2012 - 07:22 AM

Use PrintWriter

PrintWriter out = new PrintWriter(new FileWriter("studentData.txt"));
out.println(studentName);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1