Results 1 to 2 of 2
- 11-04-2011, 10:30 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
Saving string to list to add to file
Hey,
I have to design a program to create a file of sums. The user inputs the sum and then it gets outputted to a new file. The problem for me is that the file should only be written once, when all the sums are completed and the user ends the program. So as it goes along each sum should be added to a list and then I can write the list to the file. I have pasted the code so far below so you can get a general idea. I'd really appreciate it if someone could give me a few tips.
Thanks!
Java Code:import tcdIO.*; public class SumCreator { public static void main(String[] args) { Terminal terminal = new Terminal("Sum Program"); terminal.println("Welcome to my program."); terminal.println(""); terminal.println("Please follow the instructions to create your file."); terminal.println("1. Addition Sum"); terminal.println("2. Subtraction Sum"); terminal.println("3. Division Sum"); terminal.println("4. Multiplication sum"); terminal.println("0. End program and create file"); terminal.println(""); int type = terminal.readInt("Enter your selection: "); terminal.println(""); if (type == 1) { terminal.println("You have selected addition."); terminal.println(" "); String a = terminal.readString("Please select your first operand or a to abort: "); String b = terminal.readString("Please select your second operand or a to abort: "); String sum = a + " + " + b; terminal.println(sum); } else if (type == 2) { terminal.println("You have selected subtraction."); terminal.println(" "); String a = terminal.readString("Please select your first operand or a to abort: "); String b = terminal.readString("Please select your second operand or a to abort: "); String sum = a + " - " + b; terminal.println(sum); } else if (type == 3) { terminal.println("You have selected division."); terminal.println(" "); String a = terminal.readString("Please select your first operand or a to abort: "); String b = terminal.readString("Please select your second operand or a to abort: "); String sum = a + " / " + b; terminal.println(sum); } else if (type == 4) { terminal.println("You have selected multiplication."); terminal.println(" "); String a = terminal.readString("Please select your first operand or a to abort: "); String b = terminal.readString("Please select your second operand or a to abort: "); String sum = a + " * " + b; terminal.println(sum); } } }
- 11-04-2011, 11:15 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 15
- Rep Power
- 0
Re: Saving string to list to add to file
You should probably use a LinkedList or an ArrayList to remember all your sums.
See this for more details:
LinkedList (Java 2 Platform SE v1.4.2)
ArrayList (Java 2 Platform SE v1.4.2)
Collection Examples,Linked List Example, Linked List in Java,Linked List Example in Java,Linked List Tutorial
Java ArrayList example
For how to do file I/O you may find this useful:
Dystopian Code: Text File I/O in Java
Dystopian Code: Text File I/O with Strings that Contain Spaces in Java
Similar Threads
-
How can you convert a List<String[]> to a List<Double[]>?
By jetnor in forum New To JavaReplies: 8Last Post: 11-04-2011, 08:30 PM -
Saving a File
By sneeak in forum New To JavaReplies: 1Last Post: 08-17-2011, 05:40 AM -
saving a string data.
By imorio in forum New To JavaReplies: 7Last Post: 11-18-2010, 11:33 AM -
Saving a file in a gui
By rav in forum AWT / SwingReplies: 9Last Post: 07-07-2010, 05:27 PM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks