Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › how would I read something into a 2D array which is seperated by gaps in java?
New Posts  All Forums:Forum Nav:

how would I read something into a 2D array which is seperated by gaps in java?

post #1 of 6
Thread Starter 
Hello again, well I ended up sorting out my issue regarding writing to a file from an array by doing it completely different and not actually using an array, and now the issue is how do I read from a text file into an array eg:

say I have an array:
int Tally; //Tally is some value that I know it doesn't really matter as all I care about is reading the line
String[][] MediaList = new String [Tally][4];

and I want to read each word which is separated by a space into an element in the array

eg (these are made up)

1 Fish action 2007
2 hell adventure 2004
3 minds drama 2009
//it can go on for however many I want.

this would look like
[1][fish][action][2007]
[2][hell][adventure][2004]
[3][minds][drama][2009]

how can I achieve this?
Edited by donthaveacow - 6/21/11 at 7:20am
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
post #2 of 6
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string s("1 Fish action 2007");
    istringstream iss(s);

        string sub,sub1,sub2,sub3;
        iss >> sub >> sub1 >> sub2 >> sub3;
        cout << sub << sub1 << sub2 << sub3 << endl;

    return 0;
}
source
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
post #3 of 6
Thread Starter 
Quote:
Originally Posted by newbie1911 View Post
[code]
sorry forgot to mention that this is in java.
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
post #4 of 6
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #5 of 6
http://download.oracle.com/javase/6/...l/Scanner.html

Hopefully this helps a bit, look at the methods for setting up delimiters and what not

(or do what was posted above; there are several ways to do it and Java isn't my primary language, so I would probably trust someone else's opinion haha; I'm used to the old fashion C way with strtok() ).
Edited by Tomiger - 6/21/11 at 7:47am
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
post #6 of 6
http://download.oracle.com/javase/1....va.lang.String)

Code:
   String example = "1 Fish action 2007"
   String[][] MediaList = new String[Tally][4];
   MediaList[0] = example.split(" ");
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › how would I read something into a 2D array which is seperated by gaps in java?