In Java, I am reading a txt file through command line containing strings followe
ID: 3747722 • Letter: I
Question
In Java, I am reading a txt file through command line containing strings followed by a optional number. I am hashing to array based of string ascii value.
The output:
For every name and number it prints location in array , name and number
If a duplicate name and number comes up it will display and appropiate message.
Where I need help: if a name is read without a number I want to first check to see if its key is in the array already, If so display what is in that index: location name and number.
If the name with no number does not exist I want it to display message doesnot exist.
ie. txt file
sarah 23
bob 2
billy 13
billy 13
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class Sys {
public static int getHash(String name) {
int total = 0;
for(int i = 0; i < name.length(); ++i) {
total += name.charAt(i);
}
return total;
}
public static void main(String[] args) throws Exception {
String inpfile = args[0];
FileReader fr = new FileReader(inpfile);
BufferedReader br = new BufferedReader(fr);
String st, inputStrings = "";
System.out.println("Reading strings from file...");
int count = 0;
.
while ((st = br.readLine()) != null) {
count++;
}
br.close();
br = new BufferedReader(new FileReader(inpfile));
String[] names = new String[2*count];
int[] numbers = new int[2*count];
String words[], name;
int number, key;
while ((st = br.readLine()) != null) {
words = st.split(" ");
name = words[0];
number = Integer.parseInt(words[1]);
key = getHash(name) % names.length;
if (names[key] == null) {
names[key] = name;
numbers[key] = number;
System.out.printf("Location: %d, Name: %s and number: %d ", key, names[key], numbers[key]);
}else{
names[key] = name; //that name and assigns to that location in array
numbers[key] = number;//adds number to location in array
System.out.printf("Already exists at: " + "Location: %d, Name: %s and number: %d ", key, names[key], numbers[key]);
}
}
br.close();
}
}
Explanation / Answer
import java.io.BufferedReader; import java.io.FileReader; import java.util.Scanner; public class Sys { public static int getHash(String name) { int total = 0; for(int i = 0; i 1) count++; } br.close(); br = new BufferedReader(new FileReader(inpfile)); String[] names = new String[2*count]; int[] numbers = new int[2*count]; String words[], name; int number, key; while ((st = br.readLine()) != null) { words = st.split(" "); name = words[0]; key = getHash(name) % names.length; if(words.length == 1) { if(names[key] == null) { System.out.println(name + " does not exists"); } else { System.out.printf("Location: %d, Name: %s and number: %d ", key, names[key], numbers[key]); } } else { number = Integer.parseInt(words[1]); if (names[key] == null) { names[key] = name; numbers[key] = number; System.out.printf("Location: %d, Name: %s and number: %d ", key, names[key], numbers[key]); } else { names[key] = name; //that name and assigns to that location in array numbers[key] = number;//adds number to location in array System.out.printf("Already exists at: " + "Location: %d, Name: %s and number: %d ", key, names[key], numbers[key]); } } } br.close(); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.