Share on Google+Share on Google+

Raghav Purankar
how to print the content of file in 2d matrix having same dimension as given in file(n*m).
1 Answer(s)      5 years and 11 months ago
Posted in : Java Beginners

here is code:

import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class mat1 { public static void main(String[] args) throws IOException { Scanner s = new Scanner(new File("rfg.txt")); List list = new ArrayList(); while (s.hasNextLine()) { String line[] = s.nextLine().split(" ");

     list.add(new double[] { Double.parseDouble(line[0]),Double.parseDouble(line[1]),Double.parseDouble(line[2]),Double.parseDouble(line[3]) });
}
    int numberOfRows = list.size();
    int numberOfColumns =4; 
    double[][] floatValues = new double[numberOfRows][numberOfColumns];
    for (int i = 0; i < numberOfRows; i++) {
        for(int j=0;j<numberOfColumns;j++){
        floatValues[i] = list.get(i);
        System.out.print(floatValues[i][j] + " "/* + floatValues[i][1] + " " + floatValues[i][2]*/);

        }
        System.out.println();
    }

}}

here is .txt file:

1.0 3.1 2.1 3.4 2.4 2.0 4.6 5.1 0.2 3.3 4.7 9.1 4.0 5.4 5.1 3.2 6.1 2.1 6.4 2.6

here in program i have to specify no. of columns every time. it is not the case. but i want to print the file of any dimensions in the form of 2d n*m matrix. pls help me

Ads

View Answers

May 2, 2011 at 3:01 PM


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class mat1 {
    public static void main(String[] args) throws IOException {
    Scanner s = new Scanner(new File("rfg.txt"));

        List<double[]> list = new ArrayList<double[]>();

        while (s.hasNextLine()) {
            String[] line = s.nextLine().split(" ");
            list.add(new double[] { Double.parseDouble(line[0]), Double.parseDouble(line[1]), Double.parseDouble(line[2]), Double.parseDouble(line[3]) });
        }

        int numberOfRows = list.size();
        int numberOfColumns = 4;
        double[][] doubleValues = new double[numberOfRows][numberOfColumns];
        for (int i=0 ; i < doubleValues.length ; i++)
        {    doubleValues[i] = list.get(i);  
            System.out.println();
            for  (int j=0 ; j < doubleValues[i].length ; j++){
                System.out.print(doubleValues[i][j]+" ");
                  }
        }
    }
}









Related Tutorials/Questions & Answers:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads