First ever java code

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...

randomizer

Distinguished
I'm not that familiar with Java and I don't know what the "input" type is, but on lines 27-29 you're declaring a type again when you shouldn't be. Get rid of "input" from those lines. It should just be "name = Scanner.nextLine();"
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
ohhh , yea still trying to get the syntax down for java only had like 2 classes but teacher seems since it is a 300 lvl class and we all took some other kind of programing language this should be easy. But C++ syntax is like way different then java IMO. Scanner still confuses me.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
I changed up the code based on powerpoint slide the professor gave us. Not sure if I made it worse.

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

	
class baseball {
	
	public static void main(String[] args) {
	
	String name;
	int ab;
	int hits;
	float avg;
		
	String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
	Scanner scanner = null;
	
	
	try {

	/*System.out.println("Enter the file to write to: ");
	out = new FileOutputStream(in.next());*/
    	
	scanner = new Scanner(new FileReader(file));
	name = scanner.next();
	ab = scanner.nextInt();
	hits = scanner.nextInt();
	}
	catch (Exception e)
	{
		System.out.println("ERROR. Could not open file!");
	}
	
	
	
	}
	
	
}

ERROR:
cannot find symbol class FileReader line 33
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Ok thank you that was it, I just added a simple print the name statement to see if it worked but it only printed out the first player, Do I have to put it in a loop to make it print out all the names and other things? Also when I try to get the avg and print the avg out it is given me 0.

Off topic, how do I get the black command box to open in JCreator? It just shows me the output in the window below the code which is annoying. In the general output tab.

UPDATED CODE:
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;

	
class baseball {
	
	public static void main(String[] args) {
	
	String name;
	int ab;
	int hits;
	int avg;
		
	String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
	Scanner scanner = null;
	
	
	try {

	System.out.println("Enter the file to write to: ");
	//out = new FileOutputStream(in.next())
    	
	scanner = new Scanner(new FileReader(file));
	name = scanner.next();
	ab = scanner.nextInt();
	hits = scanner.nextInt();
	
	avg = hits/ab;
	
	System.out.println(avg);
	}
	
	catch (Exception e)
	{
		System.out.println("ERROR. Could not open file!");
	}
	
	
	
	}
	
	
}
 

kyeana

Distinguished
May 21, 2008
230
0
18,860
For it only getting the first line, yes you will need a loop.

I think scanner has a method along the lines of hasNextLine() (check the api) which return a boolean telling if there is another line in the file. You can just loop on that to get all of the lines in the file.

As for the JCreator thing i have never used that IDE so i couldn't say.

Good luck!
 

paperkut91

Distinguished
Feb 7, 2011
4
0
18,510
To get the command line box you have to go to the configure tab then options then JDK tools ..click on the default then click edit and make sure the "Show command line " is checked off.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630


I did this and still no black box that pops up like in C++... =( also program not finished yet, still need to learn how after i get the fiel to import correctly to calculate the avg of hits and ab and then export it to a new file that the user names.

Yea your prob getting the error message because I have the line for the user to enter the file to export to commented out, plus not even sure its correct.
 

paperkut91

Distinguished
Feb 7, 2011
4
0
18,510
Thats odd thats what i did to get the command prompt up. When you go to Option , JDK tools , theres a drop down that says like Compiler or Run Application. Click on one of those and then click default then edit then check off the show command line. Play around with that youll get the command line to pop up. show your final code when you done . You might need a loop to show every players name not to sure about it. Im new to java too
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Okay got it to read all lines and got it to export to a new file that a user names. Now I need to figure out how to add another column for the AVG of the HITS and AT BATS.

What the text file looks like:

derek 600 188
alex 550 170
mark 575 165
jorge 458 125
robby 610 210
nick 510 150
brett 540 170


what the output file should look like:

derek 600 188 AVG of the 2
alex 550 170 AVG of the 2
mark 575 165 AVG of the 2
jorge 458 125 AVG of the 2
robby 610 210 AVG of the 2
nick 510 150 AVG of the 2
brett 540 170 AVG of the 2



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.*;

	
class baseball {
	
	public static void main(String[] args) {
	
	PrintStream ps;
	

	int ab;
	int hits;
	int avg;
		
	String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
	Scanner scanner = null;
	BufferedReader reader;
	reader = new BufferedReader(new InputStreamReader(System.in));
	
	
	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();
	    ps.println(name);
	}

	
	//avg = hits/ab;
	
	//System.out.println(name);
	}
	
	catch (Exception e)
	{
		System.out.println("ERROR. Could not open file!");
	}
	
	
	
	}
	
	
}


The way I have this I dont think I have the NAME, AB, and HITS separated... I kinda need like a foreach and split like in perl to separate all the data so I can assign them to variables and then I can get the AVG. Need to figure out how to do that. Does java have this? Like the split would be white space.
 

randomizer

Distinguished
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 of the loop otherwise the data will be lost.
 
Solution

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
The thing is that 2 of the other data are int's so would make the array a string mess with that? Like I wanna do this lol,

inside like a foreach loop.
($name,$phone,$address,$date,$zip)=split(/\:/,$raw);

Not sure if java has this, since I wrote it in perl.

so it splits into just 3 variables inside a loop.
 

randomizer

Distinguished
Anything you read out of the file is a string. You need to convert it to an integer before you can use it in a calculation. There is a difference between each of these as far as the computer is concerned:

12 (an int)
12.0 (a float/decimal/double depending on how you declared it)
'12' (a character where 12 is the ASCII code)
"12" (a string)

A string array is fine for splitting input from a text file. I've used it in C# with nested loops that split a file line-by-line, and then each line by comma delimiters.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
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.*;

	
class baseball {
	
	public static void main(String[] args) {
	
	PrintStream ps;
	

	int ab;
	int hits;
	int avg;
		
	String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
	Scanner scanner = null;
	BufferedReader reader;
	reader = new BufferedReader(new InputStreamReader(System.in));
	
	
	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 = Integer.temp[1];
			hits = Integer.temp[2];
			
			
			ps.println(name);
	   		avg = hits/ab;
	    	ps.println(avg);
		}
	    
	}

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

Okay figured out how to split with white space but getting this error, Says cant find temp, but isnt String[] temp = null; declaring it?
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Alrighty, Figured out pretty much everything, all working and everything. The only 2 problems I am having is how do I get them all one the same line, the NAME, AB, HITS and AVG. My program is just listing them all like this:

NAME
AB
HITS
AVG

what I want is this with like 3 spaces between each one:

NAME AB HITS AVG


Also need to make the AVG only go 3 decimal places. I tried to do it on line 63 but getting errors.

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.*;


	
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));
	
	
	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);
			ps.println(ab);
		 	ps.println(hits);
		 	ps.println(%.3f\n, avg);
		}
	    
	}

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