Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java, I am reading a txt file through command line containing strings followe

ID: 3747691 • 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.

I need help in figuring out, if a name is in the text file with no number, i want to check to see if that name already exists. If it does print the: loaction name and number already in that spot.

If the name doesnt exist in array print f name doesnt exist

Right now the number variable wont compile if there is nothing in words[1]

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

If you have any doubts, please give me comment...

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 = -1, key;

while ((st = br.readLine()) != null) {

words = st.split(" ");

name = words[0];

if(words.length==2)

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();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote