JoanT
JoanT

Reputation: 25

Displaying first letter from user input

For example, I have a list of user input that are randomized. The user input can range between 1-10. The user input are then enqueued into a queue and dequeue to display the list of user input.

However, I want to use the user input to display the first letter of each user input. I tried creating a new variable called name1 to store the user input but it returns an error due to the name1 variable being null.

for(int i =1;i<=value ;i++){
    System.out.println("Enter name #" + i+":");
    String name = input.next();
    String name1 = name;
    myQueue.enqueue(name);   
 }
System.out.println("List of names: ");
for(int j=1; j <=value; j++){
    System.out.println(j+". " +(myQueue.dequeue()));

} 
System.out.println("Statistics:");
char firstletter = name1.charAt(0); //error: value is null 
System.out.println(firstletter); 

Upvotes: 0

Views: 140

Answers (4)

Dragomirus
Dragomirus

Reputation: 449

I hope it helps you:

for(int i = 1; i <= value; i++)
{
    System.out.println("Enter name #" + i + ":");
    myQueue.enqueue(input.next());   
}

System.out.println("List of names: ");
for(int j = 1; j <= value; j++)
{
    String name = (String) myQueue.dequeue(); //pop element
    System.out.println(j + ". " + name);
    myQueue.enqueue(name);
} 

System.out.println("Statistics: ");
for(int j = 1; j <= value; j++)
{
    String name = (String) myQueue.dequeue();
    System.out.println(name.charAt(0));
}

Upvotes: 0

vlence
vlence

Reputation: 495

I think what you did here is declared name1 outside the loop and then redeclared it inside the loop again. Remove the declaration inside the loop and write name1 = name instead.

Upvotes: 0

Hasnain Ali Sohrani
Hasnain Ali Sohrani

Reputation: 415

name1= name;

must be outside of the loop

Upvotes: 0

Shahakar Vaibhav
Shahakar Vaibhav

Reputation: 33

Hope this will help you

import java.util.LinkedList;
import java.util.Scanner;

class GenQueue<E> {
   private LinkedList<E> list = new LinkedList<E>();
   public void enqueue(E item) {
      list.addLast(item);
   }
   public E dequeue() {
      return list.poll();
   }
   public boolean hasItems() {
      return !list.isEmpty();
   }
   public int size() {
      return list.size();
   }
   public void addItems(GenQueue<? extends E> q) {
      while (q.hasItems())
         list.addLast(q.dequeue());
   }
}

public class myQueue {
   public static void main(String[] args) {
      GenQueue<Employee> empList;
      empList = new GenQueue<Employee>();
      GenQueue<HourlyEmployee> hList;
      hList = new GenQueue<HourlyEmployee>();

      Scanner reader = new Scanner(System.in);  // Reading from System.in
      System.out.println("Enter a number: ");
      int n = reader.nextInt(); // Scans the next token of the input as an int.
      for(int i =1;i<=n ;i++){
          System.out.println("Enter a name: ");
          String name = reader.next();

          hList.enqueue(new HourlyEmployee(name, name));
          empList.addItems(hList);
      }
      System.out.println("The employees' names are:");
      while (empList.hasItems()) {
         Employee emp = empList.dequeue();
         //System.out.println(emp.firstName + " "  + emp.lastName);
         char firstletter = emp.firstName.charAt(1); //error: value is null 
         System.out.println(firstletter); 
      }
   }
}

class Employee {
   public String lastName;
   public String firstName;
   public Employee() {
   }
   public Employee(String last, String first) {
      this.lastName = last;
      this.firstName = first;
   }
   public String toString() {
      return firstName + " " + lastName;
   }
}

class HourlyEmployee extends Employee {
   public double hourlyRate;
   public HourlyEmployee(String last, String first) {
      super(last, first);
   }
}

Upvotes: 1

Related Questions