Jump to content

Need basic Java help


Avortex

Recommended Posts

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 by Avortex
Link to comment
Share on other sites

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

Edited by Divinity
  • Like 1
Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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! 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

blush.png Awkward. I just saw the osbot directory and thought it was what you sent me like 4 months ago

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...