Writing to file from an array - java

I've been trying to write a method in java to write data from an array to a text file but I'm getting two errors.
public void WriteStudentDetailsToFile() {
PrintWriter out = null;
try {
out = new PrintWriter("StudentDetails.txt");
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
System.out.println("in " + System.getProperty("user.dir"));
System.exit(1);
}
for (int i = 0; i < enrolment.length; i += 1) {
if (enrolment[i] != null) {
Student a = this.enrolment[i];
if (a.getName().equals("") || a.getAddress().equals("") || a.getDOB().equals("") || a.getGender().equals("")) {
break;
} else {
String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" a.getDOB() + "\t" + a.getGender();
out.println(record);
}
}
}
}
}
The two errors are
C:\Users\B00661059\Downloads\Assignment 2\Assignment 2\Student_Enrolment.java:137: error: ';' expected
String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" a.getDOB() + "\t" + a.getGender();
^
C:\Users\B00661059\Downloads\Assignment 2\Assignment 2\Student_Enrolment.java:137: error: not a statement
String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" a.getDOB() + "\t" + a.getGender();

On line 137, you need to add a concatenation operator (+) here:
"\t" + a.getDOB()

The most obvious error is that you are missing a plus here: + "\t" a.getDOB() +.
It should be
String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" + a.getDOB() + "\t" + a.getGender();
You might also want to look into how to define and use a toString() function in your Student class to control the string representation of the object.

Try this :
String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" + a.getDOB() + "\t" + a.getGender();
instead of this :
String record = a.getName() + "\t" + "0" + "\t" + a.getAddress() + "\t" a.getDOB() + "\t" + a.getGender();
Also this doesn't make sense :
if (a.getName().equals("") || a.getAddress().equals("") || a.getDOB().equals("") || a.getGender().equals("")) {
break;
}
Since you are only interested in the else branch you can negate it.
if !(a.getName().equals("") || a.getAddress().equals("") || a.getDOB().equals("") || a.getGender().equals("")) {
// Do what you want
}

Related

how to simulate Excel control codes in String

One web project I'm involved in requires generating a CSV to the user.
One field is notes and there can be multiple notes in the field, separated by a return (alt-enter in Excel), appearing as
A USER entered on 02/17/2023: Test note
A USER entered on 02/17/2023: This is another note
Is it possible to insert control codes into a Java string that Excel will pick up and format the cell properly?
For what it's worth, this was my attempt:
}else if (formType.equalsIgnoreCase("downloadCSV")) {
Date d = new Date();
String filename = "C:/temp/rtTmp" + d.getTime() + ".csv";
File f = new File(filename);
List<Contact> contactList = RetentionTrackDatabaseUtil.getContacts();
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write("name,"
+ "gender,"
+ "age,"
+ "DOB,"
+ "DOH,"
+ "DOR,"
+ "DOS,"
+ "dateContact,"
+ "reason,"
+ "status,"
+ "empNum,"
+ "shift,"
+ "phone,note(s)\n");
for (Contact c : contactList) {
System.out.println(c.getAge());
bw.append("\"" + c.getName() + "\"" + ","
+ c.getGender() + ","
+ Integer.toString(c.getAge()) + ","
+ (c.getDOB()!=null?RetentionTrackUtil.sdf.format(c.getDOB()):"") + ","
+ (c.getDOH()!=null?RetentionTrackUtil.sdf.format(c.getDOH()):"") + ","
+ (c.getDOR()!=null?RetentionTrackUtil.sdf.format(c.getDOR()):"") + ","
+ (c.getDOS()!=null?RetentionTrackUtil.sdf.format(c.getDOS()):"") + ","
+ (c.getDateContact()!=null?RetentionTrackUtil.sdf.format(c.getDateContact()):"") + ","
+ c.getReason() + ","
+ c.getStatus() + ","
+ c.getEmpNum() + ","
+ c.getShift() + ","
+ c.getPhone() + ",");
for(Note n: c.getNoteList()){
bw.append("(" + n.getEnteredBy() + " on " + RetentionTrackUtil.sdf.format(n.getNoteDate()) + ")" + n.getNote() + "\\n");
}
bw.append("\n");
}
bw.close();
sendFile(response, filename);
}

How to search a token for a specific word in Java?

I have a segment of code that splits a string into tokens and prints them each out on a new line. I am having a hard time writing a code that determines if a word is a reserved word or not. I have to print "Reserved word is: " if the word is a java keyword, otherwise print "Current word is: ". Here is my code so far:
package projectweek3;
/**
*
* Name -
* Email Address -
* Date -
*
*/
public class Week3Project {
final static String program = "/*\n" +
" * To change this license header, choose License Headers in Project Properties.\n" +
" * To change this template file, choose Tools | Templates\n" +
" * and open the template in the editor.\n" +
" */\n" +
"package testapplication2;\n" +
"\n" +
"import java.util.Scanner;\n" +
"\n" +
"/**\n" +
" *\n" +
" * #author james\n" +
" */\n" +
"public class TestApplication2 {\n" +
"\n" +
" /**\n" +
" * #param args the command line arguments\n" +
" */\n" +
" public static void main(String[] args) {\n" +
" Scanner input = new Scanner(System.in);\n" +
" \n" +
" System.out.println(\"Enter integer #1\");\n" +
" int num1 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #2\");\n" +
" int num2 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #3\");\n" +
" int num3 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #4\");\n" +
" int num4 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #5\");\n" +
" int num5 = input.nextInt();\n" +
" \n" +
" //determine the sum\n" +
" int sum = num1 + num2 + num3 + num4 + num5;\n" +
" \n" +
" //this is helpful to make sure your sum is correct\n" +
" System.out.println(\"The sum is: \" + sum);\n" +
" \n" +
" //why doesn't this generate the sum correctly\n" +
" double average = sum / 5;\n" +
" \n" +
" //The average, lets hope its right...\n" +
" System.out.println(\"The average of your numbers is: \" + average);\n" +
" \n" +
" }\n" +
" \n" +
"}\n" +
"";
**public static void main(String[] args)
{
String str = program;
String s = "";
for (int i = 0; i < str.length(); i++) {
s += str.charAt(i) + "";
if (str.charAt(i) == ' ' || str.charAt(i) == '\t' || str.charAt(i) == '\n' || (str.charAt(i) == ' ' && str.charAt(i) == '\n')) {
String currentWord = s.toString();
String res = "int";
if (currentWord.equals(res)) {
System.out.println("Reserved word is: [" + currentWord + "]");
}
else {
System.out.println("Current word is: [" + currentWord + "]");
}
s = "";//Clear the string to get it ready to build next token.
}
}**
I would reconsider the way you're looping through the "program."
Instead of going through character by character, use the Java String.split() function.
String program = "int num1 = input.nextInt();\n";
String[] words = program.split("[\\n\\s\\t]");
for (String word : words) {
System.out.println(word);
}
Output:
int
num1
=
input.nextInt();
EDIT:
Since you can't use String.split(), your looping solution looks good. To check if the current word is reserved, try using Set.contains().
Set<String> reserved = new HashSet<>();
reserved.add("int");
// ...
if reserved.contains(word) {
System.out.println("Reserved word is: " + word);
} else {
System.out.println("Current word is: " + word);
}
That is, assuming you're allowed to use Set.

Is it possible to shorten this code?

I'm not exactly a beginner in Java, but I'm also not an expert. Is there any way to shorten this code so that it takes up less space and possibly fewer lines?
JOptionPane.showMessageDialog(null, "The student's names are: "
+ roster[0][0] + " " + roster[1][0] + ", "
+ roster[0][1] + " " + roster[1][1] + ", "
+ roster[0][2] + " " + roster[1][2] + ", and "
+ roster[0][3] + " " + roster[1][3] + ".");
Let's refactoring the code step by step
introduce variables to remove duplication.
T[] col1 = roster[0];
T[] col2 = roster[1];
String content = col1[0] + " " + col2[0] + ", "
+ col1[1] + " " + col2[1] + ", "
+ col1[2] + " " + col2[2] + ", and "
+ col1[3] + " " + col2[3] + ".";
JOptionPane.showMessageDialog(null,"The student's names are: " + content);
separate string concatenation into multi-assignments, then all assignments are the same except the last one.
int i = 0;
String content = "";
content += col1[i] + " " + col2[i] + ", ";i++;
content += col1[i] + " " + col2[i] + ", ";i++;
content += col1[i] + " " + col2[i] + ", ";i++;
content += "and " + col1[i] + " " + col2[i] + ".";i++;
make the multi-assignments consistent by using ternary operator.
int i = 0;
String content = "";
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
using while-loop to remove duplication.
int i = 0;
String content = "";
while(i<=3) {
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", ");
i++;
}
replacing while-loop with for-loop.
String content = "";
for (int i = 0; i <= 3; i++) {
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", ");
}
introduce variables to make the code more readable.
String content = "";
for (int i = 0; i <= 3; i++) {
String prefix = i == 3 ? "and " : "";
String current = col1[i] + " " + col2[i];
String suffix = i == 3 ? "." : ", ";
content += prefix + current + suffix;
}
inline the variables col1 & col2 that is used only once:
String content = "";
for (int i = 0; i <= 3; i++) {
String prefix = i == 3 ? "and " : "";
String current = roster[0][i] + " " + roster[1][i];
String suffix = i == 3 ? "." : ", ";
content += prefix + current + suffix;
}
replace magic number 3 with constant and the final code is below:
final int last = 3;
String content = "";
for (int i = 0; i <= last; i++) {
String prefix = i == last ? "and " : "";
String suffix = i == last ? "." : ", ";
String current = roster[0][i] + " " + roster[1][i];
content += prefix + current + suffix;
}
JOptionPane.showMessageDialog(null, "The student's names are: " + content);
StringBuilder message = new StringBuilder("The student's names are: ");
for (int i = 0; i < roster[0].length; i++) {
message
.append(roster[0][i])
.append(" ")
.append(roster[1][i]);
if (i < roster[0].length - 1)
message.append(", ");
if (i == roster[0].length - 2)
message.append("and ")
}
message.append(".");
JOptionPane.showMessageDialog(null, message.toString());
Possibly something like that. As you can see, you don't really save any lines, but obviously the code is more flexible as it can account for a variable length roster.
You can do the following: Initially assign the normal statement that you do not want repeated. Then loop through roster.
String rosterString= "The student's names are: ";
for(int i=0;i<= roster.length;i++){
for(int j=0;j<= roster[i].length;j++){
rosterString += (roster[i][j] + " ");
if (i == 1 && j < 2) {
rosterString += ", ";
}
else if (i == 1 && j == 2) {
rosterString += ", and";
}
else if (i == 1 && j == 3) {
rosterString += ".";
}
else {
rosterString += " ";
}
}
}
And then pass rosterString to your method.
String rosterString = "";
for(int i = 0; i < roster[0].length; i++) {
rosterString += roster[0][i] + " " + roster[1][i] + ", ";
}
That code will create a string with the names and commas. You could then add in if statements to check if it is near the end to change the , to an and or .

Formatting a return string

I am trying to format this return string to display ratings at one decimal place.
return String.format(title + "," + genre + "," + releaseYear + "(" + (getRating() == -1 ? "No ratings" : getRating()) + "): " + numOfDeaths + " deaths");
I keep getting an error saying too few parameters passed.
You want java.text.DecimalFormat.
DecimalFormat df = new DecimalFormat("0.0##");
String result = df.format(getRating());
return String.format(title + "," + genre + "," + releaseYear + "(" + (getRating() == -1 ? "No ratings" : result) + "): " + numOfDeaths + " deaths");

clearing a JLabel not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have read several posts suggesting to clear a JLabel (displayEntered) on a panel (display) with text by using the setText(" "). However, I have tried this and the outcome is it is just posting the array entered twice and does not clear the first set. I have an action shown below when a button is pressed both times; the first is to enter the data entered (I have the same code 4 times for the 4 different possible objects to enter but just put in the one since it's basically the same), which works fine and the second is to remove a specific one shown. My code is long, so am just putting that in. If someone wants something else please let me know. Thanks, I'd appreciate any input!
//adds the Herb data to the Array and list
enterHerbData.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Enter")){
Name = NameTxt.getText();
Colors = ColorTxt.getText();
ID = (int) IDCmbo.getSelectedItem();
Flavor = FlavorTxt.getText();
if(((String) MedicinalCmbo.getSelectedItem()).equals("Yes"))
Medicinal = true;
else
Medicinal = false;
if(((String) SeasonalCmbo.getSelectedItem()).equals("Yes"))
Seasonal = true;
else
Seasonal = false;
plants[count] = new Herb(Name, ID, Colors, Flavor, Medicinal, Seasonal);
String displayArraytemp = " ";
if(plants[count] != null){
if(plants[count] instanceof Flower){
displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Flower)plants[count]).getColor() + ", " + ((Flower)plants[count]).getSmell() + ", Thorny: " + ((Flower)plants[count]).getThorns() + "\n");
}
else if(plants[count] instanceof Fungus){
displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Fungus)plants[count]).getColor() + ", Poisonous: " + ((Fungus)plants[count]).getPoisonous() + "\n");
}
else if(plants[count] instanceof Weed){
displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Weed)plants[count]).getColor() + ", Edible: " + ((Weed)plants[count]).getEdible() + ", Medicinal: " + ((Weed)plants[count]).getMedicinal() + ", Poisonous: " + ((Weed)plants[count]).getPoisonous() + "\n");
}
else if(plants[count] instanceof Herb){
displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Herb)plants[count]).getColor() + ", " + ((Herb)plants[count]).getFlavor() + ", Medicinal: " + ((Herb)plants[count]).getMedicinal() + ", Poisonous: " + ((Herb)plants[count]).getSeasonal() + "\n");
}
sb.append("<html>" + displayArraytemp).
append("<br> ");
displayArray = sb.toString();
}
displayEntered.setText(displayArray);
count++;
frameB.setVisible(false);
}
}
});
//removes the data to the Array and panel
ActionListener RemoveAction = new ActionListener(){
#Override
public void actionPerformed(ActionEvent RemoveAction){
if(RemoveAction.getActionCommand().equals("Enter")){
if((Btn1).isSelected()){
String displayArraytemp2 = " ";
if(count >= 1){
for(int n = 0; n < count; n++){
plants[n] = plants[n+1];
}
count--;
frameB.setVisible(false);
displayEntered.setOpaque(true);
for(int n = 0; n < 25; n++){
if(plants[n] != null){
if(plants[n] instanceof Flower){
displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Flower)plants[n]).getColor() + ", " + ((Flower)plants[n]).getSmell() + ", Thorny: " + ((Flower)plants[n]).getThorns() + "\n");
}
else if(plants[n] instanceof Fungus){
displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Fungus)plants[n]).getColor() + ", Poisonous: " + ((Fungus)plants[n]).getPoisonous() + "\n");
}
else if(plants[n] instanceof Weed){
displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Weed)plants[n]).getColor() + ", Edible: " + ((Weed)plants[n]).getEdible() + ", Medicinal: " + ((Weed)plants[n]).getMedicinal() + ", Poisonous: " + ((Weed)plants[n]).getPoisonous() + "\n");
}
else if(plants[n] instanceof Herb){
displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Herb)plants[n]).getColor() + ", " + ((Herb)plants[n]).getFlavor() + ", Medicinal: " + ((Herb)plants[n]).getMedicinal() + ", Poisonous: " + ((Herb)plants[n]).getSeasonal() + "\n");
}
sb.append("<html>" + displayArraytemp2).
append("<br> ");
displayArray = sb.toString();
}
}
}
displayEntered.setText(" ");
displayEntered.setText(displayArray);
}
}
}};
Your real problem is that you are re-using sb without clearing it.

Categories

Resources