First ever java code

Page 2 - Seeking answers? Join the Tom's Guide community: where nearly two million members share solutions and discuss the latest tech.

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Alright first off I think my JCreator isn't working right because I got it off of my school website for free. When I try to run the program I get a message about setting about a JDK path or something and that I need the newest version. I can right code in it but don't know if it will allow me to run it. Well back to the code.


What I got to do it create a program that reads from a file of baseball scores, name, at bats(ab) and hits. Then get the avg by ab/hits and output it to a file that the user names. The output file should be: Name Ab Hits Avg. Here's the code.

Code:
/**
 * @(#)baseball.java
 *
 * baseball application
 *
 * @author 
 * @version 1.00 2011/2/5
 */
 
import java.util.Scanner;
import java.io.*;
	
class baseball {
	
	public static void main(String[] args) {
    	
	String file = "C:\\Users\\Welcome Back\\Desktop";
	input Scanner = new Scanner(new FileReader(file));
	
	System.out.println("Enter the file to write to: ");
	File outFile = new File(in.next());
	
	String name;
	int ab;
	int hits;
	float avg;
	name = input Scanner.nextLine();
	ab = input Scanner.nextLine();
	hits = input Scanner.nextline();
	
	avg = hits/ab;
	
	System.out.printf(avg);
	
	}
	
	
}

ERROR:
';' expected for lines 27,28,29....
 
Solution
Splitting strings is easy. It's basically the same as C# (which I am familiar with, and which is generally similar to Java). The method is String.split(). You'd probably call it like this:

Code:
String[] someArray = in.nextLine().split(" ");

I don't know how to declare an array properly so you'd need to check the syntax for that. Basically this will store the part of the string before the first space in someArray[0], the next part in someArray[1], etc. You'd need to make sure you have logic to catch spaces in names though, unless you're only working with one name per person. Note that this array, if declared inside the loop, will get replaced each time the loop iterates, so you'll need to do all your work on each record before the end...
FINALLY got it all working, I am way sure that I made this like 100 times more complicated then what the professor wanted, and I really don't understand what half it is doing, going to ask the professor on my free time to go through line by line so I can learn it better.


DRUM ROLL PLEASE


BOOM =DDDDDDDDD

Code:
/**
 * @(#)baseball.java
 *
 * baseball application
 *
 * @author 
 * @version 1.00 2011/2/5
 */
 
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.io.*;
import java.text.*;


	
class baseball {
	
	public static void main(String[] args) {
	
	PrintStream ps;
	float ab;
	float hits;
	float avg;
	
		
	String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
	Scanner scanner = null;
	BufferedReader reader;
	reader = new BufferedReader(new InputStreamReader(System.in));
	
	DecimalFormat dec = new DecimalFormat("0.000");
	
	
	try {

	System.out.println("Enter the file to write to: ");
	ps = new PrintStream(reader.readLine());
    	
	Scanner in = new Scanner(new FileReader(file));
	while (in.hasNextLine())
	{
		String name = in.nextLine();
		String[] temp = null;
		temp = new String[3];
		temp = name.split("\\s+");
		
		for (int x = 0; x < 1; x++)
		{
		
		
			
			name = temp[0];
			ab = Float.parseFloat(temp[1]);
			hits = Float.parseFloat(temp[2]);
			
			avg = hits/ab;
			ps.println(name + "    " + ab + "    " + hits + "    " + dec.format(avg));
			//ps.println(ab);
		 	//ps.println(hits);
		 	//ps.println(avg);
		}
	    
	}

	
	
	
	
	}
	
	catch (Exception e)
	{
		System.out.println("ERROR. Could not open file!");
	}
	
	
	
	}
	
	
}