Read Characters From Txt File / Store Them Into 2D Array And Print Information
Oct 10, 2014
The point of this program is to read characters from a txt file and store them into a 2D array. After this has been accomplished, the information is to be printed in the same manner it is read from in the txt file.
Here is the code I have so far:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws FileNotFoundException
[Code] ....
And this is the error I am receiving when trying to accomplish the goal of the project:
Exception in thread "main" java.lang.NumberFormatException: For input string: "############"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Maze.<init>(Maze.java:15)
at Main.main(Main.java:20)
What's going on here and how I can correct this?? The information I am trying to store in a 2D array and then print is this:
############
#.#........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#
View Replies
ADVERTISEMENT
Dec 3, 2014
The main method will drive your program by doing the following:
-Create an array to hold all the individual golfers and par scores (type is Golfer[ ]).
-Prompt the user for a data file containing the Par scores and the player names and score. The format of the input file should look like
Par, 3, 4, 4, 3, 4, 3, 5, 3, 4
George, 3, 3, 4, 3, 4, 2, 3, 3, 4
Paul, 4, 5, 4, 3, 6, 3, 4, 3, 5
Ringo, 3, 3, 4, 3, 4, 2, 4, 3, 4
John, 4, 4, 4, 3, 4, 2, 5, 3, 4
Your program should read the file contents and store the information in your array of Golfers.
View Replies
View Related
Feb 12, 2014
I've written some code to do this, but it's not working as I expect. This is what the ASCII map looks like:
###################
#.................#
#......G........E.#
#.................#
#..E..............#
#..........G......#
#.................#
#.................#
###################
Here's how I've tried to store the information in a 2D array:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class Map {
public void importMap() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("map.txt"));
String line;
[Code] ....
But it's not printing out what I'm expecting. It's printing out:
[[#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [
What is printed is much larger than what I've copy and pasted but I can't post it all here.
The error I get is:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Map.importMap(Map.java:26)
at Map.main(Map.java:44)
View Replies
View Related
Jul 14, 2014
New to programming. Am supposed to create a program that reads a text file (of integers) and computes a series of computations on these integers. I don't have the code for the integers in my code yet, (i know how to do those), but am struggling getting the array to simply print in the print writer. I have the user select a text file, read the file with a scanner, and then save the computations done from my code into another file. specifically, the problem is as follows: Write a program that uses a file chooser dialog to select a file containing some integers. The file contains an integer N followed by N integers. The program then uses a file chooser dialog to let the user specify the name and location of an output file to write results to.The data written to the output file will be as follows
(1) The original list of N numbers from the input file,
(2) The original list of N numbers printed in reverse order of how they appear
in the input file.
(3) The sum and average of these numbers,
(4) The minimum of all the numbers,
(5) The maximum of all the numbers.
[import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
[Code]....
View Replies
View Related
Feb 13, 2014
I have double checked this code over and over and I just can't find the problem.
What I'm trying to do is take a file and input it into an 2D array.
Ultimately, I should convert the array of integers to an array of characters, then print it out. The file contains a set of ASCII values.
After printing it out, I should then create methods to manipulate the image produced.
Using 2D arrays is a requirement for this exercise.
I think that somehow I'm overcomplicating this and the solution is a lot more simple than I think, but I can't think of what to change.
The error I am getting is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
at main.main(main.java:17)
Java Code:
import java.util.*;
import java.io.*;
public class main {
public static void main(String[] args)
throws FileNotFoundException {
String[][] data = new String[22][40];
[Code] .....
View Replies
View Related
Nov 12, 2014
The requirement asked me to write a Java program to read information from the file, display the original information along with the player's average score of the season (one line for each player), and announce who is the highest average-scoring player of the team. But I always only get the highest which show the last player.
Such like the output showed:
Smith 13.0 19.0 8.0 12.0 Player's average: 13.00
Badgley 5.0 Player's average: 5.00
Burch 15.0 18.0 16.0 Player's average: 16.33
Watson Player's average: 16.33
Knox 10.0 12.0 8.0 6.0 Player's average: 9.00
Casler 15.0 7.0 Player's average: 11.00
Winorburg 13.0 14.0 14.0 23.0 20.0 Player's average: 16.80
Morton 4.0 11.0 13.0 Player's average: 9.33
Seeling 16.0 8.0 9.0 11.0 Player's average: 11.00
Tanshi 5.0 13.0 10.0 Player's average: 9.33
The highest average-scoring player is: Tanshi
import java.util.Scanner;
import java.io.*;
public class PA7{
public static void main(String[] args) throws IOException{
String name = null;
[Code] .......
View Replies
View Related
Sep 25, 2014
I want to read this csv file and store contents on column basis; as date (single array) for all dates. open, high, low and close and be able to manipulate individual elements of the array such as Sum += close[i];
public void pratice() throws Exception {
// Create a File instance
BufferedReader br=new BufferedReader(new FileReader("gtk.csv"));
content = new String[1000];
while(content !=null) {
content[++count] = br.readLine();
[Code] .....
the file looks like this
date Open high low close
12-12-2011 15.00 15.40 14.5 15.2
11-12-2011 15.02 15.70 14.9 15.00
10-12-2011 14.70 16.00 14.70 15.02
...
View Replies
View Related
Dec 2, 2014
I am now stuck. I am writing a program that reads information from a data file and runs it through but program. However, I am almost finished with the program, but cannot figure out the last few parts.
public class Storm {
private final double KnotsToMPH = 1.15;
// global user-defined types:
private int beginDate;
private int duration;
private String name;
[Code] .....
I have not attached the data file because it contains a total of 360 lines of hurricane information.
View Replies
View Related
Apr 5, 2014
I have a college question ask me to write a class StringRevert which does the following:
-prompting user for an interger n
-creating an array of n string
-repeatedly read character string from user input and store them in the array until end of array is reached or user input -quit(quit should not saved in array)
-print the string from array in reverse order, from last enter to first enter.
-assume user always supplie correct input
This is what I've done so far but how to get it working.
import java.util.Scanner;
public class StringRevert {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("enter value of n: ");
int n1 = in.nextInt();
String[] n = new String[n1];
[Code] ....
View Replies
View Related
Apr 15, 2014
Write a Java program that asks the user to store 10 numbers in an array. You should then print the array in the order entered and then print in reverse order. However, instead of putting all the information in the main, you should use a class called PrintIt. The PrintIt class should have an instance variable that is an array to hold the numbers. You should have a method that will print the array in the order entered. You will also need a method to print in reverse oder. Then create a tester class that asks the user to enter 10 numbers and puts them in an array.
So far I've got this.
public class printIt {
int i;
int [] numArr = new int [10];
public printIt () {
i = -1;
[Code] .....
Output:
Enter a number:
8
34
Forward:
8
34
Reverse:
34
8
end
what i want as an number is being able to print out 10 numbers but this only lets me do two numbers. Why is that so?
View Replies
View Related
Sep 22, 2014
Designed to store a first name (string), last name (string), and zip (int). Assume each line will contain two strings followed by integer each separated by tab. Then print.
I have my two class files (one for the individual and one for the array) and then my driver below. I think my only problem is my driver. I think I'm reading the data file wrong but not sure how exactly.
test in .txt file:
JohnSmith12345
JohnDoe12346
SueSmith09877
VeronicaVarguez67890
MuhammadMaliki54321
public class Person
{
private String firstName;
private String lastName;
private int zipCode;
public Person(String fName, String lName, int zCode)
[Code] ....
View Replies
View Related
Jul 7, 2014
I'm doing desktop application about recipes. User can create new recipe, add information. I want to sort recipes, save them somewhere, maybe make it possible to send recipes over network to other users. But I don't know where and how store information about those recipes. I always used to use .txt files for storing needed information, but it seems that it is not the best option.
View Replies
View Related
Nov 11, 2014
i have a program that is to store DVD information and when i enter all the information asked it returns no info. I have a DVD inventory file and a DVD extended file.
DVDinventoryprogram class
package inventory2;
import java.util.Scanner;
[Code]....
View Replies
View Related
Dec 19, 2014
I have to write a program that will read a picture and then print out the number of blocks inside it.
I have to read the picture as a binary matrix of the size r - c (number of rows times number of columns). The blocks are groups of one or more adjacent elements with the value 1.
- Blocks are built exclusively of elements with value 1
- Each element with value 1 is a part of some block
- Adjacent elements with value 1 belong to the same molecule.
We only take into account the horizontal and vertical adjacency but not diagonal.
INPUT:
In the first line of the input we have the integers r and c, separated with one space.
Then we have the r lines, where each contains s 0's and 1's.
The numbers inside the individual lines are NOT separated by spaces.
The OUTPUT only print the number of blocks in the picture.
Example:
INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100
OUTPUT:
6
THIS IS WHAT I CAME UP SO FAR:
import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char ch[][];
int rowNum=sc.nextInt();
int columnNum=sc.nextInt();
[Code] ....
View Replies
View Related
Apr 4, 2014
I am searching a XMI parser in java code that read an xmi file and print classes with related attributes.
View Replies
View Related
Sep 25, 2014
I have a file called statecapitals.txt that is read in, I want to store it in either a 2d array or hashmap and select a random state then Ask the user for the name of the capital. Then I want to Let them know if they are correct or not and have a choice to play as many times as they like. When they no longer want to play,I want to let them know how many they got correct and how many incorrect. I am not sure which would be better a hash map or 2d array and dont know where to start with each.
here is what the text file looks like:
Alabama - Montgomery
Alaska - Juneau
Arizona - Phoenix
Arkansas - Little Rock
California - Sacramento
Colorado - Denver
[code]....
View Replies
View Related
Nov 5, 2014
I'm trying to write a program to read the names off of email addresses in a text file and my code is not printing anything to the console. So I want to print everything before the "@" sign. It seems like I'm missing a big thing
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Email {
public static void main(String[] args) throws FileNotFoundException, IOException {
[Code] ....
View Replies
View Related
Feb 11, 2014
So I am trying to open a file and store the contents into a two dimensional array. I seem to have no problem using this same basic code to read the file and output it. But when I try to change it so that all the data is stored into the array, it does not work.
Java Code:
public void convertFile()
{
String filePath = ("C:UsersBradDownloadsFB1.csv");
String [][] rowCol = new String [429][6];
try
{
BufferedReader br = new BufferedReader(new FileReader(filePath));
StringTokenizer st = null;
System.out.println("Your file is being converted to an array. This may take several minutes.");
[code]....
View Replies
View Related
Mar 17, 2015
public class InputFileData {
/**
* @param inputFile a file giving the data for an electronic
* equipment supplier’s product range
* @return an array of product details
* @throws IOException
*/
public static Product [] readProductDataFile(File inputFile)
throws IOException{
// YOUR CODE HERE
}
This code is meant to be used to read a text file and store the data in an array of type Product[]. I know how to read in a text file and have it sort it into an array, but I've never seen code laid out in this fashion before (specifically "public static Product[]", and I'm unsure how to work with "(File inputfile)". I've looked all over the place but can't find any examples of anything like this.
Also, the code given cannot be changed, as it's that code I have to work with.
I still don't understand how to use it as a whole. For example, do I read the file in the main and have this method read that in and output to the Product class? Do I read the file in this method? I can't work out how to make this work when I have to use this method.
View Replies
View Related
May 8, 2014
How i can get the data from a file, for a key that have multiple values without using split method and i want to store that data in an array
file name:"new.txt"[contains below data]
Name=google.com, yahoo.com,facebook.com
Attachment=abc1,abc2,abc3
I am successfully able to do this using following code
Properties props = new Properties();
String configFilePath = (Cjava_confignew.txt);
props.load(new FileReader(configFilePath));
String TestName = props.getProperty(Name);
String test[] = TestName.split(,);
Now i just want to store the values for key "Name" in array without using TestName.split(,); but i am not able to do this.
I have also used .yml file to avoid this but did not succeed, some way to do this
file name test.yml
Name
-google.com
-yahoo.com
-facebook.com
Attachment
-abc1
-abc2
-abc3
i want to store multiple values for single key(Name) within the array
View Replies
View Related
May 12, 2015
Write a java program to store employee information in a linked list, the program should implement the following functions:
The program should display the list of functions, and ask the user to enter the number of function that he/she wants to do, then perform the function as it is required in the previous table.
import java.util.*;
public class Employee {
Scanner in = new Scanner(System.in);
String Name;
String Address;
String Department;
int ID;
int Salary;
[code]....
this is my out put
Please choose a number:
1-Add a new employee
2-Update employee's info
3-Remove employee's info
4-Exit
1
Enter name:
Enter address:
2
Enter department:
3
Enter ID:
4
Enter salary:
now:
1- why are not my adding coming out in the output only the Enter name & Enter address ??
2- how can I add names and ID's and information to test that program
View Replies
View Related
Dec 8, 2014
I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...
The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data.
Here is the base Product class that must be used to create the objects for the array.
public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;
[Code] .....
And then here is the data from the text file that i must extract to use to create product objects.
Dill Seed,938,34
Mustard Seed,100,64
Coriander Powder,924,18
Turmeric,836,80
Cinnamon (Ground Korintje),951,10
Cinnamon (Ground) Xtra Hi Oil (2x),614,31
Cinnamon (Ground) High Oil (1X),682,19
These continue for about 40-50 entries, they are not separated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name separated with spaces, then price after a comma, then quantity after the second comma.....
View Replies
View Related
Mar 21, 2015
I am trying to read a file and input each line of the file into an array. I have not set the array size as I was hoping to fill the array using a while loop. Unfortunately the scope of the array does to work inside the while loop so I am being told that the array 'students' has not been initialised even though it has just outside of the while loop. Is it possible to do what I am trying to without having the array initialised in the while loop as surely the array will be reset every time if it was in the while loop? Here is my code:
public static void students(String file) throws FileNotFoundException {
try {
File studentInfo = new File(file);
Scanner input = new Scanner(studentInfo);
String[] students;
[Code] ....
View Replies
View Related
Jul 13, 2014
I'm new to java and I'm trying to figure out how to read a file like this:
Jan26081.5910211.79
Feb23301.5918221.79
Mar24261.5922101.79
Apr23751.6921431.79
May24001.6915381.79
And output the file and print it into an array with total under it.
View Replies
View Related
Nov 17, 2014
I have to write a program for sorting an array of random numbers by the users choice. These random numbers are stored in a text file, which the user inputs, and is then stored into an array for sorting. I won't have a problem with the sorting algorithms, but I've never had to read a text file and store it into an array before
The text file has the numbers stored like so :
148
626
817
4
312
652
643
etc....
I gather that I'll probably have to user the Scanner for the user to input the text file name, but how do I store it to an array? So far I only have the bones of the program done, like so
import java.util.Scanner;
public class SortingAlgorithms {
public static void main(String[] args) {
[Code].....
View Replies
View Related
Oct 25, 2014
I am trying to read in lines of text from a file then prints out the text. I only need one 2D array and can't copy from one array to another.This how the file looks like:
4 4
FILE
WITH
SOME
INFO
I have been able to read the file but when I am trying to run my program I have blank spacing instead of an array.
import java.util.Scanner;
import java.io.*;
public class Array {
public static void main(String[] args) throws IOException{
[code]....
View Replies
View Related