logo
down
shadow

Unable to read and sort information from a text file in Java


Unable to read and sort information from a text file in Java

Content Index :

Unable to read and sort information from a text file in Java
Tag : java , By : Cowtung
Date : November 28 2020, 09:01 AM

it should still fix some issue I'm attempting to read a text file using BufferedReader and sort the information within the file. The purpose is to load and be able to access information on maps for a game I'm making. The problem I have is that when my program tried to sort the information I get a NullPointerException. , you need to initialize your variable Map
Map = new Map[5]; //array of fixed size 5

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Eclipse, Unable to Read Text File in Java Class


Tag : java , By : s8k
Date : March 29 2020, 07:55 AM
should help you out I know that some people have already encountered this problem, but I am still unable to solve it.
public static void main(String[] args) {

        BufferedReader br = null;
        try {
            String sCurrentLine;
//          br = new BufferedReader(new FileReader("D:\\test.txt")); //Absolute File
            br = new BufferedReader(new FileReader("test.txt"));//Relative File
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
 MyProject
 --src
    --Main
 --test.txt

How to read efficiently a huge text file in java and split its content to sort it?


Tag : java , By : user177910
Date : March 29 2020, 07:55 AM
wish of those help LinkedList is not particularly efficient in terms of memory and no good for the built in sort algorythims. I suggest you load each line into an Array List and split it only by the name (not each field as you don't need to break those up)
You can sort an ArrayList with Collections.sort and a custom comparator.

Unable to read simple text file Java


Tag : java , By : Sinisa Ruzin
Date : March 29 2020, 07:55 AM
hope this fix your issue Well, there are two types of exception - unchecked and checked. checked are the ones that are checked at compile time. So, when compiler says "10: error: unreported expection FileNotFoundExcpetion; must be caught or declared to be thrown" It means line inFile = new Scanner(new FileReader("input.txt")); throws checked exception which means that this method has an underlying risk that it can throw a FileNotFoundExceptionand hence you should handle it. So, wrap your code in try/catch block -
/** Import util for console input **/
import java.util.*;
import java.io.*;
/** Standard Public Declarations **/
public class costing
{
 public static void main(String[] args)
 {

  Scanner inFile;
  try {
     inFile = new Scanner(new FileReader("Coursework/input.txt"));
     String name = inFile.next();
     System.out.println(name);
} catch (FileNotFoundException ex) {
    ex.printStackTrace();
}
}
}

How to read a portion of a text file to extract information to be used to create an object in Java


Tag : java , By : ChrisMe
Date : March 29 2020, 07:55 AM
I hope this helps you . So I've got a text file:
public class ThisClass {
private static final URL path =      ThisClass.class.getResource("TextFile.info");
private static final String TEXTFILE = path.getFile();

private String name;
private int loadingBays;
private int containerHandlingTime;
private int storageCapacity;

public ThisClass(String name, int loadingBays, int containerHandlingTime,
                 int storageCapacity) {
    this.name = name;
    this.loadingBays = loadingBays;
    this.containerHandlingTime = containerHandlingTime;
    this.storageCapacity = storageCapacity;


}

public static void main(String[] args) throws FileNotFoundException, IOException {
    BufferedReader br = null;
    FileReader fr = null;

    String sCurrentLine;

    br = new BufferedReader(new FileReader(TEXTFILE));

    String name = "";
    int loading = 0;
    int time = 0;
    int capacity = 0;

    int count = 1;

    int elemCount = 0;

    ThisClass[] classArray = new ThisClass[2];

    while ((sCurrentLine = br.readLine()) != null) {

        System.out.println("sCurrentLine = " + sCurrentLine);
        if (sCurrentLine.toLowerCase().startsWith("name")) {
            name = sCurrentLine.split(":")[1].trim();
        } else if (sCurrentLine.toLowerCase().startsWith("loading")) {
            loading = Integer.parseInt(sCurrentLine.split(":")[1].trim());
        } else if (sCurrentLine.toLowerCase().startsWith("container")) {
            time = Integer.parseInt(sCurrentLine.split(":")[1].trim().replace("ms", ""));
        } else if (sCurrentLine.toLowerCase().startsWith("storage")) {
            capacity = Integer.parseInt(sCurrentLine.split(":")[1].trim());
        }
        if (count % 4 == 0) {
            classArray[elemCount] = new ThisClass(name, loading, time, capacity);
            elemCount += 1;

            System.out.println("name = " + name);
            System.out.println("loading = " + loading);
            System.out.println("time = " + time);
            System.out.println("capacity = " + capacity);
        }


        if (!sCurrentLine.trim().equalsIgnoreCase(""))
            count += 1;
    }

    for (ThisClass cl : classArray) {
        System.out.println("cl = " + cl);
    }
}

@Override
public String toString() {
    return "name: " + this.name
            + "loadingBays: " + this.loadingBays
            + "handlingTime: " + this.containerHandlingTime
            + "capacity: " + this.storageCapacity;
}

Unable to read text file in Java using FileReader and BufferedReader


Tag : java , By : Juan Pablo
Date : March 29 2020, 07:55 AM
I wish did fix the issue. To confirm that you are having a class with the name FileReader, just use the full class name in the code :
java.io.Reader fr = new java.io.FileReader("testfile.txt");
java.io.BufferedReader br =  new java.io.BufferedReader(fr);
import java.io.*

...

Reader fr = new java.io.FileReader("testfile.txt");
BufferedReader br =  new BufferedReader(fr);
System.out.println(FileReader.class.getPackage());
A
A.Run
A.Test
B
B.Test
System.out.println(Test.class.getPackage());
Related Posts Related QUESTIONS :
  • Launching two spring boot apps in integration test
  • Is there a way to add a HashMap's value that is a integer array into a ArrayList?
  • Is there any way that I can get a parameter in paintComponent?
  • Empty stack with one recursive method and one iterative method
  • What's the behavior of onBackpressureBuffer in RxJava2
  • Java regex can only use 1 quantifier in a lookback (need 2)
  • How to fix error in native query : it is showing syntax error near or at
  • How to retrieve nested object from a document and display it in FirestoreRecyclerOptions?
  • Why not use ListIterator for full LinkedList Operation?
  • Android Webview EvaluateJavascript sometimes does not return a response
  • Matcher java doesn't work but regex seems to be good
  • Finding dimensions of a .gif file
  • Java Number format how to change +/- sign to custom text
  • Entity partially saved when using JOINED inheritance strategy and setting spring.jpa.properties.hibernate.jdbc.batch_siz
  • Stored Procedure in Java Spring Boot Project returns null as Output
  • How to solve org.hibernate.MappingException which is causing due to inheriting a class
  • Clean Archtecture. Understanding of scheme
  • Processing 3 triangle not showing in Javafx 8 Window tab
  • How to specify a sequence-based generated value in Hibernate 5 via legacy mapping
  • Spring-boot application not getting auto-deployed on startup
  • How to only pass strings that the user select
  • Is there a way to SELECT using "GREATEST(field1, field2)" where field1 and field2 are aggregate sums in the sa
  • How to handle JSON objects wrapped into one JSON object with retrofit2?
  • Configure Hazelcast CPSubsystem Retries Timeout
  • how to use onBindViewHolder with multiple items in android RecyclerView
  • No ParameterResolver registered for parameter in BeforeAll method
  • Finding the path in a graph with the least casualties according to the lanchester square law
  • MongoWriteException when inserting into Mongodb with composite custom _id
  • Fetch Oracle procedure metadata with Java when multiple procedure signatures
  • Value modification of key-pair in HashMap and impact for a HashCode
  • Migration from solrj to spring-data-solr
  • How to check if you're still connected to the database with jpa
  • Use Date type in the graphql scheme
  • Split and add the string based on length
  • Is "main" method of spring boot application required when deploy as war
  • Getting the average within specific numbers in an array
  • how to use izpack to make my jar application to installer?
  • What is meant by src in Java Eclipse?
  • Create a mirrored linked list in Java
  • Examples of good JPA Java Desktop Application
  • Translate Java to Python -- signing strings with PEM certificate files
  • Algorithm Analysis tool for java
  • Java serial comm API - what does inputstream.read() return if a timeout occurs?
  • How do I make a background thread in Java that allows the main application to exit completely? This works in Linux, but
  • How to add an image dynamically at runtime in java
  • Java App on Mac asking for allow network connections everytime
  • Best actively maintained Java XMPP Library?
  • Multi-Threaded Application - Help with some pseudo code!
  • Scoping a StringBuilder inside a for loop
  • How to specify hash algorithm when updating LDAP via Java?
  • Class not found exception (org.apache.openjpa.enhance.PersistenceCapable) thrown in a client of WLS 10
  • In Java ,where in memory are class functions put?
  • How do I test expectedExceptionsMessageRegExp (exception message) using TestNG?
  • Help In understanding Multi Dimentional Arrays
  • No bean named '...' is defined and Spring @Resource annotation
  • Singleton design pattern vs Singleton beans in Spring container
  • flashvars object was not working in mozilla browser
  • Shell script to import mysql dump file
  • What are the best practices to separate data from users
  • May I use com.google.code prefix for my packages?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com