Producing Image By Reading From A Data File Filled With Digits
Nov 16, 2014
I'm a complete beginner in Java programming and I'm interested to learn more about its concepts.
Recently, I've been given an exercise which instructs me to display two versions of a picture. The picture to be displayed is provided in the form of a data file of 40,000 digits that are arranged in rows (although there is no marker between rows) and it starts from the top of the picture. So the first digit represents the top left corner of the picture and the last is the bottom right.
Basically, what the exercise wants me to construct a program that plots a dot in one of two colours for each digit. If the digit is in the range 0 to 3 the output should be one colour and for digits in the range 4 to 9 the dot should be in the other colour.
I understand I have to use arrays and also loops to perform this. I'm familiar with the fillEllipse, drawEllipse, drawRectangle and fillRectangle but this exercise is nothing I've attempted before.
I am creating a program where it reads the data inside a file and then places this data into arrays. The file I created has numbers 1-30 in it, file named, testing1.txt .
I can read the data to a monitor perfectly. But, I'm having problem reading data from an external file into an array of class objects. Here's my attempt at writing the method:
private void openFile() //This method asks the user to enter a file name(including the file extension) and then //Sets the data to an array of Product type { String fileName, storeName="", emptyLine1="", emptyLine2="", name="", productName=""; int demandRate=0; double setupCost=0.0, unitCost=0.0, inventoryCost=0, sellingPrice=0; Scanner inputStream = null;
I have to read data from a text file and print it in a new text file. An example of one line is like this: Johnson 85 98 75 89 82
I then have to take the average of all the numbers and assign a "grade" to the numbers for each line of the text file and make a new file so it looks like this for 10 lines:
Name 1 2 3 4 5 Average Grade
Johnson 85 98 75 89 82 85.80 B
My problem is extracting the data from the file so I can use it.
We have an autosys job running in our production on daily basis. It calls a shell script which in turn calls a java servlet. This servlet reads these files and inserts the data into two different tables and then does some processing. Java version is 1.6 & application server is WAS7 and database is oracel-11g.
We get several issues with this process like it takes time, goes out of memory etc etc. Below are the details of the way we have coded this process.
1. When we read the file using BufferedReader, do we really get a lot of strings created in the memory as returned by readLine() method of BufferedReader? These files contain 4-5Lacs of line. All the records are separated by newline character. Is there a better way to read files in java to achieve efficiency? I couldnt find any provided the fact that all the record lines in the file are of variable length.
2. When we insert the data then we are doing a batch process with statement/prepared statement. We are making one batch containing all the records of the file. Does it really matter to break the batch size to have better performance?
3. If the tables has no indexes defined nor any other constraints and all the columns are VARCHAR type, then which operation will be faster:- inserting a new row or updating an existing row based upon some matching condition?
I'm having a bit of trouble with using the Scanner and the Printwriter. I start with a file like this (1 = amount of Houses in the file)
1 FOR SALE: Emmalaan 23 3051JC Rotterdam 7 rooms buyprice 300000 energylevel C
The user gets (let's say for simplicity) 3 options:
1. Add a House to the file, 2. Get all Houses which fullfil requirements (price, FOR SALE / SOLD etc.) and 3. Close the application.
This is how I start:
Scanner sc = new Scanner (System.in); while (!endLoop) { System.out.println("Make a choice); System.out.println("1) Add House"); System.out.println("2) Show Houses"); System.out.println("3) Exit"); int choice = sc.nextInt();
Then I have a switch for all of the three cases. I keep the scanner open, so Java can get the user input (house = for sale or sold, price = ... etc). If the user chose option 1, and all information needed is inputted and scanned, the House will be written to the file (which looks like what I typed above).
For this, I use try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))). This works perfectly (at least so it seems.)
If the user chose option 1, and all requirements are inputted and scanned, the Houses will be read (scanner) from the file and outputted. For this I use the same Scanner sc. This also works perfectly (so it seems atleast).
My problem is as follows: If a House has been added, I can only read the House(s) which were already in the file. Let's say I have added 2 houses, and there were from the start 3 houses. If option 2 is chosen, the first 3 houses will be scanned perfectly. An exception will be caught for the remaining 2 (just added) Houses. How can I solve this? I tried to close the Scanner, and reopening it, but apparently Java doesn't agree with this
Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.
import javax.swing.JOptionPane; public class DigitsAndSum { public static void main (String[] args) { String str1; String str2; int int1 = 0; int int2 = 0;
I am trying to crate an interface for the following file (code). The interface that I am trying to create will display a open dialog box where the user chooses which image (.dcm) it would like to open and then it should open it in a frame. The code below shows opening the image. The image opens when the image is in the same folder as the source code but I need it to open from a given path and I am really confused on how to do that. I have tried a lot of methods but still doesn't seem work. How to make it work.
This is the code (assuming the parameter that you are passing in image.dcm, I have tried a full path name on mac computer which is like /Users/Documents/image.dcm, where this path name never works):
/** * This class displays the first frame of a file from any format * supported by ImageIO. * In the case of DICOM files, stored values are displayed as * is, without using Value of Interest or Presentation LUTs. */ class Read1 { public static void main(String[] s) {
I'm trying to read images and draw them on JLabel. This code snippet below is reading images from a folder
private ArrayList<ShowItem> getData() { int score = 0; ArrayList<ShowItem> item = new ArrayList<ShowItem>(); File dir = new File(getImageFolderPath()); if (dir.isDirectory()) { for (final File f : dir.listFiles(IMAGE_FILTER)) { BufferedImage img = null; try {
[Code]...
ShowItem is a class that extends Image. DisplayImage extends ShowItem.
One of the overridden methods in ShowItem is
@Override public Graphics getGraphics() { return null; }
I'm not exactly sure what to do here so I left it.
In another class, where I draw the images on Jlabel. This is what I have:
I tried to run it, and check if I'm actually getting the images, and did system out and it listed all the jpg images I have but it's not displaying on the panel.
Code isn't giving me the output that I want and keeps giving me NaN
What I want -
Enter the A value for the line: 2.45 Enter the B value for the line: 4 Enter the C value for the line: -8 Enter the x coordinate of the point: 2.17 Enter the y coordinate of the point: -4
Distance from the point to the line is: 3.9831092774319026
What I am getting
Distance from the point to the line is: NaN
Here is my code-
Tester-
import java.io.*; import java.util.*; class tester { public static double A; public static double B; public static double C; public static double distance; public static void main(String args[])
I have two programs that talk to each other over a TCP connection. When I write the data "STX+1234" where STX is the Ascii character STX or Ctrl B and I expect the written String length to be 6 which it is. On the other side of the socket I create the ServerSocket and use the client socket's InputStream to create a BufferedReader. When I receive the string it now has 12 characters where each original character has been replaced by NUL and the character. How do I read the string as it is originally specified without the conversion? And is the problem on the reader side or the writer side?
Am new to programming and am trying to create a system that is similar to a train timetable system.I want the user to be able to enter the stops between two stations ( from a preset list of 5 stations ) and this be saved as an array list, and this array list be saved accordingly within a 2D array so if they were to look in [1][2] there would be an arraylist of stops between station 1 and 2.I used this to initialise the array of arraylists (in a class i created called stops)
public void stops(){ for (int i = 0; i<5; i++){ for (int j=0; j<5; j++){ stopsArray[i][j]= new ArrayList<String>(); } } }
Then I use this code to set the stops
public void setNoStops(int stopsNo){ noOfStops = stopsNo; }
[code]...
The bottom written line of code is mentioned in the null pointer error I recieve. Then.Then in my main programme I declare the new object and use these methods the method options displays the list of five stations with a number reference
System.out.println("Please choose a start station :"); options(); place1 = scanner.nextInt(); System.out.println();
System.out.println("Please choose an end station :"); options(); place2 = scanner.nextInt(); System.out.println();
[code]...
mystops is the object I created to store the information.
when compiling I recieve no errors.when I run the program it runs smoothly, asks for the number of stops, allows the user to enter a number, then says enter stop 1, I enter a word and the program crashes giving the error java.lang.NullPointerException
How can I extract data from two different files but produce one output. For example, the first three columns are from text_file_1 and the last column (the last foruth column of the output) is from text_file_2...
Im building a program that tracks overtime for employees. Basically there will be a list of employees. Number will be dynamic as different departments will have different amounts of employees.A user could pick the department from a JCombobox. E.g Quality Control. A Jtable will be populated with the employees from Quality Control.
The problem is this.
The way it will work is the list will be in a specific order. Whoever is at the top of the list will be asked if they want to do overtime. Then they will be moved to the back of the queue. If they do the work or not doesnt matter.How would i go about planning a system like this? Specifically creating a dynamic list that people can be moved up and down and stored persistently. URL...
Ok so I know how to import a csv using java. What I'm curious about doing is importing a csv using file chooser, reading the data, sorting the data out by certain parameters, and then outputting a count of each parameter I chose. Say for example I have columns 1,2 and 3. Column 1 has the name, column 2 has the percentage, column 3 has an o'clock time. I want to use queries to sort through the parameters and have a count of each parameter I choose...
A method in Java returns a schema. In .net, we use DataTable to get the data from the schema. How to write the following code in Java:
/* Result r = method(); r.DataSchema; */ using (DataTable tbl = new DataTable()) { using (MemoryStream ms = new MemoryStream()) using (StreamWriter sr = new StreamWriter(ms))
I am reading Excel data using java apache. I got format issue while reading double value such as 869.87929 (in excel) into 869.8792899999999 (in java).
if(type.equals("Double")){ String str = content[i-1]; //System.out.println(str); BigDecimal d = new BigDecimal(str); listObjects.add(d); }
Note: type from schema.csv & content [] value from file.xls If I print **str**, it shows value as 869.8792899999999. But i need to get **str** value as 869.87929. How can I get it?
I have a Char stack: ArrayStack<Character> stek=new ArrayStack<>();
The stack is filled with numbers and characters (only '+' and '*' ), but I need to make calculations, so I need the INT values of the numbers i pop from the stack. So each time I need to make a calculation, I have to pop two numbers, convert them into INT ,then add/multiply them, and put them back into the stack again, but as CHAR (because the stack is not accepting them to be added as INT)
int a= Character.getNumericValue(stek.pop()); //stack no1 int b= Character.getNumericValue(rezultat.pop()); //stack no2 int tmp=a*b; char tmp2 = (char) tmp; stek.push(tmp2);
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]...
these continue for about 40-50 entries, they are not seperated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name seperated with spaces, then price after a comma, then quanity after the second comma.....
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; //Constructor public Product( String pName, double price, 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 quanity after the second comma.....