First ever java code
Tags:
Last response: in Applications
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.
ERROR:
';' expected for lines 27,28,29....
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.
/**
* @(#)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....
More about : java code
I changed up the code based on powerpoint slide the professor gave us. Not sure if I made it worse.
ERROR:
cannot find symbol class FileReader line 33
/**
* @(#)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
Related ressources
- Biologist needs Mac code or script to repeatedly write java - Forum
- Java Tutorial Recommendations - Forum
- Java game code - Forum
- Calculator program - JAVA - Forum
- Java code to access pc webcamea - Forum
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:
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:
/**
* @(#)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!");
}
}
}
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!
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!
The api is also available online, and Java tends to have amazing documentation and example for their stuff. I would recommend checking that out as well.
In this case here is the scanner api
http://download.oracle.com/javase/1.5.0/docs/api/java/u...
and specifically the hasNextLine() method
http://download.oracle.com/javase/1.5.0/docs/api/java/u...()
Good luck
In this case here is the scanner api
http://download.oracle.com/javase/1.5.0/docs/api/java/u...
and specifically the hasNextLine() method
http://download.oracle.com/javase/1.5.0/docs/api/java/u...()
Good luck
paperkut91 said:
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.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.
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
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
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.
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
/**
* @(#)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.
Best 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:
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.
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.
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.
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.
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.
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.
/**
* @(#)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?
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.
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.
/**
* @(#)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!");
}
}
}
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
DRUM ROLL PLEASE
BOOM =DDDDDDDDD
/**
* @(#)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!");
}
}
}
Related ressources:
- Forum[help] java roguelike development insight needed
- ForumDownload java for android tablet
- ForumVery first-ever build-enthusiast $1,100-1,200
- ForumInstalling Java on the latest Apple Mac version
- ForumJava confusion
- ForumVery first-ever build-Enthusiast $1,100-1,200 revision 1
- ForumHow NOT to code
- ForumBeginner JAVA arithmetics help
- Forum(Maybe) some new idea about RL's source code openess in ge..
- Forumcompiling to z-machine code ?
- ForumWhat is the difference between Extended and normal version of a software?
- More resources
Read discussions in other Applications categories
!