Take user input and search through 2D array and printout the result - java

I am trying to make a program that takes input from the user, searches through 2D array and prints out if the input matches data from the arrays. So, basically if the user types in VA, it should output Virginia. I am reading data from a Binary file that has 2 rows of data. The 1st row contains 2 letter abbreviations for all the states and the 2nd row contains the state names. For example: VA Virginia and in new line FL Florida and so on. Below is what I have so far. readStateFile() method works fine. I just need some help with getState method.
public static void main(String[] args) throws IOException {
try {
int age = getAge();
String[][] states = readStateFile();
String state = getState(states);
int ZIPCode = getZIPcode();
System.out.printf("\nAge:\t\t%d\n", age);
System.out.printf("Address:\t%s %s\n\n", ZIPCode, state);
System.out.println("Your survey is complete. " + "Your participation has been valuable.");
} catch (CancelledSurveyException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("Thank you for your time.");
}
}
private static String getState(String[][] states) throws IOException {
states = readStateFile();
String in = "";
String[][] abb;
abb = states;
System.out.println("Please enter the 2 letter state abbrevation or 'q' to quit: ");
Scanner st = new Scanner(System.in);
in = st.next();
if (in.equals("q")) {
System.out.println("Your survey was cancelled.\n" + "Thank you for your time.");
System.exit(0);
}
if (abb.equals(states)) {
for (int i = 0; states[0][i] != null; i++) {
if (abb.equals(states[0][i])) {
for (int state = 1; state <= 100; state++) {
System.out.println(states[0][i]);
}
}
}
} else {
System.out.println("You've entered invalid state abbrevation.");
}
return in;
}
private static String[][] readStateFile() throws IOException {
String states[][] = new String[50][50];
try {
FileInputStream fstream = new FileInputStream("states copy.bin");
DataInputStream inputFile = new DataInputStream(fstream);
for (int i = 0, j = i + 1; i < 50; i++) {
states[i][0] = inputFile.readUTF();
states[i][j] = inputFile.readUTF();
// System.out.println(states);
}
inputFile.close();
return states;
} catch (EOFException e) {
System.out.println("Survey Cancelled");
}
return states;
} ```

Instead of using a multidimensional array, it might be more helpful to use a HashMap.
Each abbreviation is used as a key, and the name of the state can be found using that key as a lookup. Illustrated below:
public static void main(final String[] args)
{
try
{
final Map<String, String> states = readStateFile();
// Display the contents of the file
// for (final Map.Entry<String, String> s : states.entrySet())
// {
// System.out.println(s.getKey() + " = " + s.getValue());
// }
final String state = getState(states);
final int age = getAge();
final int postalCode = getZIPcode();
System.out.println();
System.out.printf("Age:\t\t%d\n", Integer.valueOf(age));
System.out.printf("Address:\t%s %s\n\n", Integer.valueOf(postalCode), state);
System.out.println("Your survey is complete. Your participation has been valuable.");
}
catch (final IOException ex)
{
System.out.println(ex.getMessage());
}
System.out.println("Thank you for your time.");
}
private static String getState(final Map<String, String> states)
{
System.out.println("Please enter the 2 letter state abbrevation or 'q' to quit: ");
final StringBuilder sb = new StringBuilder();
try (final Scanner st = new Scanner(System.in))
{
final String stateAbbrev = st.next().toUpperCase(Locale.getDefault());
if ("Q".equals(stateAbbrev))
{
System.out.println("Your survey was cancelled." + System.lineSeparator() + "Thank you for your time.");
System.exit(0);
}
if (states.containsKey(stateAbbrev))
{
final String stateName = states.get(stateAbbrev);
sb.append(stateName);
}
else
{
System.out.println("You've entered an invalid state abbrevation: " + stateAbbrev);
}
}
return sb.toString();
}
private static Map<String, String> readStateFile() throws IOException
{
final List<String> lines = Files.readAllLines(Paths.get("C:/states copy.bin"));
// Get a list of items, with each item separated by any whitespace character
final String[] stateAbbrev = lines.get(0).split("\\s");
final String[] stateNames = lines.get(1).split("\\s");
final Map<String, String> states = new HashMap<>();
for (int i = 0; i < stateAbbrev.length; i++)
{
states.put(stateAbbrev[i], stateNames[i]);
}
return states;
}

Related

How to speed up code with Multithreading?

I have created a password cracker in Java that cracks passwords from a text file list. It then generates a dictionary that contains the following pairs: the word hashed and the original word. I am looking for a way to speed up the program as having it read all of the words from the file and then use multithreading to generate the hashes. How can I break up the list of words so that it is in four separate partitions that I can then have multiple threads operate on in the createDictionary method? Here is what I have so far:
public class Main {
private static final String FNAME = "words.txt";
private final static String PASSWDFNAME = "passwd.txt";
private static Map<String, String> dictionary = new HashMap<>();
public static void main(String[] args) {
// Create dictionary of plain / hashed passwords from list of words
System.out.println("Generating dictionary ...");
long start = System.currentTimeMillis();
createDictionary(FNAME);
System.out.println("Generated " + dictionary.size() + " hashed passwords in dictionary");
long stop = System.currentTimeMillis();
System.out.println("Elapsed time: " + (stop - start) + " milliseconds");
// Read password file, hash plaintext passwords and lookup in dictionary
System.out.println("\nCracking password file ...");
start = System.currentTimeMillis();
crackPasswords(PASSWDFNAME);
stop = System.currentTimeMillis();
System.out.println("Elapsed time: " + (stop - start) + " milliseconds");
}
private static void createDictionary(String fname) {
// Read in list of words
List<String> words = new ArrayList<>();
try (Scanner input = new Scanner(new File(fname));) {
while (input.hasNext()) {
String s = input.nextLine();
if (s != null && s.length() >= 4) {
words.add(s);
}
}
} catch (FileNotFoundException e) {
System.out.println("File " + FNAME + " not found");
e.printStackTrace();
System.exit(-1);
}
// Generate dictionary from word list
for (String word : words) {
generateHashes(word);
}
}
private static void crackPasswords(String fname) {
File pfile = new File(fname);
try (Scanner input = new Scanner(pfile);) {
while (input.hasNext()) {
String s = input.nextLine();
String[] t = s.split(",");
String userid = t[0];
String hashedPassword = t[1];
String password = dictionary.get(hashedPassword);
if (password != null) {
System.out.println("CRACKED - user: "+userid+" has password: "+password);
}
}
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
System.exit(-1);
}
}
private static void generateHashes(String word) {
// Convert word to lower case, generate hash, store dictionary entry
String s = word.toLowerCase();
String hashedStr = HashUtils.hashPassword(s);
dictionary.put(hashedStr, s);
// Capitalize word, generate hash, store dictionary entry
s = s.substring(0, 1).toUpperCase() + s.substring(1);
hashedStr = HashUtils.hashPassword(s);
dictionary.put(hashedStr, s);
}
}
It's very simple, check this out:
public static void main(String[] args) {
List<String> words = new ArrayList<>();
List<Thread> threads = new ArrayList<>();
int numThreads = 4;
int threadsSlice = words.size() / numThreads;
for(int i = 0; i < numThreads; i++) {
Thread t = new Thread(new WorkerThread(i * threadsSlice, (i + 1) * threadsSlice, words));
t.start();
threads.add(t);
}
threads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
static class WorkerThread implements Runnable {
private final int left;
private final int right;
private final List<String> words;
public WorkerThread(int left, int right, List<String> words) {
this.left = left;
this.right = right;
this.words = words;
}
#Override
public void run() {
for (int i = left; i < right; i++) {
generateHashes(words.get(i));
}
}
}
This code is creating 4 threads, each one scanning one partition of your list, and applying the generateHashes method on all the words in the partition.
You can put the words in the heap memory to avoid passing it to each thread via constructor param.
Also make sure to use a ConcurrentMap for your dictionary in the generateHashes method

Reading file and store in vector

I'm reading from the file:
name1 wordx wordy passw1
name2 wordx wordy passw2
name3 wordx wordy passw3
name (i) wordx wordy PASSW (i)
x
x word
x words
words
x
words
At the moment I can print line by line:
Line 1: name1 wordx wordy passw1
Line 2: name2 wordx wordy passw2
I plan to have access to:
users [0] = name1
users [1] = name2
users [2] = name3
..
passws [0] = passw1
passws [1] = passw2
passws [2] = passw3
..
My code is:
public static void main(String args[]) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("C:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (inFile.hasNextLine()) {
list.add(inFile.nextLine()+",");
}
String listString = "";
for (String s : list) {
listString += s + "\t";
}
String[] parts = listString.split(",");
System.out.println("Line1: "+ parts[0]);
}
How do I get the following output:
User is name1 and password is passw1
User is name32 and password is passw32
Thanks in advance.
Something like this will do:
public static void main(String args[]) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("C:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (inFile.hasNextLine()) {
list.add(inFile.nextLine());
}
int line = 0;
String[] parts = list.get(line).split(" ");
String username = parts[0];
String pass = parts[3];
System.out.println("Line" + (line + 1) + ": " + "User is " + username +" and password is " + pass);
}
EDIT: if you want to iterate through all lines just put last lines in a loop:
for (int line = 0; line < list.size(); line++) {
String[] parts = list.get(line).split(" ");
String username = parts[0];
String pass = parts[3];
System.out.println("Line" + (line + 1) + ": " + "User is " + username +" and password is " + pass);
}
First thing to do is, to add this loop to the end of your code :
for(int i = 0; i <= parts.length(); i++){
System.out.println("parts["+i+"] :" + parts[i] );
}
that will simply show the result of the split using ,.
Then adapt your code, you may want to use another regex to split() your lines, for instance a space.
String[] parts = listString.split(" ");
for documentation about split() method check this.
If you want to get that output then this should do the trick:
public static void main(String args[]) throws FileNotFoundException, IOException {
Scanner inFile = null;
try {
inFile = new Scanner(new File("F:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Map<String, String> userAndPassMap = new LinkedHashMap<>();
while (inFile.hasNextLine()) {
String nextLine = inFile.nextLine();
String[] userAndPass = nextLine.split(" ");
userAndPassMap.put(userAndPass[0], userAndPass[1]);
}
for (Map.Entry<String, String> entry : userAndPassMap.entrySet()) {
System.out.println("User is:" + entry.getKey() + " and password is:" + entry.getValue());
}
}
By storing in a map you are linking directly each username with its password. If you need to save them into separate arrays then you can do this in the while loop instead:
List<String> users = new LinkedList<>(),passwords = new LinkedList<>();
while (inFile.hasNextLine()) {
String nextLine = inFile.nextLine();
String[] userAndPass = nextLine.split(" ");
users.add(userAndPass[0]);
passwords.add(userAndPass[1]);
}
and later transform them to arrays
users.toArray()
I recommend you use a java.util.Map, a standard API which allows you to store objects and read each one of them by a key. (In your case, string objects indexed by string keys). Example:
Let's assume this empty map:
Map<String, String> map=new HashMap<String,String>();
If you store this:
map.put("month", "january");
map.put("day", "sunday");
You can expect that map.get("month") will return "january", map.get("day") will return "sunday", and map.get(any-other-string) will return null.
Back to your case: First, you must create and populate the map:
private Map<String, String> toMap(Scanner scanner)
{
Map<String, String> map=new HashMap<String, String>();
while (scanner.hasNextLine())
{
String line=scanner.nextLine();
String[] parts=line.split(" ");
// Validation: Process only lines with 4 tokens or more:
if (parts.length>=4)
{
map.put(parts[0], parts[parts.length-1]);
}
}
return map;
}
And then, to read the map:
private void listMap(Map<String,String> map)
{
for (String name : map.keySet())
{
String pass=map.get(name);
System.out.println(...);
}
}
You must include both in your class and call them from the main method.
If you need arbitraray indexing of the read lines, use ArrayList:
First, define a javabean User:
public class User
{
private String name;
private String password;
// ... add full constructor, getters and setters.
}
And then, you must create and populate the list:
private ArrayList<User> toList(Scanner scanner)
{
List<User> list=new ArrayList<User>();
while (scanner.hasNextLine())
{
String line=scanner.nextLine();
String[] parts=line.split(" ");
// Validation: Process only lines with 4 tokens or more:
if (parts.length>=4)
{
list.add(new User(parts[0], parts[parts.length-1]));
}
}
return list;
}

inefficient looping in java

This is my csv data:
Name,Code,Price,Colour,Type,Stock
A,1001,35000,Red,Car Paint,54
B,1002,56000,Blue,House Paint,90
As you can see, my coding is inefficient.
This is because all the textfields in netbeans do not allow same variable names, I have to give different variable names to each text field (Example: code1, code2, code3, name1, name2,name3)
Can someone help me on how to loop this data so they do it four times and i dont have to repeat the coding? and to skip the process if the fields are blank.
The following is my coding:
try
{
for(int z=0; z<4;z++)
{
String code1;
code1=this.text1.getText();
System.out.println("this is the code: " + code1);
String qty;
int qty1;
qty=this.quantity1.getText();
qty1=Integer.parseInt(qty);
System.out.println("quantity: "+qty1);
String code2;
code2=this.text2.getText();
System.out.println("this is the code: " + code2);
int qty2;
qty=this.quantity2.getText();
qty2=Integer.parseInt(qty);
System.out.println("quantity: "+qty2);
String code3;
code3=this.text3.getText();
System.out.println("this is the code: " + code3);
int qty3;
qty=this.quantity2.getText();
qty3=Integer.parseInt(qty);
System.out.println("quantity: "+qty3);
String code4;
code4=this.text4.getText();
System.out.println("this is the code: " + code4);
int qty4;
qty=this.quantity2.getText();
qty4=Integer.parseInt(qty);
System.out.println("quantity: "+qty4);
int sum=0;
BufferedReader line = new BufferedReader(new FileReader(new File("C:\\Users\\Laura Sutardja\\Documents\\IB DP\\Computer Science HL\\cs\\product.txt")));
String indata;
ArrayList<String[]> dataArr = new ArrayList<>();
String[] club = new String[6];
String[] value;
while ((indata = line.readLine()) != null) {
value = indata.split(",");
dataArr.add(value);
}
for (int i = 0; i < dataArr.size(); i++) {
String[] nameData = dataArr.get(i);
if (nameData[1].equals(code1)) {
System.out.println("Found name.");
name1.setText(""+ nameData[0]);
int price;
price=Integer.parseInt(nameData[2]);
int totalprice=qty1*price;
String total=Integer.toString(totalprice);
price1.setText(total);
sum=sum+totalprice;
break;
}
}
for (int i = 0; i < dataArr.size(); i++) {
String[] nameData = dataArr.get(i);
if (nameData[1].equals(code2)) {
System.out.println("Found name.");
name2.setText(""+ nameData[0]);
int price;
price=Integer.parseInt(nameData[2]);
int totalprice=qty2*price;
String total=Integer.toString(totalprice);
price2.setText(total);
sum=sum+totalprice;
break;
}
}
for (int i = 0; i < dataArr.size(); i++) {
String[] nameData = dataArr.get(i);
if (nameData[1].equals(code3)) {
System.out.println("Found name.");
name3.setText(""+ nameData[0]);
int price;
price=Integer.parseInt(nameData[2]);
int totalprice=qty3*price;
int totalprice3=totalprice;
String total=Integer.toString(totalprice);
price3.setText(total);
sum=sum+totalprice;
break;
}
}
for (int i = 0; i < dataArr.size(); i++) {
String[] nameData = dataArr.get(i);
if (nameData[1].equals(code4)) {
System.out.println("Found name.");
name4.setText(""+ nameData[0]);
int price;
price=Integer.parseInt(nameData[2]);
int totalprice=qty4*price;
int totalprice4=totalprice;
String total=Integer.toString(totalprice);
price4.setText(total);
sum=sum+totalprice;
break;
}
}
total1.setText("Rp. "+sum);
}
}
catch ( IOException iox )
{
System.out.println("Error");
}
Why don't you use a library like http://commons.apache.org/proper/commons-csv/
Solving this problem is actually rather straight forward if you break it down into separate parts.
First you need to solve the problem of loading the data into an internal data representation that is easy to use. Just loading the file into Java is rather simple and you have already done this:
BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
String line = "start";
int count = 0;
while((line = csvFile.readLine()) != null){
System.out.println(line);
}
csvFile.close();
The next problem is splitting the line and store it in a meaningful way - for each line.
HashMap<Integer, String> record = new HashMap<Integer, String>();
String[] raw = line.split(",");
for(int i=0;i<raw.length; i++){
record.put(i, raw[i]);
}
Now you state you only want to store records that have non-empty fields so we need to check for that:
HashMap<Integer, String> record = new HashMap<Integer, String>();
String[] raw = line.split(",");
Boolean store = true;
for(int i=0;i<raw.length; i++){
if(raw[i].equals("") || raw[i].equals(null)){
store = false;
break;
}
record.put(i, raw[i]);
}
if(store)
csvData.add(record);
Now, you can load each record of the csv file as a dictionary that you can easily use. All that remains is to save a list of these dictionaries.
ArrayList<Map<Integer, String>> csvData = new ArrayList<Map<Integer, String>>();
BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
String line = "start";
int count = 0;
while((line = csvFile.readLine()) != null){
if(count == 0){//skip first line
count++;
continue;
}
HashMap<Integer, String> record = new HashMap<Integer, String>();
String[] raw = line.split(",");
Boolean store = true;
for(int i=0;i<raw.length; i++){
if(raw[i].equals("") || raw[i].equals(null))
{
store = false;
break;
}
record.put(i, raw[i]);
}
if(store)
csvData.add(record);
}
csvFile.close();
Full code snippet that loads in data and easily access whatever information you want:
public class Main {
public static final int NAME = 0;
public static final int CODE = 1;
public static final int PRICE = 2;
public static final int COLOR = 3;
public static final int TYPE = 4;
public static final int STOCK = 5;
public static void main(String[] args) throws IOException{
ArrayList<Map<Integer, String>> csvData = loadCSVFile("C:\\path\\to\\file\\products.txt");
//Print some of the data
System.out.println("---------------------------");
for(Map<Integer, String> record : csvData){
printInfo(record);
}
}
public static ArrayList<Map<Integer, String>> loadCSVFile(String path) throws IOException{
ArrayList<Map<Integer, String>> csvData = new ArrayList<Map<Integer, String>>();
BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
String line = "start";
int count = 0;
while((line = csvFile.readLine()) != null){
if(count == 0){
count++;
continue;
}
HashMap<Integer, String> record = new HashMap<Integer, String>();
String[] raw = line.split(",");
Boolean store = true;
for(int i=0;i<raw.length; i++){
if(raw[i].equals("") || raw[i].equals(null))
{
store = false;
break;
}
record.put(i, raw[i]);
}
if(store)
csvData.add(record);
}
csvFile.close();
return csvData;
}
public static void printInfo(Map<Integer, String> record){
System.out.println(record.get(CODE) + " : " + record.get(TYPE));
System.out.println(record.get(NAME) + " : " + record.get(STOCK) + " : " + record.get(PRICE));
System.out.println("---------------------------");
}
}

How do I print out my Hashmap in Java

Okay so I have a hashmap of the class Stock, which basically has data about different stocks from yahoo that a user can enter. Each time they enter a new stock, I add that Stock class to the hashmap, and I don't know how to print out the whole hashmap and display everything entered so far
public class AS4stocks {
static Map<String, Stock> mappin = new HashMap<String, Stock>();
public static void main(String[] args) throws IOException {
int menuchoice;
do {
Scanner in1 = new Scanner(System.in);
System.out
.println("What would you like to do \n1) Print table\n2) Add a stock\n3) Do something else");
menuchoice = in1.nextInt();
switch (menuchoice) {
case 1:
System.out.println(mappin);
break;
case 2:
System.out.print("Enter the stock's ticker symbol\n");
String ticker = in1.next();
addstock(ticker);
break;
case 3:
break;
}
} while (menuchoice != 0);
}
private static void Printtable(Map<String, Stock> mappin) {
fo
}
private static void addstock(String ticker) throws IOException {
URL url = new URL("http://download.finance.yahoo.com/d/quotes.csv?s="
+ ticker + "&f=snd1ohgpvwm3m4&e=.csv");
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
Object[] otable = new String[11];
int counter = 0;
while ((line = br.readLine()) != null) {
String str = line;
StringTokenizer st = new StringTokenizer(str, ",\"");
while (st.hasMoreTokens()) {
String adder = st.nextToken();
otable[counter] = adder;
if (counter == 0) {
System.out.println("-------------------" + adder
+ "-------------------");
System.out.print("Ticker: ");
}
if (counter == 2) {
System.out.print("Company: ");
}
if (counter == 3) {
System.out.print("Open: ");
}
if (counter == 4) {
System.out.print("High: ");
}
if (counter == 5) {
System.out.print("Low: ");
}
if (counter == 6) {
System.out.print("Close: ");
}
if (counter == 7) {
System.out.print("Volume: ");
}
if (counter == 8) {
System.out.print("52 Week Range: ");
}
if (counter == 9) {
System.out.print("50 Day Average: ");
}
if (counter == 10) {
System.out.print("200 Day Average: ");
}
System.out.println(adder);
counter++;
}
Stock snew = new Stock(otable);
mappin.put(ticker, snew);
}
System.out.println();
}
static class Stock {
String compname;
String ticker;
String date;
String open;
String high;
String low;
String close;
String volume;
String range;
String average50;
String average200;
public Stock(Object otable[]) {
compname = (String) otable[0];
ticker = (String) otable[1];
date = (String) otable[2];
open = (String) otable[0];
high = (String) otable[1];
low = (String) otable[2];
close = (String) otable[3];
volume = (String) otable[4];
range = (String) otable[5];
average50 = (String) otable[6];
average200 = (String) otable[7];
}
}
}
You'll need to iterate through the map and print what you want for each entry. Easiest way to iterate through the values would be:
for (Stock stock : mappin.values()) {
System.out.println(stock.toString());
}
This assumes of course your Stock class has meaningful output for toString()

method to read array list to find certain string and value

public class array {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));
System.out.println("enter the fruit you want to search");
Scanner input = new Scanner(System.in);
String fruit = input.nextLine();
String line;
List<String> list = new ArrayList<String>();
while((line=reader.readLine()) !=null)
{
list.add(line);
}
reader.close();
for (String s : list) {
System.out.println(s);
}
}
}
I have fruit.txt
apple 20 good
orange 30 good
banana 40 needmore
how do I retrieve how many oranges I have from the array list.
I want the program to read the user input in this case "orange" and display out 30 and the status is not good.
ideal output is
You have orange 30 of them and status is good
Try the following updated class.
public class array
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));
System.out.println("enter the fruit you want to search");
Scanner input = new Scanner(System.in);
String fruit = input.nextLine();
String line;
boolean found = false;
int count = 0;
List<String> list = new ArrayList<String>();
while ((line = reader.readLine()) != null)
{
String[] items = line.split(" ");
if (fruit.equals(items[0]))
{
found = true;
count = Integer.parseInt(items[1]);
break;
}
list.add(line);
}
reader.close();
if (found)
{
System.out.println("You have " + fruit + " " + count + " of them and status is good");
}
}
}
You need to split your Strings in your List, and then print each elements of your array obtained within your specified string format: -
for (String s : list) {
String[] tokens = s.split(" ");
if (tokens[0].equals(fruit)) {
System.out.println("You have " + tokens[0] + " " + tokens[1] +
" of them and status is " + tokens[2]);
break;
}
}
Or, you can use: -
System.out.format("You have %s %s of them and status is %s",
tokens[0], tokens[1], tokens[2]);
You will need to split up the lines into the three fields using a StringTokenizer. Then I would create a new class to hold that information.
When you read a line, split the values into String array like this:
while((line=reader.readLine()) !=null)
{
String [] values = line.split(" ");
list.add("You have "+values[0] + " " + values[1] " of them and status is "+values[2] );
}
Not tested but should work, try:
public class array {
public static class Fruit {
private String name;
private String count;
private String status;
public Fruit(String name, String count, String status) {
this.name = name;
this.count = count;
this.status = status;
}
public String getName() {
return name;
}
public String getCount() {
return count;
}
public String getStatus() {
return status;
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));
System.out.println("enter the fruit you want to search");
Scanner input = new Scanner(System.in);
String fruit = input.nextLine();
String line= "";
HashMap<String, Fruit> map = new HashMap<String, Fruit>();
while ((line = reader.readLine()) != null) {
String[] strings = line.split(" ");
map.put(strings[0], new Fruit(strings[0], strings[1], strings[2]));
}
reader.close();
System.out.print("You have " + fruit + " " + map.get(fruit).getCount() + " of them and status is: " + map.get(fruit).getStatus());
}
}

Categories