/home/karlrees/public_html/gallery2/bla
/home/karlrees/public_html/gallery2/bla
/home/karlrees/public_html/gallery2/bla Java lesson 2c | Wayne and Rebecca Madsen

Java lesson 2c

wayne's picture

I resumed training in java this week, requesting that Rebecca teach me some things about reading and writing to external files. Now that I have a rough beginning in programming java, I have decided that I want to learn how to access database files and mine the information. So after I have the groundwork laid for understanding IO interfaces, I will soon learn about algorithms.

I keep calling java "javascript." Rebecca explained that javascript is for web pages; she doesn't know javascript. I think it is a remnant of calling the code for flash design "actionscripting." This problem, along with many other communication problems, reveals that my language isn't entrenched into the world of java. Let's review the process of what I learned this past week.

Initially, I explained to Rebecca that I wanted to learn how to create, access, write and read to external files. Eventually, I would like to learn some good data mining skills, but for now I need to get the basics down. She then referred me to several sites which went over some of the basic principles of the IO interface java uses. What does IO interface mean? I have no idea. Possible I I[n] - O[ut], but your guess is as good as mine. Rebecca is pressuring me to get used to the "Java API," which is the online database of java commands and an explanation of what each class/method does; however, since it reads worse than stereo instructions, I haven't been able to figure out how to make sense of what the Java API says. I have come to the understanding that the Java API is a real semiotic threshold for making sense of the community of programmers who use java.

This first group of code follows very accurately to the examples given to me. Mostly, I copied the code directly from the websites and then posthumously figuring out what the code had done. Rebecca has convinced me (another threshold, in my opinion) that much of learning how to use code revolves around the copying of other code posted on the internet and then deciphering the language meaning afterwards. This teaching method, not through understanding first and then correct usage afterwards but through mimicking, creates an interesting parallel for language development in children: adoption and then comprehension.

package fileReader

public class MainCode {
public static void main (String[] args) throws IOException {
File file = new File ("myData.txt");
Scanner scan = new Scanner (file);
int num, square;

while (scan.hasNextInt()){
num = scan.nextInt();
square = num * num;
System.out.println("The square of " + num + " is equal to "+ square);
}
}

}

The previous block of code accesses the external file, myData.txt, which had been created on my computer prior to running the code. This code would not create the file, to my knowledge, but only read it for integers. Many of the features used in this block of code I had already used in my earlier lessons, such as printing to the console and creating a scanner. In fact, the only two new symbols of language I "learned" from java in this block was the class object File and the parsing method hasNext, both of which are immeasurably important in reading information. I had to verify with Rebecca if I understood correctly about File objects and hasNext methods. I'm still a bit hazy on when something is an object and when I am calling a method.

The following block of code is a simple file writing program which introduced me to the printStream class.

package fileWriter;

public class MainCodeWrite {
public static void main (String[] args) throws IOException{
File file = new File ("myData.txt");
PrintStream print = new PrintStream (file);

Scanner user = new Scanner(System.in);
System.out.print("Enter your text here: ");
String finaltext;
finaltext = user.nextLine().trim();
print.println (finaltext);

}

}

package filereader;

import java.util.*;
import java.io.*;
import java.text.*;
import java.lang.*;

public class MainCode {
public static void main (String[] args) throws IOException {
int num, square;
Scanner user = new Scanner (System.in);
String fileName;
System.out.print("Please type file name to access: ");
fileName = user.nextLine().trim() + ".txt";
File file = new File(fileName);
Scanner scan = new Scanner (file);

while (scan.hasNext()){
if (scan.hasNextInt()){
num = scan.nextInt();
square = num * num;
System.out.println("The square of " + num + " is " + square);
}
else{
scan.next();
}
}
}
}

package readWrite;

import java.util.*;
import java.io.*;

public class MainCode {
public static void main (String[] args)throws IOException{
// this is where I declare my little variables
double num, square;
String user;

//this is the first declaration of the scanner, also determining the name for the file to be created
Scanner scan = new Scanner(System.in);
System.out.print("Enter the name of the file you wish to create: ");
user = scan.nextLine().trim() + ".txt";

//the file is then created by using the data in the string, user
File myFile = new File (user);
myFile.createNewFile();

//I then open a printstream IO interface to write to the file, using the data now put in the string, user
PrintStream myPrintStream = new PrintStream (myFile);
System.out.print("Enter data to process: ");
user = scan.nextLine().trim();
myPrintStream.println(user);
myPrintStream.close();

//redefine the scanner, scan, to read information from the external file, myFile
scan = new Scanner (myFile);

//loop, to read each clump of data, see if it is a number and then do some math with it
while(scan.hasNext()){
if(scan.hasNextDouble()){
num = scan.nextDouble();
square = num * num;
System.out.println("The square of " + num + " is " + square);
}
else{
scan.next();
}
}
scan.close();
}
}