Java File I/O project

Ok so im taking intermediate java and I remember nil from intro to java. I've got to make a program that reads a text file (the file was provided by the professor), then Ive got to use the stingtokenizer method to break the the file into pieces (using , as the delimiter). Then I have to output to the screen and to a file each student and their information (the file is a list of students and their classes). Theres actually a little more than that but right now thats all im concerned with...got to take it one step at a time.
Right now Im building the read method for my class which should find the file, read the file, and then tokenize it. This is what Ive got so far, but I dont know what to put as a return value I need help understanding the concept of what Im doing.
package homework1;
* @author Joseph Weaver
import java.util.*;
import java.io.*;
public class Inforetrieval {
    public String ReadFile()throws IOException {
        BufferedReader input = new BufferedReader(new FileReader("students.txt"));
        String studentinfo;
        while ((studentinfo = input.readLine()) != null) {
            StringTokenizer line = new StringTokenizer(studentinfo);
            while (line.hasMoreTokens()) {
                System.out.println(line.nextToken());
        input.close();
        return;
}

Does your tokenizer work the way you want it to? It looks like you've got printing to the screen, so you're concerned with writing to a file?
First of all you need to know what exactly you're going to write to a file. Since you're reading from a file it would probably make sense that the files your program generates (writes to) will be formatted differently. One file per student? Figure out or decide what you want to write to a file.
Next, you need to decide what your method ReadFile() should do. It looks like its doing several perhaps unrelated things - not only is it reading a file, but it's also tokenizing the file and printing to the screen. Would you expect a method named ReadFile() to do all that?
I suggest breaking your logic into several methods. Your methods should perform only one thing. Once you've separated your logic into several small methods, it should be clear what their return values should be.
Once you've done this, you could take the results from the tokenize method and pass them on to a method that will print the tokens to the screen and and to a method that will write the tokens to a file.
As you said, there's more to the assignment but this is the part you're working on now. If you divide the logic into methods as suggested, later on if you need to tokenize something or write something to a file, you'll have already written methods to do that.
Or, you could put everything into the main() class and get the assignment done quickly.

Similar Messages

  • How to use external java files into my project

    Hi all,
    I need your help to import my external java files in to my project.
    The files are in package folder.
    So how to tell the import command with the path of the folder.
    I read also that I can make a jar file and integrate it into the project.
    How can I make jar file.
    Best regards...

    there is one utility in j2se jar and check the option also
    put that jar file in lib folder of yr application folder
    [email protected]

  • How to include .java file in all projects?

    Hi Friends, i have some questions related....
    1) i have a file .java with some great functions, this functions i need use in all projects, then what is the easy by moment i start a project, and copy this file to project..
      is possible have only 1 file with these utilities functions? (if yes how to declare or use in all projects?)
    Note these utilkities file i want use on JAVA desktop(swinf and javafx), on JSP webpages and in the futhurer on the mobile.
    2) i have a JSP project in 1 .JSP file i have 8 Tabs (CSS tabs) when user click on tab1 i execute some jsp java code. and if user click on tab2 i execute a different JSP code, but my problem is, the JSP file is large large large, 1500 lines, my question is: is possible (similar in PHP) do an include?
    <%
       include tab1.jsp
    %>
    thanks

    jamiguel77 wrote:
          1) i have a file .java with some great functions,
    If that is one Java file only I really doubt that those functions are "great" from a technical point of view...
    this functions i need use in all projects, then what is the easy by moment i start a project, and copy this file to project..
      is possible have only 1 file with these utilities functions? (if yes how to declare or use in all projects?)
    Note these utilkities file i want use on JAVA desktop(swinf and javafx), on JSP webpages and in the futhurer on the mobile.
    There is a  common approach to this problem which is a dependency repository. Various build tools address the dependency resolution problem. Most common are maven and greadle. Place your Jar file on a location you can reach from your various developement environments (eg. a share on a server in your network) Then configure your build tool to know about your repository.
    A better solution that a naked net share is a nexus server, which will not only hanlde dependencies you namually placed on your share but also dependencies available on public repositories.
    In your project you simply declare the (direct) dependencies of the Code you're writing. For maven you need an aditional file in your project: the pom.xml. maven (or greadle) will handle all the transient dependencies for you (as long as the pom.xml of the dependency also declares its own dependencies and those other dependencies are reachable for maven too).
    bye
    TPD

  • Creating a java file in the project.

    Hi
    I am using jdev 11.1.2,
    And i have created a table type in database navigator in my hr schema.Now i want to create a java file for this table type.
    In jdev 11.1.1.3 i guess i can do it by just right clicking on the type and selecting the 'Generate to java' option, but i cant find the same here in 11.1.2,
    How can fulfill my requirement now.
    Help needed .Thanks/.

    Since you have not specified the technology, i will take a dig and assue you are wanting to create a JPA/EJB project
    http://xmlandmore.blogspot.com/2011/01/create-jpa-entities-from-tables-with.html

  • DISASTER!  Lost a .java file after a Project Rebuild

    Is there a way to get it back?  It was the Component.java file.  When I open the component and go to "Implement" it says the file is gone.
    Is this file stored in the ear file that's uploaded to the server?  How can I retrieve my last deployed application?
    Thanks
    Andrew

    Hi,
    The latest deployed project and its corresponding files you can get it from
    C:\Documents and Settings\youraccount\Documents\SAP\workspace\yourproject
    ( in case after few chnges if you say rebuild project older project will be modified with latest changes.My suggestion you can have timely back up in to onther workspace i mean another directory/folder so that you can retrive/import your older projects to NWDS).
    Apart from this i am not finding any other solution
    Hope it helped you
    Reagrds
    RK

  • Deploying the java files from main project

    haii..
    i am using netbeans5.5 ...i started do all my programs in a single project...all these are small small programs using Jframes and all..now i want to run this programs separately from the IDE..like from the cmdline...so i go to tat netbeans installtn folder src and i find my programs there all the files are there in .java and .form etn...and wen i tried to compile the .java alone it gives me lot of errors ....
    what can i do ...plz help me

    If you use NetBeans you can extract the .class files
    and then get source from the .class files.Not necessarily. Presumably, then, Netbeans comes with a decompiler. All well and good, but of course what gets spat out of a decompiler is not necessarily what was compiled in the first place. How much that matters depends very much on why you want to see the source, of course, of course

  • JBuilder question on how to add a new .java file

    I am new to Java and even newer to JBuilder. I am creating the GUI for our school project and my other members are creating the networking and database parts of our program seperatly. I am at the point were I need to import their .java files into my project I created in JBuilder.
    Can someone tell me the steps I need to take to get those files into my project so I can start using the classes from my group they made up?
    Thanks,
    Scott

    You can use show_alert statement and then make a check on button that is pressed from the alert.
    Here is an example of show_alert built_in.
    DECLARE
      my_alert number;
      ALERT_TXT     VARCHAR2(200) := 'Do you want to delete record?';
    BEGIN
         set_alert_property('your_alert',ALERT_MESSAGE_TEXT,ALERT_TXT);
           my_alert := show_alert('your_alert');
           If my_alert = Alert_Button1 Then
             /** I choose to delete record **/
            -- here you can put your statements
           else
           -- I pressed NO button
           -- here you can put your statements
           end if;
    END;Hope it helps you,
    Fabrizio
    If this answer is helpful or correct, please mark it. Thanks.

  • Using .java files in studio creator 2

    I am a C programmer new to Java and Studio Creator 2.
    If I add a blank java file to my project and create a class test1:
    public class test1 {
    public static void main (String[] args) {
    System.out.println("Hello World");
    How do I execute this code from within creator2?
    What I want to do is use creator 2 to write, debug, and run a .java file.

    Never mind, thank you anyway - jcarr

  • How can I share a *.java source file across multiple projects in NetBeans?

    I'm sure this simple and a pretty common operation but how can I share a *.java source file across multiple projects in NetBeans? Right now I keep cut, coping and pasting the same source file between multiple projects to re-use the same code. But I could I make this source file a library file or something like that so that I could access it from any project. I assume this would be a generic operation but I mentioned NetBeans for clarity. Thanks.

    fiebigc wrote:
    I know I mentioned NetBeans but I'm most interested in the generic method for creating a library of source files that I can call into whatever program I am developing. I've done such a thing in C using header files and such but I'm trying to get a direction on how this is accomplished in Java. I'm sorry if I could edit the title I would. If anyone wants to be specific about NetBeans I welcome that too.
    Edited by: fiebigc on Jun 20, 2008 5:57 PM
    >I know I mentioned NetBeans but I'm most interested in the generic method for creating a library of source files that I can call into whatever program I am developing. I've done such a thing in C using header files and such but I'm trying to get a direction on how this is accomplished in Java. I'm sorry if I could edit the title I would. If anyone wants to be specific about NetBeans I welcome that too.
    Edited by: fiebigc on Jun 20, 2008 5:57 PM
    Create a class library.
    Write your code, compile it to .class files, put those class files in a .jar file and include the jar file in the classpath whenever you want to compile a project against it.

  • How to open java files in a java project

    hi ,
    i have to implement a java program analyzer and now i have implement a java parser using javaCC. My parser parse a java file at a time and i have to read and parse all java files in a java project. my idea is to enter
    java project name *.jpx as an input and search java files in project and parse them one by one, or , to enter the folder path of java source files of project and parse them.
    i am a beginer of java language and i don't know where to start to do it.
    is there someone tell me which way is easier and better for my case and some ways to implement it. i search sample files that is similar to my case but i cannot find any.
    thanks in advance.
    ami

    if u don't mind , can u tell me which tutorial i
    should read for that? http://java.sun.com/docs/books/tutorial/essential/io/
    This... for example.
    xH4x0r

  • Cannot compile Java files or create Java Project

    Hi,
    I cannot compile java files and also didn't see any option
    for creating java project using flex 2 builder.
    Actually am trying to create a FDS project with an option to
    compile on the server. I am using Weblogic 8.1 SP5 as my server.
    thank you
    sun

    hi
    this is may jvm.config file. Can anyone tell me if it is
    right because all the fields are blank.
    # VM configuration
    # Where to find JVM, if {java.home}/jre exists then that JVM
    is used
    # if not then it must be the path to the JRE itself
    # If no java.home is specified a VM is located by looking in
    these places in this
    # order:
    # 1) JAVA_HOME environment variables (same rules as java.home
    above)
    # 2) bin directory for java.dll (windows) or
    lib/<ARCH>/libjava.so (unix)
    # 3) ../jre
    # 4) registry (windows only)
    java.home=C:\Adobe\Flex Builder 2.0 Beta 3\Flex SDK 2.0\jre
    # Arguments to VM
    java.args=-ea -Xmx384m
    # Environment variables we care about, whitespace-separated
    env=
    # java.class.path - use this for adding individual jars or
    # directories. When directories are included they will be
    searched
    # for jars and zips and they will be added to the classpath
    (in
    # addition to the directory itself), the jar to be used in
    launching
    # will be appended to this classpath
    java.class.path=C:\Adobe\Flex Builder 2.0 Beta 3\Flex SDK
    2.0\jre
    # where to find shared libraries, again use commas to
    separate entries
    java.library.path=
    thanks
    sun

  • Can I include java files into my javafx project?

    If I do so, exception always occurs until I delete the created java files.

    Suppose I include the following class in my project (all other classes are of .jx type)
    public interface ServicePortType extends java.rmi.Remote{
        public String getMessage() throws java.rmi.RemoteException;
    }However if I'm trying to run the whole project,
    the following exception occurs:
    Cannot run program "C:\Program Files\Java\jre6\bin\javac.exe": CreateProcess error=2, The system cannot find the file specified
    C:\Documents and Settings\daps-stg\My Documents\NetBeansProjects\ProductionPath\nbproject\build-impl.xml:143: exec returned: -1
    BUILD FAILED (total time: 0 seconds)If I cancel the java source from my javafx project, it runs normally without any bug.
    So how is this happening and what is the potential solution?
    Thx. -- Isaac.

  • Can not see the design view nor the java file in CentOS

    Hi all. I've recently installed JSC2 in CentOS. I find this tool really useful. But I have a problem with the design view and the java file view. At the beginning, when I first create a Page1.jsp, I can see the design view and the java view with no problem. I run apps correctly. But after a while, I get a "null pointer exception" error and I am able to see only the jsp view, not the others. I don't even have the option to see the Design and Java views. And the page icon in the project window appears to be darker than a well working page. I'm not really sure what sequence of actions I do to get this error, it just suddenly happens. What I've done so far in JSC2 is to link to pages in the Page Navigation view. It works fine for a while, but after a while I get the error I've described.

    Yes, if I do that It happens the same. I've tried everything. Now I'm using Window$ 2000 and it works properly. Thanks anyway.

  • Using a JAR file as a project file

    My application allows the user to create projects. Previously, a project file was simply an XML file. Now, a project file needs to also contain many images.
    I thought it would be a good idea to simply use a JAR file as my project file.
    Since I've really never really worked with JAR files and the java.util.Jar package directly, I have a few questions.
    1) Would it be better just to simply store the images as binary in the XML file? I just spent a good amount of time making the XML file very readable so I thought it would be better to keep the images separate while still having a single "project" file.
    2) Is there anything inherently wrong with the way I'm working with the JAR file in the code below?
    Simple XML file:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE project [
      <!ELEMENT project (img+)>
      <!ELEMENT img EMPTY>
      <!ATTLIST img src CDATA #REQUIRED>
    ]>
    <project>
         <img src="blah.jpg"/>
    </project>
    import java.io.*;
    import java.util.jar.JarFile;
    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import org.xml.sax.helpers.DefaultHandler;
    public class LoaderTest {
        private String imageName;
        public LoaderTest() {
            InputStream i = null;
            JarFile jf = null;
            try {
                jf = new JarFile("project.jar");
                i = jf.getInputStream(jf.getJarEntry("project.xml"));
                // Here's my simple XML parser to load the project and images
                SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
                parser.parse(new InputSource(i), new DefaultHandler() {
                    @Override
                    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                        // Right now we only care about images
                        if (qName.equalsIgnoreCase("img")) {
                            imageName = attributes.getValue("src");
                // Load the image from the inputstream
                java.awt.image.BufferedImage img = javax.imageio.ImageIO.read(jf.getInputStream(jf.getJarEntry(imageName)));
                // Display the image in a JFrame
                javax.swing.JFrame f = new javax.swing.JFrame();
                f.setLayout(new java.awt.BorderLayout());
                f.setSize(800, 600);
                f.getContentPane().add(new javax.swing.JLabel(new javax.swing.ImageIcon(img)), java.awt.BorderLayout.CENTER);
                f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            } catch (ParserConfigurationException ex) {
                ex.printStackTrace();
            } catch (SAXException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                // Do our best to close the input streams
                if (i != null) {
                    try {
                        i.close();
                    } catch (IOException ex) {}
                if (jf != null) {
                    try {
                        jf.close();
                    } catch (IOException ex) {}
        public static void main(String[] args) {
            new LoaderTest();
    }Thank you!

    1) I also think it's much better to keep your images separate from your XML, this prevents a lot of in case you just want to read XML data
    2) What's exactly going wrong here? For the project.xml, make sure it's not inside a directory within the JAR file, or else the variable i will be null. Otherwise it looks fine..

  • Adding .class files to your project and making them work.

    Ok, im fairly new to java and having trouble understanding how to make importing of .class files work. Im using eclipse 3.2
    I have the files in a directory and I have gone to project->properties->java build path->libraries->add external class folder and pointed it to the correct place.
    Now, I am trying to use some of the classes it provides. I get no errors when creating the object with this code:
    com.ECS.client.jax.ItemSearchRequest itemRequest = new com.ECS.client.jax.ItemSearchRequest();
    Before I did the add external class folder that code would error cause it didnt know what com.ECS was. Now it works fine. My problem is that when I try to use a class, it does not recognize it as a class. After doing the above code I have:
    itemRequest.setSearchIndex("Books");
    its giving me an error on the period, saying: "Syntax error on token(s), misplaced construct(s)". Its not seeing that it is a class, so I can't use it. But when I am creating the object I get no errors. For reference, here is the chunk of code:
    com.ECS.client.jax.ItemSearchRequest itemRequest = new com.ECS.client.jax.ItemSearchRequest();
    itemRequest.setSearchIndex("Books");
    the second line of which errors. So my question is, how do I get it to recognize that the itemRequest is a class when I have imported the .class files to my project already?

    It's working fine. It's just that you have to put your code in a method.

Maybe you are looking for

  • Siri not working in the UK

    I've been using Siri and it works well for most questions, but if I ask it for something about a local business, for example, where is the nearest pizza restaurant, it always replies that Siri is not able to give information for businesses outside th

  • In windows 8 I can't print to an Adobe PDF printer

    Upgraded to windows 8 (forced to) and reloaded Adobe 8.  Even though it created the printer (PDF) it returns a printer error everytime you try to print to it.

  • Adding dynamic size table to Interactive Form

    Hello everyone, I have a node with multiple elements that i would like to show in an interactive pdf form. I'm using the Adobe LiveCycle Designer to do it, but i'm only managing to bind "solo" variables, not tables. How can i associate dynamically a

  • Currupted Workflow Manager Configuration, RE-Installation Guidence?

    Dear All, We have few workflows which are currently running in our SP 2013 server farm. The workflows stopped working for sometime, when we examined the errors, we could see that we are no longer able to open the workflow manager and Service Bus conf

  • E72 CALENDAR ALARM PROBLEM - PLS HELP

    I am using a Nokia E 72. sometimes when I set alarm for anniversary event at 9.00 AM the alarm is saved for 14.30 hrs. If I try to edit the entry to correct the alarm time, then also it is being saved for 14.30hrs. Is it a bug in the SW? Are you also