Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Java read file and store text in an array

Java read file and store text in an array

August 19, 2021 by James Palmer

Stored as strings:
public class ReadTemps {

public static void main(String[] args) throws IOException {
// TODO code application logic here

// // read KeyWestTemp.txt

// create token1
String token1 = “”;

// for-each loop for calculating heat index of May – October

// create Scanner inFile1
Scanner inFile1 = new Scanner(new File(“KeyWestTemp.txt”)).useDelimiter(“,\s*”);

// Original answer used LinkedList, but probably preferable to use ArrayList in most cases
// List temps = new LinkedList();
List temps = new ArrayList();

// while loop
while (inFile1.hasNext()) {
// find next line
token1 = inFile1.next();
temps.add(token1);
}
inFile1.close();

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

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

For floats:
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class ReadTemps {

public static void main(String[] args) throws IOException {
// TODO code application logic here

// // read KeyWestTemp.txt

// create token1

// for-each loop for calculating heat index of May – October

// create Scanner inFile1
Scanner inFile1 = new Scanner(new File(“KeyWestTemp.txt”)).useDelimiter(“,\s*”);

// Original answer used LinkedList, but probably preferable to use ArrayList in most cases
// List temps = new LinkedList();
List temps = new ArrayList();

// while loop
while (inFile1.hasNext()) {
// find next line
float token1 = inFile1.nextFloat();
temps.add(token1);
}
inFile1.close();

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

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

If you don’t know the number of lines in your file, you don’t have a size with which to init an array. In this case, it makes more sense to use a List :
List tokens = new ArrayList();
while (inFile1.hasNext()) {
tokens.add(inFile1.nextLine());
}

After that, if you need to, you can copy to an array :
String[] tokenArray = tokens.toArray(new String[0]);

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in