0

This question has had me working for hours so I would immensely appreciate any help. I need to read in a file through command line argument that contains name, major, ID, and graduation date of multiple students. Then, I need to store this information in an array, and use this array to output just the information of specific students that satisfy a condition (they will graduate in 2017). So essentially I'm outputting a new file that only contains students that match this condition.

I've created this code to read in a file and store them to an array. But from here, I am stumped :(

Please, any help would be valuable.

import java.util.*;
import java.io.*;
import java.io.File;

public class Aid {

    public static void main(String[] args) throws IOException {


        String token1 = "";

        Scanner inFile1 = new Scanner(new File("location of file in my folder")).useDelimiter(",\\s*");


        List<String> temps = new ArrayList<String>();

        while (inFile1.hasNext()) {
           token1 = inFile1.next();
           temps.add(token1);
        }
        inFile1.close();

        String[] tempsArray = temps.toArray(new String[0]);

        for (String s : tempsArray) {
            System.out.println(s);
        }
    }
}

2 Answers 2

1

Are you required to answer it in that form? you should use a split instead of stringtokenzier. To read from a file for simplicities sake,

Create an arrayList of type String so you can resize as needed.

<ArrayList>String info = new <ArrayList>String();

Create a File object. File thisFile = new File(The name of the file being read goes)

Create your scanner. Scanner scanThis = new Scanner(thisFile);

while(scanThis.hasNext())
{
String str = scanThis.next();
info.add(str);
}

Ive only been programming for 2 weeks or so, feel free to correct me if im wrong ill actually go test this myself ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Ok to output to a file you need PrintWriter hold up ill see if i can make a quick file
0

Adding another answer because it might be difficult to read from the comments

import java.io.*;
import java.util.*;
/**
 * Write a description of class Other here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Other 
{
public static void main(String[] args) throws IOException
{
File myFile = new File("Users/josephcrachiolo/Desktop/ReadThis.txt"); //Create a file object and hold the name of our file.
Scanner readThis = new Scanner(myFile); //Scanner object to read from the file
ArrayList<String> temp = new ArrayList<String>(); arraylist for the purpose of resizing array
StringTokenizer stok; //Not needed i believe
PrintWriter output = new PrintWriter("Yalla.txt"); //create a printwriter object so we can write to the specified file "Yalla.txt"
int i = 0; //Used as incrementor

while(readThis.hasNext()) //while the file has something to be read
{
String str = readThis.next(); //Here will read each peice of text and will seperate by spaces
//stok = new StringTokenizer(str) --> possibly not needed
temp.add(str); //add to the arraylist object //add that text into our arraylist
}
while(!temp.isEmpty()) //while our arraylist is not empty execute
{
output.print(temp.get(i)); //print to the output object("Yalla.txt") the first element (0)
i++; //increase the element to get the next.
}

All of this should read from a file, and store it in another file. Couldnt compile because of bad path got a new mac idk how that works yet buut yeah let me know if something goes wrong readThis.close(); output.close(); } }

1 Comment

This is awesome, thank you but I'm getting an error. I'm getting an out of bounds exception. Index:11, Size:11. I am not aware of what this means. Do you know?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.