I have,
1. I have an xml file consists of individuals info.
2. Hash map that reads the xml-file.
3. Scanner that lets the user gives an individual name(just the name), so that code compare with the information stored in the has map if i matches ant then then the code prints that individual information(name, phone etc).
I face an issue with the looping part, can you help to get to work ?
From my Main class:
System.out.println("Please enter a name:");
Scanner scan = new Scanner(System.in);
Person c1 = new Person();
c1.setName(scan.nextLine());
String value = c1.getName();
c1.getTimeInfo(value);
From my Person class:
public void getPersonInfo(String value)
{
List<Person> t = persons.get(name);
int iD ;
value = t.get(0);
for(int i = 0; i < t.length(); ++i) {
if(t.get(i) == value) {
value = t.get(i);
this.iD = i;
}
System.out.println("The person info : " + this.name.get(iD) + "-" +this.phone.get(iD) + " "+ this.address.get(iD)+ "-" + this.title,get(iD));
The way your getPersonInfo() method functions now is to return the last person in the names list. This is because you reassign the input value to t.get(0), and then loop through the list. This will result in the last name always being returned. You can do without having a variable for this comparison, as you don't use it in your output. Also, you should never use == for comparing strings, use .equals() instead, like so:
public void getPersonInfo(String value){
List<Person> t = persons.get(name);
int iD ;
for(int i = 0; i < t.length(); ++i) {
if(t.get(i).equals(value)) {
this.iD = i;
}
System.out.println("The person info : " + this.name.get(iD) + "-" +this.phone.get(iD)
}
}
This still does not prevent nothing from happening if the input value is not in the name list, so you may want to take care of that case. However, this will prevent you from always printing the information about the last person in the names list.
Also, you are comparing a Person to a string in the if- statement in the for loop, so either change the input to a Person or the list of people to a list of strings.
Related
I have to make a Java Program, where a user type in the total numbers of students, so I made this code:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int numReaders = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of magazin readers:");
numReaders = scan.nextInt();
Now, after adding the total number of students, we should add their names into an array:
//Creating an array of names, where the length is the total number entered by the user
String[] nameStr = new String[numReaders];
int[] ages = new int[numReaders];
for(int i=0; i<numReaders; i++)
{
Scanner n = new Scanner(System.in);
System.out.println("Enter the name of reader: "+i);
nameStr[i] = n.next();
}
After that, we should add correspondingly the age of each name, so I made this portion of code:
for(int i=0; i<numReaders; i++)
{
Scanner a = new Scanner(System.in);
System.out.println("Enter the age of reader: "+i);
ages[i] = a.nextInt();
}
//Display the results
System.out.println("Number of readers is: "+numReaders);
for (int i=0; i<numReaders; i++)
{
System.out.println("The name of reader "+i+" is "+nameStr[i]+ " and his age is "+ages[i]);
}
After making this code, I tested it using Ideone and Command Prompt and it works properly:
Now, I need to call method according to selection of the user:
if he typed 'a' a method should be called to specify the name and the age of the oldest student.
If he typed 'b' a method called to see how many students have an age specified by the user and If he typed 'c', a function called to calculate the average age of them all.
I am new to methods so I don't know how to add arrays into methods and make statements.
Here is the full code:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int numReaders = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of magazin readers:");
numReaders = scan.nextInt();
//Creating an array of names, where the length is the total number entered by the user
String[] nameStr = new String[numReaders];
int[] ages = new int[numReaders];
for(int i=0; i<numReaders; i++)
{
Scanner n = new Scanner(System.in);
System.out.println("Enter the name of reader: "+i);
nameStr[i] = n.next();
}
for(int i=0; i<numReaders; i++)
{
Scanner a = new Scanner(System.in);
System.out.println("Enter the age of reader: "+i);
ages[i] = a.nextInt();
}
//Display the results
System.out.println("Number of readers is: "+numReaders);
for (int i=0; i<numReaders; i++)
{
System.out.println("The name of reader "+i+" is "+nameStr[i]+ " and his age is "+ages[i]);
}
//Choosing a statistic
//if a:
System.out.println("Please choose a, b or C:");
Scanner stat = new Scanner(System.in);
char X;
X = stat.next().charAt(0);
if(X=='a')
System.out.println(X+X);
else if(X=='b')
//System.out.println(X);
//Scanner newAge = new Scanner(System.in);
//int ageToSearchFor = newAge.nextInt();
//maxAge(ageToSearchFor);
else
System.out.println(X);
}
}
Right, so to start with your user enters an input, for example 'a', so let's go with this:
Firstly, you need to create the method where the name of the oldest student is displayed, so let's call it 'getOldestStudent' - when naming methods this is the typical naming convention, starting lowercase and then moving to uppercase for each new word - try and make them as intuitive as possible.
When making the method signature, you need to give it its visibility and also what it is going to return. In this case, as you are only using one class, we will give it private, so it is only visible by this class.
Now we need to return 2 things, so we can either put these into a string or put them into an array, which is what I would recommend, so we are going to return an array. However, you want to input an array to search through, so this goes in tbe brackets as parameters (or arguments). Therefore our method signature is the following:
private String[] getOldestStudent(String[] students, int[] ages)
Then inside this method, you can simply do the code you need to find the oldest student, add their name and age to the array and then return this.
Need anymore help just drop a comment.
On a side note, you would have been better off creating a 'Student' object and then giving this object a 'name' property and an 'age' property and then simply making an array of students and getters and setters (or accessors and mutators) for these properties.
James Lloyd's covers your question pretty well, I thought I might add some input as I think you are struggling with some principles.
At first, I would do as James advised and create a class Student that stores the values for each person.
public class Student {
public String name;
public int age;
// Constructors allow you to create a new Object and set some variables
// when you create it.
public Student (String name) {
this.name = name;
}
}
I used public to avoid getters and setters for this explanation, but I'd use private most had I to write it by myself.
Anyways, that way you only have to use one instead of two arrays (and name and age are connected with each other, e.g., you know the age of a student you know the name of, whereas with two different arrays it could happen that you don't know if nameArray[0] belongs to ageArray[0].
So you have an array Student[] students = new Student[numReaders]; and you can set each Student after reading the input, i.e., after reading the name you call students[i] = new Student(name); If you want to set the age of a Student afterwards you can do so by using student[i].age = age.
Now that we have filled our array, we can advance to your actual question.
char method;
method = stat.next().charAt(0);
// I think switch is a little easier to read for such cases
switch(method) {
case 'a': Student oldest = getOldestStudent(students);
if (oldest != null)
System.out.println(oldest.name);
break;
case 'b': //another method
break;
default: // equals to else as if none of the other cases was fulfilled
break;
}
Now you can write your own method for each scenario you have to cover.
public Student getOldestStudent(Student[] students) {
// at first we check some cases that do not require further checks
if (students.length == 0) {
System.out.println("No students have been specified");
return null; // this might lead to a NullPointerException so check the return Object whether it is null before doing anything with it
} else if (students.length == 1)
return students[0];
// no we have to see which students if the oldest in the regular case
// the first student will be used for comparison
Student oldestStudent = students[0];
for (int i = 1; i < students.length; i++) {
// see if our current student is older
if (oldestStudent.age < students[i].age)
oldestStudent = students[i];
}
return oldestStudent;
}
This way you can easily access the Students name afterwards (see above in the switch). You can build all your methods like this by passing the array to the methods and iterating through it. Depending on whether you want to return one or more Students (as it might vary between the different methods) you have to change the return type from Student to Student[].
Hi I wanted to know how to write up a try and catch block to stop from getting the below error.
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
I have this method which takes a sentence and splits it into an ArrayList. I then use that to store values into a hashmap, where index 1 is the key and the words after become the value. I use the below method to split the user input into an array.
private Scanner reader;
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new Scanner(System.in);
}
public ArrayList<String> getInput()
{
System.out.print("> "); // print prompt
String inputLine = reader.nextLine().trim().toLowerCase();
String[] wordArray = inputLine.split(" "); // split at spaces
// add words from array into ArrayList
ArrayList<String> words = new ArrayList<String>();
for(String word : wordArray) {
words.add(word);
}
return words;
}
}
and the below method uses the class above to detect user input. So when the user types in write they can write into a hashmap but if they press return before they type in a key and value I get the out of bounds exception. So How can i rewrite the below method to avoid this?
public void start()
{
boolean finished = false;
printWelcome();
while(!finished) {
ArrayList<String> input = reader.getInput();
if(input.contains("shutdown")) {
finished = true;
}
if (input.contains("load")) {
System.out.println();
instruct.readAndFill();
System.out.println();
}
if (input.contains("write")) {
String key = input.get(1);
String value = "";
for(int i=2; i<input.size(); i++) {
value = value + " " + input.get(i);
}
instruct.mapWrite(key, value);
}
}
instructorGoodBye();
}
Sorry if i wasn't clear enough, or if my code is not up to scratch i have only been learning java for about 2 months now.
basically if the user types in write key value on one line it is fine but if they hit return after write then the error happens.
So, fundamentally what you are missing is error checking. Your program is taking input from a user, and assuming it is valid. This is always a bad idea.
Instead, you should validate what you get from the user. One way you can do this, for your "write" block, is to make sure the elements you expect to be there, are actually there.
To start, I would rewrite your loop as follows:
while(!finished) {
List<String> input = reader.getInput();
if(input.size() == 0) {
throw new IllegalArgumentException("Must specify command, one of 'shutdown', 'load', 'write'");
}
final String command = input.remove(0).toLowerCase();
// TODO: Make sure command is one of the valid commands!
Note the changes:
Assigning to List instead of ArrayList is just a good general practice.
Checking the input to make sure it has more than zero elements
Taking the first element, since we don't want to have to do List.contains(). Consider the input garbage garbage garbage write, clearly we don't want this to invoke the "write" command, it should be considered invalid input.
Finally, we use this to rewrite the conditions on executing our commands:
if(command.equals("write")) {
// Make sure the user put the right stuff in here
// Since we removed the command from the input already, just make sure what is left is
if(input.size() <= 1) {
throw new IllegalArgumentException("Must specify correct data");
}
String key = input.remove(0);
String value = String.join(" ", input); // Java 8
instruct.mapWrite(key, value);
}
You are getting the error for below part of the code..
if (input.contains("write")) {
String key = input.get(1);// here is the problem..
String value = "";
for(int i=2; i<input.size(); i++) {
value = value + " " + input.get(i);
}
instruct.mapWrite(key, value);
}
in the line 2 of this code snippet. you are accessing a value by using the index. Now just imagine you just enter a single word in the console. so the arraylist you will get from the getInput() method will have the size of 1. So.. in the arraylist the word will be placed on 0th position.(that is the first position) but you are accessing the value on second position.. Thats gives you a index out of bond exception..
basically the fix was simpler than throwing a new exception and using a try and catch block. All I had to do was slightly change the logic and just use and if else statement.
if (input.contains("write")) {
if(input.size() >=2) {
String key = input.get(1);
String value = "";
for(int i=2; i<input.size(); i++) {
value = value + " " + input.get(i);
}
mapWrite(key, value);
} else {
System.out.println("Please type in the key & value after write all on line");
}
}
From what I have learned from java so far, is that the best solutions are normally always normally the simplest. Thanks for all the help, everyone who commented and tried to help me basically helped me come up with the idea.
I'm trying to write a program in which the user inputs their grocery list then based upon the type of item it is the program will give you a good order to shop for your items(i.e puts the meats together and the vegetables together)
here's my current problem, I can't get the user input to compare to the multiple string type arrays I have of store items.
String[] VegiFruit = {"Apples", "Lettuce", "Broccoli"};
String[] Meats = {"Ground beef", "Hambuger"};
Scanner USER_IN = new Scanner(System.in);
Methods Use = new Methods(); //This is another class I have, it just makes printing empty lines and lines of **** look nicer
Use.border();
System.out.println("Enter Item name then follow instructions.");
Use.space();
System.out.print("Item 1: ");
String InptOne = USER_IN.nextLine();
}
for (int i = 0; i < VegiFruit.length; i++)
{
if(Arrays.asList(VegiFruit).contains(InptOne))
{
System.out.println("Item is a VEGI");
}
}
for(int p = 0; p < Meats.length; p++)
{
if(Arrays.asList(Meats).contains(InptOne))
{
System.out.println("Item is a MEAT");
}
}
you need not go through the loops as contains method will compare with all the elements in the list.
I ran your code and the below works fine.
if(Arrays.asList(VegiFruit).contains(InptOne))
{
System.out.println("Item is a VEGI "+InptOne);
}
if(Arrays.asList(Meats).contains(InptOne))
{
System.out.println("Item is a MEAT "+InptOne);
}
However, please note that this is case senstive comparison and if the user does not enter the vegetable in your format then it will not work.
To solve that problem, you can take 2 approaches:
Have the list of veegetables/meat in caps and make the input.toUpperCase() before comparing it in contain method.
2.Use the answer on Array contains() without case sensitive lookup?
public static void read(String a[], double b[], String c) throws IOException {
Scanner in = new Scanner(new File("data.txt"));
while (in.hasNext()) {
int i = 0;
String id = in.next();
String name = in.next();
String lastname = in.next();
double grade = in.nextDouble();
if (name.substring(0, 2).equalsIgnoreCase(c)) {
a[i] = id + "\t" + name + "\t" + lastname + "\t" + grade;
b[i] = grade;
}
i++;
}
}
When I use this method with
String men[] = new String[501];
double menGrade[] = new double[501];
read(men, menGrade, "MR");
My men[0] is assigned a String but men[1] to men [500] are all null ...
You need to declare your variable i outside of the while loop to keep it incremented.
Right now you are
declaring it with value 0
assign the values to the 1st array position
increment i, and then
declare it again with value 0 at the next loop iteration.
SO, just change your lines:
while (in.hasNext()) {
int i = 0;
to
int i = 0;
while (in.hasNext()) {
Your code has also other issues which you should adress in some way.
I do not know why you initialize your array with a fixed size of 500 and also check some conditions before you add your men and grades to those Arrays. This will however lead to a few problems if you are not careful.
Right now you would have holes in your array whenever the if condition does not evaluate to true.
Also your program would crash if there is more than 500 entries in your file.
A rather good solution when dealing with dynamic data structures (so, when you do not know beforehand how many records you will have exactly), is to use a dynamic data structure.
In java you can have a look at java.util.List interface and probably java.util.ArrayList as a good implementation.
Here is also the java doc of that class: Java Doc
Here you find more on the collections api which are a good thing for dynamic data structures: Collections - List tutorial
while (in.hasNext()) {
int i = 0;
...
This will RESET i each time you start the while loop and you always overwrite a[0] and b[0].
swap these two lines! (so the int i = 0; comes before the loop:
int i = 0;
while (in.hasNext()) {
...
You should increment i in your if statement and not always like you do now. You don't want holes in your men array.
This simply means, either your loop is executing only once.
Or, if block in loop is exceuting only once.
Dependecy is on the content of file you are importing and your if condition.
I'm pretty sure that something is wrong with your "data.txt" that caused the while loop to execute only once. Otherwise, I don't see any mistake in the code.
Why don't you check the value of i during the execution of the program?
If your data.txt file contains One single line then the corresponding while will be running for once populating the first element of the array i.e men in your case
The reason is this:
while (in.hasNext()) {
int i = 0;
...
i++;
}
you are destroying and creating i variable each time loop is executed effectively reseting it to 0 each time. Asides from notes from other answers you can simply move i outside the loop:
int i = 0;
while (in.hasNext()) {
...
i++;
}
Now I can't run in myself, but I see
while (in.hasNext()) {
int i = 0;
//other
a[i] = id + "\t" + name + "\t" + lastname + "\t" + grade;
b[i] = grade;
}
i++;
If you use a counter i over an array/Collection, generally you have to give a greater scope to counter.
if the counter is inside the while, at every iteration you recreate the counter and you point always at the same element of array
The/one solution can be:
int i=0;
while (in.hasNext()) {
//etc
for my project, i have an arraylist for the user to input whatever names they want in one method as long as it isn't stop. then in a separate method, the values i just inputted have to be called out again, so i can input more information for each name.
example:
enter names:
name: bob
name: joe
name: Stop
this triggers another method to prompt more info:
bob:
enter age:
enter address:
joe:
enter age:
enter address:
However, right now, the arraylist isn't printing out correctly, and i'm getting repeats of certain names, while other names don't show up in the second method. Also, the loop in the second method doesn't end. i enter in info for the names i entered, but i keep getting prompted with "name:" and no actual arraylist value. I suspect something is wrong with my loops, but i don't quite know how to fix it? I also thought that maybe the problem has something to do with how i'm using a wrapper to put in values into the arraylist, but i don'tknow how to fix that.
in the second method, I've tried swapping the countervariable to keep track of the order in the Array List with a separate counter in the second method, but it doesn't seem to make a difference. In the first method, i've tried swapping the loop with different a boolean while loop, with a straight up while (!input.equals("Stop")), a for loop counter inside of the previous two options, if loops, and some combination of the above.
here is my code
Scanner console = new Scanner(System.in);
private ArrayList<Directory> nameList; //i have to use object oriented programming to store values in the Directory Class
public int i;
first method:
private void addName()
{
Scanner LocalInput = new Scanner(System.in);
Directory buffer = null;
String ID = null;
System.out.println("Enter Station Designations Below, Enter Stop to quit");
boolean breaker = false;
while(breaker ==false)
{
System.out.print("Name: ");
ID = (LocalInput.nextLine());
if(ID.equals("Stop"))
breaker = true;
else
buffer = new Directory(ID);
nameList.add(buffer);
}
}
second method:
private void getInfo()
{
Scanner LocalInput = new Scanner(System.in);
Directory buffer;
buffer = nameList.get(i);
double age; String address;
System.out.println("Enter Temperatures below...");
System.out.println("" + nameList.get(i));
for (i = 0; i < nameList.size(); i++)
{
System.out.println("Name: " + buffer.GetID()); //there's a getter and setter in the Directory class
System.out.println( "Age:\t");
age = LocalInput.nextDouble();
System.out.print( "Address:\t");
address = LocalInput.nextLine();
buffer= new Directory(age, address);
nameList.add(buffer);
}
}
Critique of first method
I haven't looked closely yet, but I strongly suspect this is the problem:
if(ID.equals("Stop"))
breaker = true;
else
buffer = new Directory(ID);
nameList.add(buffer);
You appear to be expecting the last statement only to be executing when ID is not equal to "Stop", but actually it's always going to execute. Unlike (say) Python, whitespace is irrelevant in Java. If you statements to be considered as a block, you need braces:
if(ID.equals("Stop"))
breaker = true;
else {
buffer = new Directory(ID);
nameList.add(buffer);
}
Personally I would put braces around both parts:
if (ID.equals("Stop")) {
breaker = true;
} else {
buffer = new Directory(ID);
nameList.add(buffer);
}
... and quite possibly get rid of the somewhat-irrelevant buffer variable:
if (ID.equals("Stop")) {
breaker = true;
} else {
nameList.add(new Directory(ID));
}
I'd also get rid of the breaker variable, and limit the scope of ID (changing its name, too, to comply with normal conventions) with a result of:
while (true) {
System.out.print("Name: ");
string id = LocalInput.nextLine();
if (id.equals("Stop")) {
break;
}
nameList.add(new Directory(ID));
}
Critique of second method
This is really odd at the moment. It's not at all clear where the i variable is declared, or why you only fetch buffer once, or why you're just adding to the existing list rather than mutating the existing object. I suspect you really want:
for (Directory entry : nameList) {
System.out.println("Name: " + entry.GetID());
System.out.println( "Age:\t");
double age = LocalInput.nextDouble();
entry.setAge(age);
System.out.print( "Address:\t");
String address = LocalInput.nextLine();
entry.setAddress(address);
}
Note that your current loop will always continue until i equals nameList.size() - but you're always increasing the size in the loop, so you'll never terminate.
private void getInfo()
{
Scanner LocalInput = new Scanner(System.in);
double age; String address;
for (Directory name : nameList) {
System.out.println("Name: " + name .GetID());
System.out.println( "Age:\t");
double age = LocalInput.nextDouble();
name.setAge(age);
System.out.print( "Address:\t");
String address = LocalInput.nextLine();
name.setAddress(address);
}
}
get method should be like this
and Add must be -
private void addName()
{
Scanner LocalInput = new Scanner(System.in);
Directory buffer = null;
String ID = null;
System.out.println("Enter Station Designations Below, Enter Stop to quit");
boolean breaker = false;
while(breaker ==false)
{
System.out.print("Name: ");
ID = (LocalInput.nextLine());
if(ID.equals("Stop"))
breaker = true;
else {
buffer = new Directory(ID);
nameList.add(buffer);
}
}
}
Consider using the break keyword, then you don't need the breakerflag and the else branch:
while(true)
{
System.out.print("Name: ");
ID = (LocalInput.nextLine());
if(ID.equals("Stop"))
break;
buffer = new Directory(ID);
nameList.add(buffer);
}
Sorry was already posted...