Sign in with
Sign up | Sign in
Your question
Solved

First ever java code

Last response: in Applications
Share
February 6, 2011 12:07:48 AM

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.

  1. /**
  2. * @(#)baseball.java
  3. *
  4. * baseball application
  5. *
  6. * @author
  7. * @version 1.00 2011/2/5
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.io.*;
  12.  
  13. class baseball {
  14.  
  15. public static void main(String[] args) {
  16.  
  17. String file = "C:\\Users\\Welcome Back\\Desktop";
  18. input Scanner = new Scanner(new FileReader(file));
  19.  
  20. System.out.println("Enter the file to write to: ");
  21. File outFile = new File(in.next());
  22.  
  23. String name;
  24. int ab;
  25. int hits;
  26. float avg;
  27. name = input Scanner.nextLine();
  28. ab = input Scanner.nextLine();
  29. hits = input Scanner.nextline();
  30.  
  31. avg = hits/ab;
  32.  
  33. System.out.printf(avg);
  34.  
  35. }
  36.  
  37.  
  38. }


ERROR:
';' expected for lines 27,28,29....

More about : java code

a b L Programming
February 6, 2011 5:16:27 AM

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();"
m
0
l
February 6, 2011 4:21:56 PM

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.
m
0
l
Related resources
February 6, 2011 6:42:12 PM

I changed up the code based on powerpoint slide the professor gave us. Not sure if I made it worse.

  1. /**
  2. * @(#)baseball.java
  3. *
  4. * baseball application
  5. *
  6. * @author
  7. * @version 1.00 2011/2/5
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13.  
  14.  
  15. class baseball {
  16.  
  17. public static void main(String[] args) {
  18.  
  19. String name;
  20. int ab;
  21. int hits;
  22. float avg;
  23.  
  24. String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
  25. Scanner scanner = null;
  26.  
  27.  
  28. try {
  29.  
  30. /*System.out.println("Enter the file to write to: ");
  31. out = new FileOutputStream(in.next());*/
  32.  
  33. scanner = new Scanner(new FileReader(file));
  34. name = scanner.next();
  35. ab = scanner.nextInt();
  36. hits = scanner.nextInt();
  37. }
  38. catch (Exception e)
  39. {
  40. System.out.println("ERROR. Could not open file!");
  41. }
  42.  
  43.  
  44.  
  45. }
  46.  
  47.  
  48. }


ERROR:
cannot find symbol class FileReader line 33
m
0
l
a b L Programming
February 7, 2011 7:21:09 AM

You need to import java.io.FileReader
m
0
l
February 7, 2011 6:35:28 PM

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:
  1. /**
  2. * @(#)baseball.java
  3. *
  4. * baseball application
  5. *
  6. * @author
  7. * @version 1.00 2011/2/5
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.FileReader;
  14.  
  15.  
  16. class baseball {
  17.  
  18. public static void main(String[] args) {
  19.  
  20. String name;
  21. int ab;
  22. int hits;
  23. int avg;
  24.  
  25. String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
  26. Scanner scanner = null;
  27.  
  28.  
  29. try {
  30.  
  31. System.out.println("Enter the file to write to: ");
  32. //out = new FileOutputStream(in.next())
  33.  
  34. scanner = new Scanner(new FileReader(file));
  35. name = scanner.next();
  36. ab = scanner.nextInt();
  37. hits = scanner.nextInt();
  38.  
  39. avg = hits/ab;
  40.  
  41. System.out.println(avg);
  42. }
  43.  
  44. catch (Exception e)
  45. {
  46. System.out.println("ERROR. Could not open file!");
  47. }
  48.  
  49.  
  50.  
  51. }
  52.  
  53.  
  54. }

m
0
l
a b L Programming
February 7, 2011 9:36:44 PM

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!
m
0
l
February 7, 2011 10:21:36 PM

Damn shucks need to figure that out to make sure it works correctly. I will look in my book about the scanner and see how that goes. Thanks!
m
0
l
February 7, 2011 10:45:52 PM

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.
m
0
l
February 7, 2011 10:59:21 PM

i tried running your program it just says

Enter the file to write to:
Error. Could not open file!
m
0
l
February 7, 2011 11:02:44 PM

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.
m
0
l
February 7, 2011 11:12:34 PM

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
m
0
l
February 7, 2011 11:52:46 PM

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



  1. /**
  2. * @(#)baseball.java
  3. *
  4. * baseball application
  5. *
  6. * @author
  7. * @version 1.00 2011/2/5
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.FileReader;
  14. import java.io.PrintStream;
  15. import java.io.*;
  16.  
  17.  
  18. class baseball {
  19.  
  20. public static void main(String[] args) {
  21.  
  22. PrintStream ps;
  23.  
  24.  
  25. int ab;
  26. int hits;
  27. int avg;
  28.  
  29. String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
  30. Scanner scanner = null;
  31. BufferedReader reader;
  32. reader = new BufferedReader(new InputStreamReader(System.in));
  33.  
  34.  
  35. try {
  36.  
  37. System.out.println("Enter the file to write to: ");
  38. ps = new PrintStream(reader.readLine());
  39.  
  40. Scanner in = new Scanner(new FileReader(file));
  41. while (in.hasNextLine())
  42. {
  43. String name = in.nextLine();
  44. ps.println(name);
  45. }
  46.  
  47.  
  48. //avg = hits/ab;
  49.  
  50. //System.out.println(name);
  51. }
  52.  
  53. catch (Exception e)
  54. {
  55. System.out.println("ERROR. Could not open file!");
  56. }
  57.  
  58.  
  59.  
  60. }
  61.  
  62.  
  63. }



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.
m
0
l
February 8, 2011 12:09:35 AM

you making progress. Ill try messing around with it
m
0
l

Best solution

a b L Programming
February 8, 2011 12:19:31 AM

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:

  1. 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.
Share
February 8, 2011 12:45:39 AM

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.
m
0
l
a b L Programming
February 8, 2011 12:56:39 AM

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.
m
0
l
February 8, 2011 1:11:28 AM

  1. /**
  2. * @(#)baseball.java
  3. *
  4. * baseball application
  5. *
  6. * @author
  7. * @version 1.00 2011/2/5
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.FileReader;
  14. import java.io.PrintStream;
  15. import java.io.*;
  16.  
  17.  
  18. class baseball {
  19.  
  20. public static void main(String[] args) {
  21.  
  22. PrintStream ps;
  23.  
  24.  
  25. int ab;
  26. int hits;
  27. int avg;
  28.  
  29. String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
  30. Scanner scanner = null;
  31. BufferedReader reader;
  32. reader = new BufferedReader(new InputStreamReader(System.in));
  33.  
  34.  
  35. try {
  36.  
  37. System.out.println("Enter the file to write to: ");
  38. ps = new PrintStream(reader.readLine());
  39.  
  40. Scanner in = new Scanner(new FileReader(file));
  41. while (in.hasNextLine())
  42. {
  43. String name = in.nextLine();
  44. String[] temp = null;
  45. temp = new String[3];
  46. temp = name.split("\\s+");
  47. for (int x = 0; x < 1; x++)
  48. {
  49.  
  50.  
  51. name = temp[0];
  52. ab = Integer.temp[1];
  53. hits = Integer.temp[2];
  54.  
  55.  
  56. ps.println(name);
  57. avg = hits/ab;
  58. ps.println(avg);
  59. }
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67. }
  68.  
  69. catch (Exception e)
  70. {
  71. System.out.println("ERROR. Could not open file!");
  72. }
  73.  
  74.  
  75.  
  76. }
  77.  
  78.  
  79. }


Okay figured out how to split with white space but getting this error, Says cant find temp, but isnt String[] temp = null; declaring it?
m
0
l
February 8, 2011 3:09:53 AM

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.

  1. /**
  2. * @(#)baseball.java
  3. *
  4. * baseball application
  5. *
  6. * @author
  7. * @version 1.00 2011/2/5
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.FileReader;
  14. import java.io.PrintStream;
  15. import java.io.*;
  16.  
  17.  
  18.  
  19. class baseball {
  20.  
  21. public static void main(String[] args) {
  22.  
  23. PrintStream ps;
  24.  
  25.  
  26. float ab;
  27. float hits;
  28. float avg;
  29.  
  30.  
  31. String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
  32. Scanner scanner = null;
  33. BufferedReader reader;
  34. reader = new BufferedReader(new InputStreamReader(System.in));
  35.  
  36.  
  37. try {
  38.  
  39. System.out.println("Enter the file to write to: ");
  40. ps = new PrintStream(reader.readLine());
  41.  
  42. Scanner in = new Scanner(new FileReader(file));
  43. while (in.hasNextLine())
  44. {
  45. String name = in.nextLine();
  46. String[] temp = null;
  47. temp = new String[3];
  48. temp = name.split("\\s+");
  49.  
  50. for (int x = 0; x < 1; x++)
  51. {
  52.  
  53.  
  54.  
  55. name = temp[0];
  56. ab = Float.parseFloat(temp[1]);
  57. hits = Float.parseFloat(temp[2]);
  58.  
  59. avg = hits/ab;
  60. ps.println(name);
  61. ps.println(ab);
  62. ps.println(hits);
  63. ps.println(%.3f\n, avg);
  64. }
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. }
  73.  
  74. catch (Exception e)
  75. {
  76. System.out.println("ERROR. Could not open file!");
  77. }
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84. }
m
0
l
February 8, 2011 3:54:50 AM

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

  1. /**
  2. * @(#)baseball.java
  3. *
  4. * baseball application
  5. *
  6. * @author
  7. * @version 1.00 2011/2/5
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.FileReader;
  14. import java.io.PrintStream;
  15. import java.io.*;
  16. import java.text.*;
  17.  
  18.  
  19.  
  20. class baseball {
  21.  
  22. public static void main(String[] args) {
  23.  
  24. PrintStream ps;
  25. float ab;
  26. float hits;
  27. float avg;
  28.  
  29.  
  30. String file = "C:\\Users\\Welcome Back\\Desktop\\BaseballData.txt";
  31. Scanner scanner = null;
  32. BufferedReader reader;
  33. reader = new BufferedReader(new InputStreamReader(System.in));
  34.  
  35. DecimalFormat dec = new DecimalFormat("0.000");
  36.  
  37.  
  38. try {
  39.  
  40. System.out.println("Enter the file to write to: ");
  41. ps = new PrintStream(reader.readLine());
  42.  
  43. Scanner in = new Scanner(new FileReader(file));
  44. while (in.hasNextLine())
  45. {
  46. String name = in.nextLine();
  47. String[] temp = null;
  48. temp = new String[3];
  49. temp = name.split("\\s+");
  50.  
  51. for (int x = 0; x < 1; x++)
  52. {
  53.  
  54.  
  55.  
  56. name = temp[0];
  57. ab = Float.parseFloat(temp[1]);
  58. hits = Float.parseFloat(temp[2]);
  59.  
  60. avg = hits/ab;
  61. ps.println(name + " " + ab + " " + hits + " " + dec.format(avg));
  62. //ps.println(ab);
  63. //ps.println(hits);
  64. //ps.println(avg);
  65. }
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. }
  74.  
  75. catch (Exception e)
  76. {
  77. System.out.println("ERROR. Could not open file!");
  78. }
  79.  
  80.  
  81.  
  82. }
  83.  
  84.  
  85. }
m
0
l
February 21, 2011 2:18:57 AM

Best answer selected by laserpp.
m
0
l
!