New Posts  All Forums:Forum Nav:

Java File I/O Help - Page 2

post #11 of 30
Thread Starter 
Another question...

How do I create a dynamic array?

I need to be able to have an unlimited amount of data come in from this file, and even more possibly added. So I need a dynamic array.
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
post #12 of 30
Quote:
Originally Posted by pauldovi View Post
Another question...

How do I create a dynamic array?

I need to be able to have an unlimited amount of data come in from this file, and even more possibly added. So I need a dynamic array.
I actually know this one. Don't specify an array size and it will be dynamic.
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
post #13 of 30
Just use an ArrayList- You can grow/shrink it as you need more/less space.

array.add(whateverYouWant);

array.remove(integer position);
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Reply
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Reply
post #14 of 30
Thread Starter 
Another question

I am trying to access data from another class.

I have the method setXYCood in the class AddComponents. I have an instance of AddComponents called bottomDisplay.

When I try:

Code:
bottomDisplay.setXYCoord(e.getX(),e.getY());
bottomDisplay is underlined in the error status and says:

bottomDisplay cannot be resolved
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
post #15 of 30
Quote:
Originally Posted by pauldovi View Post
Another question

I am trying to access data from another class.

I have the method setXYCood in the class AddComponents. I have an instance of AddComponents called bottomDisplay.

When I try:

Code:
bottomDisplay.setXYCoord(e.getX,e.getY());
bottomDisplay is underlined in the error status and says:

bottomDisplay cannot be resolved
You forgot the parenthesis for e.getX().
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
post #16 of 30
Thread Starter 
Quote:
Originally Posted by Licht View Post
You forgot the parenthesis for e.getX().
They are there in the code.

The issue is with the reference to "bottomDisplay".
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
post #17 of 30
Code:
public void readData(String filename) {
        
    BufferedReader in = new BufferedReader(new FileReader(filename)); // create a reader from a given file
       
    String lineFromFile = in.readLine();    // read a line from file

    String[] temp = lineFromFile.split(" "); // split the line into an array of strings with the separator being " ".
}
Basically this method gives you access to the file you specify, reads in a single line, and lets you break up that line into data elements seperated by whatever you want. You could could use lineFromFile.split("\") if your file was tab delimited or whatever other separator you want. After things are neatly seperated into a string[], you can just use Integer.parseInt(temp[i]); or a similar structure to extract all your values. Those are the basic methods that will allow you to do it.

Code:
public void readData(String filename) {
        
    BufferedReader in = new BufferedReader(new FileReader(filename)); // create a reader from a given file
       
    String lineFromFile = in.readLine();    // read a line from file

    String[] temp = lineFromFile.split(" "); // split the line into an array of strings with the separator being " ".
}
Basically this method gives you access to the file you specify, reads in a single line, and lets you break up that line into data elements seperated by whatever you want. You could could use lineFromFile.split("\") if your file was tab delimited or whatever other separator you want. After things are neatly seperated into a string[], you can just use Integer.parseInt(temp[i]); or a similar structure to extract all your values. Those are the basic methods that will allow you to do it.

Edit: As far as the reference not showing up, are you using everything in the same package? Otherwise you may need to import that class up top, or use the full path like System.out.println();
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #18 of 30
Thread Starter 
Quote:
Originally Posted by C-bro View Post
Edit: As far as the reference not showing up, are you using everything in the same package? Otherwise you may need to import that class up top, or use the full path like System.out.println();
It is all in the same project, package, folder in Eclipse.

What I have:

I have class StartGUI which contains the main method. Within the main method I create instance of class AddComponents called bottomDisplay and I create an instance of ScrollablePicture called mapWindow. I try and set xCoord and yCoord within bottomDisplay (instance of AddComponent) using the setXYCoord method in AddCompont from the ScrollablePicture class.

So:

Code:
public class StartGUI extends JFrame
{
     public void main(String[] args)
     {
          AddComponents bottomDisplay = new AddComponents();
          ScrollablePicture mapWindow = new ScrollablePicture();
     }
}

public class AddComponents extends JPanel
{
     int xCoord;
     int yCoord;

     public void setXYCoord(int x, int y)
     {
          xCoord = x;
          yCoord = y;
     }
}

public class ScrollablePicture extends JLabel
                                       inplements Scrollable,
                                                      MouseMotionListener
{
     public void mouseMoved(MouseEvent e)
     {
          bottomDisplay.setXYCoord(e.geyX(),e.getY());
     }
}
If I try and do the same bottomDisplay.setXYCoord(0,0) from main it works (recognizes bottomDisplay). But not from ScrollablePicture.
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
post #19 of 30
Quote:
Originally Posted by pauldovi View Post

This is the exact reason why applications are so bloated. High level languages like Java where the programmer doesn't have a clue what is going on, the language is creating objects and calling methods implicitly, and pointers are not used because they are to complicated.
Pointer are used, all objects are actually pointers in Java.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
post #20 of 30
Thread Starter 
Quote:
Originally Posted by pauldovi View Post
It is all in the same project, package, folder in Eclipse.

What I have:

I have class StartGUI which contains the main method. Within the main method I create instance of class AddComponents called bottomDisplay and I create an instance of ScrollablePicture called mapWindow. I try and set xCoord and yCoord within bottomDisplay (instance of AddComponent) using the setXYCoord method in AddCompont from the ScrollablePicture class.

So:

Code:
public class StartGUI extends JFrame
{
     public void main(String[] args)
     {
          AddComponents bottomDisplay = new AddComponents();
          ScrollablePicture mapWindow = new ScrollablePicture();
     }
}

public class AddComponents extends JPanel
{
     int xCoord;
     int yCoord;

     public void setXYCoord(int x, int y)
     {
          xCoord = x;
          yCoord = y;
     }
}

public class ScrollablePicture extends JLabel
                                       inplements Scrollable,
                                                      MouseMotionListener
{
     public void mouseMoved(MouseEvent e)
     {
          bottomDisplay.setXYCoord(e.geyX(),e.getY());
     }
}
If I try and do the same bottomDisplay.setXYCoord(0,0) from main it works (recognizes bottomDisplay). But not from ScrollablePicture.
Is this ^^^ possible to do?
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming