New Posts  All Forums:Forum Nav:

Quick IO question

post #1 of 8
Thread Starter 
So this is my IO method for an assignment im doing im trying to read the file and immediately print it to a file for echoed output to make sure it works.
public static void getInput(String fileName){
String line;
String[] tokens;
Customer[] customers = new Customer[20];
int size = 0;
List list = new List();
List removed = new List();

try{
BufferedReader input = new BufferedReader(new FileReader(inputFile));
PrintWriter writer = new PrintWriter(new FileWriter(outputFile));

line = input.readLine();

while(line != null){
tokens = line.split(" ");
writer.println(line);

if(tokens[0].equals("ARRIVE")){
customers[size] = new Customer(tokens[1]);
list.addCustomer(customers[size].getName());
stack.push(customers[size]);

if(stack.fullSeats()){
queue.add(customers[size]);
}
size++;
}

if(tokens[0].equals("LEAVE")){
removed.addCustomer(tokens[1]);

stack.clear();
stack.push(new Customer(tokens[1]));

}
line = input.readLine();
}// end while
input.close();
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}// end getInput()

Can java not read and right at the same time?
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #2 of 8
Are you getting an error? If so then what is it.
I see fileName, inputFile, and outputFile. Are they all initialized? If inputFile and outputFile are initialized somewhere else then whats the point of fileName?
This is the problem with not showing all the code, and use a code block to post code.
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
post #3 of 8
Thread Starter 
the input and output files are static variable and both files exist there is no error message only a blank output file. when i put the writer into it's own method it works. We need to have a certain kind of output and i figured it would be easier to output as i input it. Here is the rest of main

import java.io.*;
public class A1Q1 {

static Stack stack = new Stack();
static List list = new List();
static Queue queue = new Queue();
static String inputFile = "shopevents.txt";
static String outputFile = "shoplog.txt";

public static void main(String[] args) {
List stackCopy = new List();
List queueCopy = new List();

getInput(inputFile);

list.queueCopy(queueCopy);
list.stackCopy(stackCopy);

//System.out.println("This is the stack: " + stack);
//System.out.println("This is the queue: " + queue);

}// end main

public static void getInput(String fileName){
String line;
String[] tokens;
Customer[] customers = new Customer[20];
int size = 0;
List list = new List();
List removed = new List();

try{
BufferedReader input = new BufferedReader(new FileReader(inputFile));
PrintWriter writer = new PrintWriter(new FileWriter(outputFile));

line = input.readLine();

while(line != null){
tokens = line.split(" ");
writer.println(line);

if(tokens[0].equals("ARRIVE")){
customers[size] = new Customer(tokens[1]);
list.addCustomer(customers[size].getName());
stack.push(customers[size]);

if(stack.fullSeats()){
queue.add(customers[size]);
}
size++;
}

if(tokens[0].equals("LEAVE")){
removed.addCustomer(tokens[1]);

stack.clear();
stack.push(new Customer(tokens[1]));

// remove the same people from the queue
}
line = input.readLine();
}// end while
input.close();
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
//writeOutput(list, removed);
//System.out.println(list);
//System.out.println(removed);
}// end getInput()

public static void writeOutput(List list, List removed){
PrintWriter writer;

try{
writer = new PrintWriter(new FileWriter(outputFile));



writer.close();
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
}
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #4 of 8
You never close your writer on the first post. As long as the files are different names java should be able to read and write at the same time I am pretty sure I have done it before. I cannot test now as I am at work but if closing the writer does not fix the problem I will take a closer look at lunch time.
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
post #5 of 8
Thread Starter 
thank you so much. It was that i didnt do writer.close(); but now im confused i thought java did that by default even when you forget to put it in the code. Oh well thanks so much
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #6 of 8
Yeah, you have to close the file input when you're finished with it in C++.
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #7 of 8
Heres another tip. When doing this:

if(tokens[0].equals("ARRIVE"))

You should make "ARRIVE" a constant, and flip the order of the condition, so it's something like this

if(ARRIVE.equals(tokens[0]){}

That way if tokens[] or tokens [0] is null you won't throw a nullPointerException and your code can continue to process Little stuff like that will help you down the road when you get more complex...
Sweaty Box
(10 items)
 
Stacker T01 v5.0
(10 items)
 
Sturbin' Box
(13 items)
 
CPUMotherboardGraphicsRAM
i7 5960X ASUS Rampage V Extreme 2x Nvidia 1080ti SLI 32GB Corsair Dominator DDR4 3400Mhz 
Hard DriveCoolingOSMonitor
Samsung 850 pro NVMe M.2 Custom EK watercooled loop Windows 10 Pro Triple LG 4K 27" 
PowerCase
Cosair 1500Ti CaseLabs M10 
CPUMotherboardGraphicsRAM
Delidded i7 3770K @ 5Ghz ASUS Maximus V Gene EVGA GTX 670 SLI - Heatkiller Blocks 16GB Corsair Vengeance DDR3 @ 2400Mhz 
Hard DriveCoolingOSMonitor
Samsung 840 EVO Custom Loop - EK Supreme HF Windows 7 Pro 64-bit Dell 30" + 2 x Dell 24" 
PowerCase
Sparkle Gold 1200W CM Stacker T01 
CPUMotherboardGraphicsRAM
Phenom II X6 1055T @4.0GHz (308x13) / 3.08GHz NB ASUS Crosshair IV Formula w/EK full coverage block ATI 5870x2 Crossfired @ 950/1250 8GB Corsair Vengeance DDR3 1600 
Hard DriveCoolingOSMonitor
OCZ Vertex 3 SSD Swifttech Apogee Drive Block/Pump Windows 7 64bit Pro ASUS 27" LCD 
PowerCase
Corsair Professional Series Gold 650W  Lian-Li PC-650A 
  hide details  
Reply
Sweaty Box
(10 items)
 
Stacker T01 v5.0
(10 items)
 
Sturbin' Box
(13 items)
 
CPUMotherboardGraphicsRAM
i7 5960X ASUS Rampage V Extreme 2x Nvidia 1080ti SLI 32GB Corsair Dominator DDR4 3400Mhz 
Hard DriveCoolingOSMonitor
Samsung 850 pro NVMe M.2 Custom EK watercooled loop Windows 10 Pro Triple LG 4K 27" 
PowerCase
Cosair 1500Ti CaseLabs M10 
CPUMotherboardGraphicsRAM
Delidded i7 3770K @ 5Ghz ASUS Maximus V Gene EVGA GTX 670 SLI - Heatkiller Blocks 16GB Corsair Vengeance DDR3 @ 2400Mhz 
Hard DriveCoolingOSMonitor
Samsung 840 EVO Custom Loop - EK Supreme HF Windows 7 Pro 64-bit Dell 30" + 2 x Dell 24" 
PowerCase
Sparkle Gold 1200W CM Stacker T01 
CPUMotherboardGraphicsRAM
Phenom II X6 1055T @4.0GHz (308x13) / 3.08GHz NB ASUS Crosshair IV Formula w/EK full coverage block ATI 5870x2 Crossfired @ 950/1250 8GB Corsair Vengeance DDR3 1600 
Hard DriveCoolingOSMonitor
OCZ Vertex 3 SSD Swifttech Apogee Drive Block/Pump Windows 7 64bit Pro ASUS 27" LCD 
PowerCase
Corsair Professional Series Gold 650W  Lian-Li PC-650A 
  hide details  
Reply
post #8 of 8
Thread Starter 
that doesnt really help me here but i did it anyways so i wont forget thanks for the help and tips guys!!!! This is why i come here for help you are much more help than our so called comp sci "help center"
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming