Avortex Posted June 26, 2015 Share Posted June 26, 2015 (edited) Hi, so I just started to learn Java and need some help on this practice assignment that I got and I have no clue what's going on lol The question is: Three employees in a company are up for a special pay increase. Create an input file called Lab3_input.txt, with the following data: Miller Andrew 65789.87 5 Green Shelia 75892.56 6 Sethi Amit 74900.50 6.1 Wills George 56784.67 10 Array Steve 87034.50 2.5 Each input line consists of an employee's last name, first name, current salary, and percent pay increase. For example, in the first input line, the last name of the employee is Miller, the first name is Andrew, the current salary is 65789.87, and the pay increase is 5%. Write a program that reads data from the specified file and stores the output in the file Lab3_output.txt. For each employee, the data must be output in the following form: firstname lastname updatedsalary Formart the output of decimal numbers to two decimal places. -------------- Any help is appreciated. Thanks! Edited June 26, 2015 by Avortex Quote Link to comment Share on other sites More sharing options...
Extreme Scripts Posted June 26, 2015 Share Posted June 26, 2015 (edited) You would need to use a scanner/buffered reader for this. Very simple assignment. private String fName; private String sName; private double pay; private double increase; private BufferedReader br; private void readData(){ br = new BufferedReader(new InputStreamReader("FiLE PATH")); String input = ""; while((input = br.readLine()) != null){ String[] fields = input.split(" "); sName = fields[0]; fName = fields[1]; pay = Double.parseDouble(fields[2]); increase = Double.parseDouble(fields[3]); } } Then you would have to use a PrintWriter/FileWriter for the output part... Sure you can figure out the rest Edited June 26, 2015 by Divinity 1 Quote Link to comment Share on other sites More sharing options...
Avortex Posted June 26, 2015 Author Share Posted June 26, 2015 You would need to use a scanner/buffered reader for this. Very simple assignment. private String fName; private String sName; private double pay; private double increase; private BufferedReader br; private void readData(){ br = new BufferedReader(new InputStreamReader("FiLE PATH")); String input = ""; while((input = br.readLine()) != null){ String[] fields = input.split(" "); sName = fields[0]; fName = fields[1]; pay = Double.parseDouble(fields[2]); increase = Double.parseDouble(fields[3]); } } Sure you can figure out the rest Thanks bro. But could you do Miller Andrew for me, so I would know how to do the rest? Thanks. I just don't know where to put the first guy -_- Quote Link to comment Share on other sites More sharing options...
Apaec Posted June 26, 2015 Share Posted June 26, 2015 Nice and straightforward, perfect way to practice file interaction Here's a method which botre sent me a while back, should do the trick for loading the data. (modified the onLoadData() method to show you how to load ints, Strings and booleans). public void loadData() { FileReader fileReader = null; try { fileReader = new FileReader(OSBDirectory.GUI_DIRECTORY + OSBDirectory.separator + "test.txt"); //example directory } catch (FileNotFoundException e) { return; } BufferedReader bufferedReader = new BufferedReader(fileReader); onLoadSettings(bufferedReader); try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } public void onLoadData(BufferedReader bufferedReader) { try { boolean b = Boolean.parseBoolean(bufferedReader.readLine()); //reads first line and saves into local var b as a boolean. } catch (NumberFormatException | IOException e) { e.printStackTrace(); } try { int i = Integer.parseInt(bufferedReader.readLine()); //reads second line and saves into local var i as an int. } catch (IOException e) { e.printStackTrace(); } catch (NumberFormatException e) { e.printStackTrace(); } try { String s = (bufferedReader.readLine()); //reads the 3rd line outputting the line into local variable s as a String. } catch (IOException e) { e.printStackTrace(); } } As for saving the settings: public void saveSettings() { PrintWriter writer = null; try { writer = new PrintWriter(OSBDirectory.GUI_DIRECTORY + OSBDirectory.separator + "test.txt", "UTF-8"); //example directory, look at docs for exact implementation of PrintWriter() } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } onSaveSettings(writer); writer.flush(); writer.close(); } public void onSaveSettings(PrintWriter writer) { writer.println(boolean b); writer.println(int i); writer.println(String s); } ** Saves the format into a file in the given directory under the name test.txt in the form: b i s dont forget the file needs creating. ** You should hopefully find it really straight forward after I ggave you that as a starting point hehe Apa Quote Link to comment Share on other sites More sharing options...
Avortex Posted June 26, 2015 Author Share Posted June 26, 2015 Nice and straightforward, perfect way to practice file interaction Here's a method which botre sent me a while back, should do the trick for loading the data. (modified the onLoadData() method to show you how to load ints, Strings and booleans). public void loadData() { FileReader fileReader = null; try { fileReader = new FileReader(OSBDirectory.GUI_DIRECTORY + OSBDirectory.separator + "test.txt"); //example directory } catch (FileNotFoundException e) { return; } BufferedReader bufferedReader = new BufferedReader(fileReader); onLoadSettings(bufferedReader); try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } public void onLoadData(BufferedReader bufferedReader) { try { boolean b = Boolean.parseBoolean(bufferedReader.readLine()); //reads first line and saves into local var b as a boolean. } catch (NumberFormatException | IOException e) { e.printStackTrace(); } try { int i = Integer.parseInt(bufferedReader.readLine()); //reads second line and saves into local var i as an int. } catch (IOException e) { e.printStackTrace(); } catch (NumberFormatException e) { e.printStackTrace(); } try { String s = (bufferedReader.readLine()); //reads the 3rd line outputting the line into local variable s as a String. } catch (IOException e) { e.printStackTrace(); } } As for saving the settings: public void saveSettings() { PrintWriter writer = null; try { writer = new PrintWriter(OSBDirectory.GUI_DIRECTORY + OSBDirectory.separator + "test.txt", "UTF-8"); //example directory, look at docs for exact implementation of PrintWriter() } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } onSaveSettings(writer); writer.flush(); writer.close(); } public void onSaveSettings(PrintWriter writer) { writer.println(boolean b); writer.println(int i); writer.println(String s); } ** Saves the format into a file in the given directory under the name test.txt in the form: b i s dont forget the file needs creating. ** You should hopefully find it really straight forward after I ggave you that as a starting point hehe Apa Omg thank you so much. But I'm left with a couple of question(s) So, now do I just copy and paste both of those sections onto Eclipse (or what program do you use? Also, in what section of the code do I put the first names and last names? Thanks! Quote Link to comment Share on other sites More sharing options...
MalikDz Posted June 26, 2015 Share Posted June 26, 2015 https://github.com/TheBiblMan/Employee Here you go. Quote Link to comment Share on other sites More sharing options...
Twin Posted June 26, 2015 Share Posted June 26, 2015 Nice and straightforward, perfect way to practice file interaction Here's a method which botre sent me a while back, should do the trick for loading the data. (modified the onLoadData() method to show you how to load ints, Strings and booleans). public void loadData() { FileReader fileReader = null; try { fileReader = new FileReader(OSBDirectory.GUI_DIRECTORY + OSBDirectory.separator + "test.txt"); //example directory } catch (FileNotFoundException e) { return; } BufferedReader bufferedReader = new BufferedReader(fileReader); onLoadSettings(bufferedReader); try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } public void onLoadData(BufferedReader bufferedReader) { try { boolean b = Boolean.parseBoolean(bufferedReader.readLine()); //reads first line and saves into local var b as a boolean. } catch (NumberFormatException | IOException e) { e.printStackTrace(); } try { int i = Integer.parseInt(bufferedReader.readLine()); //reads second line and saves into local var i as an int. } catch (IOException e) { e.printStackTrace(); } catch (NumberFormatException e) { e.printStackTrace(); } try { String s = (bufferedReader.readLine()); //reads the 3rd line outputting the line into local variable s as a String. } catch (IOException e) { e.printStackTrace(); } } As for saving the settings: public void saveSettings() { PrintWriter writer = null; try { writer = new PrintWriter(OSBDirectory.GUI_DIRECTORY + OSBDirectory.separator + "test.txt", "UTF-8"); //example directory, look at docs for exact implementation of PrintWriter() } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } onSaveSettings(writer); writer.flush(); writer.close(); } public void onSaveSettings(PrintWriter writer) { writer.println(boolean b); writer.println(int i); writer.println(String s); } ** Saves the format into a file in the given directory under the name test.txt in the form: b i s dont forget the file needs creating. ** You should hopefully find it really straight forward after I ggave you that as a starting point hehe Apa I was so confused on why you were telling him to read from the osbot directiory, then I realized this was from your rock crab script. Quote Link to comment Share on other sites More sharing options...
Apaec Posted June 26, 2015 Share Posted June 26, 2015 I was so confused on why you were telling him to read from the osbot directiory, then I realized this was from your rock crab script. No, it was just an example of fetching a directory. Also, @ op, yes, you need to copy and paste those. I'm not going to tell you exactly how to do it, it is paramount you learn yourself. But i've shown you how to grab and write elements to a file. Also, you can see that i've done it without arrays. This is probably the best way of doing it if you have 2-3 lines but after that it may become somewhat strenuous. So you'll need to implement arrays if you have more than 6 lines, just to make it tidier. apa Quote Link to comment Share on other sites More sharing options...
Twin Posted June 26, 2015 Share Posted June 26, 2015 No, it was just an example of fetching a directory. Also, @ op, yes, you need to copy and paste those. I'm not going to tell you exactly how to do it, it is paramount you learn yourself. But i've shown you how to grab and write elements to a file. Also, you can see that i've done it without arrays. This is probably the best way of doing it if you have 2-3 lines but after that it may become somewhat strenuous. So you'll need to implement arrays if you have more than 6 lines, just to make it tidier. apa Awkward. I just saw the osbot directory and thought it was what you sent me like 4 months ago Quote Link to comment Share on other sites More sharing options...
MalikDz Posted June 26, 2015 Share Posted June 26, 2015 No, it was just an example of fetching a directory. Also, @ op, yes, you need to copy and paste those. I'm not going to tell you exactly how to do it, it is paramount you learn yourself. But i've shown you how to grab and write elements to a file. Also, you can see that i've done it without arrays. This is probably the best way of doing it if you have 2-3 lines but after that it may become somewhat strenuous. So you'll need to implement arrays if you have more than 6 lines, just to make it tidier. apa Why use arrays when https://github.com/TheBiblMan/Employee Quote Link to comment Share on other sites More sharing options...
Avortex Posted June 26, 2015 Author Share Posted June 26, 2015 https://github.com/TheBiblMan/Employee Here you go. What am I suppose to do with this? Quote Link to comment Share on other sites More sharing options...
Avortex Posted June 26, 2015 Author Share Posted June 26, 2015 To get the pay increase do I multiply the current salary which is 65789.87 * (.05) 5%? For Miller Andrew. Quote Link to comment Share on other sites More sharing options...
darvinb98 Posted June 26, 2015 Share Posted June 26, 2015 figured out lol Quote Link to comment Share on other sites More sharing options...
Bobrocket Posted June 27, 2015 Share Posted June 27, 2015 To get the pay increase do I multiply the current salary which is 65789.87 * (.05) 5%? For Miller Andrew. float increasedSalary = (oldSalary / 100) * payIncrease; float updatedSalary = oldSalary + increasedSalary; Quote Link to comment Share on other sites More sharing options...