Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help needed reading selected data from text file (Java)
New Posts  All Forums:Forum Nav:

Help needed reading selected data from text file (Java)

post #1 of 9
Thread Starter 
Hey guys

I am working on a programming assignment it has 3 text files. I want to ask the user to input a customer number and have it validate it with the data from the customers text file.

Here is where I am stuck I do not know how to say if customer id does exist in the text file carry on if not display an error. I have learned how to read and write to a text file just not how to check against it.

Any help is much appreciated.
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS HD7770 Series Corsair 8GB 2x 2GB 
Hard DriveOptical DriveCoolingOS
segate none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS HD7770 Series Corsair 8GB 2x 2GB 
Hard DriveOptical DriveCoolingOS
segate none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
post #2 of 9
Read the text file into an array, a string/int element per line.
Then loop through array checking for the id.
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
post #3 of 9
Thread Starter 
Cool thanks mate I just got it going but now I want to read a specific line from the txt file or array list that starts with a certain number google search gave me a lot of results but none seemed to be what I was looking for. Any Ideas?
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS HD7770 Series Corsair 8GB 2x 2GB 
Hard DriveOptical DriveCoolingOS
segate none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS HD7770 Series Corsair 8GB 2x 2GB 
Hard DriveOptical DriveCoolingOS
segate none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
post #4 of 9
Quote:
Originally Posted by ipv89 View Post

Cool thanks mate I just got it going but now I want to read a specific line from the txt file or array list that starts with a certain number google search gave me a lot of results but none seemed to be what I was looking for. Any Ideas?

Can you give an example of the text file?

You could read a line to an string, and then check if its the right number, if so read the rest of the line.
else go to the next line and try again
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
post #5 of 9
Thread Starter 
The text file is like this.

1,Customer1
2,Customer2

ect

I want to be able to print a line that starts with a certain number
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS HD7770 Series Corsair 8GB 2x 2GB 
Hard DriveOptical DriveCoolingOS
segate none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS HD7770 Series Corsair 8GB 2x 2GB 
Hard DriveOptical DriveCoolingOS
segate none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
post #6 of 9
Yep, Are you familiar with loops?

So you have the line number you want to print num
You have the empty string for checking line

Loop through the file, assign the line text to line, check the string until the comma against (int)num
If this is true, print the String line
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
post #7 of 9
OR, if the file only contains these customer records Like this
from http://stackoverflow.com/questions/2312756/in-java-how-to-read-from-a-file-a-specific-line-given-the-line-number
Code:
FileInputStream fs= new FileInputStream("someFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for(int i = 0; i < 30; ++i)
  br.readLine();
String lineIWant = br.readLine();

Edited by jakethesnake438 - 10/14/13 at 12:57am
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
post #8 of 9
Thread Starter 
I have just tried that code all I get is Null. Ill try it they way you suggested. Thanks for your help mate its good to see another New Zealander on ocn.
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS HD7770 Series Corsair 8GB 2x 2GB 
Hard DriveOptical DriveCoolingOS
segate none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS HD7770 Series Corsair 8GB 2x 2GB 
Hard DriveOptical DriveCoolingOS
segate none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
post #9 of 9
Quote:
Originally Posted by ipv89 View Post

I have just tried that code all I get is Null. Ill try it they way you suggested. Thanks for your help mate its good to see another New Zealander on ocn.

That was just pseudo code, non compilable, just to get the idea.
You wouldn't happen to be first semester at Unitec would you?
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
3770/980ti
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.5GHZ Asus Z77M-pro EVGA 980Ti SC ACX 8GB 1600MHz 
Hard DriveHard DriveCoolingOS
256GB Samsung OEM SSD 128Gb Sandisk SSD Hyper 212 Evo Windows 10 
MonitorKeyboardMouseMouse Pad
Dell P2715Q 4K Apple Kbd Logitech G402 Steelseries QCK 
Audio
Senheiser CX215 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help needed reading selected data from text file (Java)