Q BgQuestion:

      
Pupil
Karma Points: 74
Respect (100%):
posted by  noo on 11/7/2009 10:53:39 AM  |  status: Live  |  Earned Karma: 74

Java programming

Course Textbook Chapter Problem Needs by
Software Design N/A N/A N/A 11/9/2009 at 1:00:00 PM
Question Details:
Using a 2-dimensional array you will load the information stored in the CSV file.
The user will be able to look up people in the small database by any attribute.
Assume a maximum of 50 people.
If the user enters the character ‘*’ within the search, then the system fill output EVERY record.
When outputting ALL records, make sure that empty array locations are accounted for.
When listing ALL users, make sure the program stops if there are no more records saved, even if there is room.

Here is content of CSV fileFirst
Name,Last Name,eMail Address,Birth Year,Birth Month,Birth Day,University
John,Smith,jsmith@yahoo.com,1956,12,25,TU University
Michael,Carrol,mcarrol@yahoo.com,1986,7,4,MN College
Adams,Samuels,asamuels@msn.com,1994,11,26,South Park University
Sam,Andrew,sandrew@verizon.com,1973,12,14,US University
Randy,Otton,rotton@live.com,1992,9,21,JH University
Mary,Wesly,mwesly@yahoo.com,1978,4,5,MS University
Michelle,Reges,mregesr@yahoo.com,1945,1,4,
Marty,Clinton,mclinton@comcast.com,1996,8,14,South University

Bonus Point Alert! Earn +7 additional karma points for helping this gold member.

AAnswers:

Answer Question Ask for clarification
Oracle
Karma Points: 18,830
posted by Marth on 11/7/2009 12:08:44 PM  |  status: Live
Asker's Rating: Helpful   
Response Details:
You could input the data as follows:


// read in header
Scanner file = new Scanner(new File("CSV.dat"));

String[] fields = file.nextLine().split(",");

// now read in the rest of the data into one string and split it

String temp = "";

while(file.hasNextLine())
{
      String line = file.nextLine().trim();

      if(line.length()>0)
      {
            output += line+"\n";
      }      

}

// trim extra newlines
output = output.trim();

// create temporary split
String[] split = output.split("\n");

// create 2d matrix
String[][] mat = new String[split.length];

// create 1d arrays from split
for(int i = 0; i < split.length; i++)
{
      mat[i] = split[i].split(",");
}


 
The rest of the accessors and modifications are just performing linear searches.


Thank you for rating my response. Feel free to PM me if you have further questions.

Java Genius

"Lois Lane is falling, accelerating at an initial rate of 32ft per second per second. Superman swoops down to save her by reaching out two arms of steel. Ms. Lane, who is now traveling at approximately 120 miles per hour, hits them, and is immediately sliced into three equal pieces." - Sheldon
Oracle
Karma Points: 31,880
posted by rapunzel on 11/7/2009 7:40:23 PM  |  status: Live
Asker's Rating: Helpful   
Response Details:
please rate - thanks

I did it with a menu, like your message said, but I'm not sure that's what you want because of the *

import java.util.*;
import java.io.*;
public class untitled
{static int i;
public static void main(String[] args)throws FileNotFoundException
{String [] first=new String[50];
String [] last=new String[50];
String [] mail=new String[50];
String []univ=new String[50];
String input="";
int[] year=new int[50];
int[] month=new int[50];
int[] day=new int[50];
int count=0,choice,input1=0;
Scanner in=new Scanner(new File("input.txt"));
Scanner kb=new Scanner(System.in);
while(in.hasNext())
    {i=0;
    input =in.nextLine();
     first[count]=part(input,',');
     last[count]=part(input,',');
     mail[count]=part(input,',');
     year[count]=Integer.parseInt(part(input,','));
     month[count]=Integer.parseInt(part(input,','));
     day[count]=Integer.parseInt(part(input,','));
     univ[count]=part(input,'\0');
      count++;
      }
System.out.println("first\t\tLast\teMail Address   \tBirth Year\tBirth Month\tBirth Day\tUniversity\n");            
for(i=0;i<count;i++)
    System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);
for(;;)
{do
  {choice=menu(kb);
  if(choice<1||choice>8)
     System.out.println("Invalid-Entry\n");
  }while(choice<1||choice>8);
  switch(choice)
  {case 2: System.out.println("Enter Last Name");
           input=kb.next();
              for(i=0;i<count;i++)
                  if(input.compareToIgnoreCase(last[i])==0)
                        System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);


              break;
    case 1: System.out.println("Enter First Name");
           input=kb.next();
              System.out.println("first\t\tLast\teMail Address   \tBirth Year\tBirth Month\tBirth Day\tUniversity\n");          
           for(i=0;i<count;i++)
                  if(input.compareToIgnoreCase(first[i])==0)
                        System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);


              break;
    case 3: System.out.println("Enter email address");
           input=kb.next();
              System.out.println("first\t\tLast\teMail Address   \tBirth Year\tBirth Month\tBirth Day\tUniversity\n");          
           for(i=0;i<count;i++)
                  if(input.compareToIgnoreCase(mail[i])==0)
                        System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);


              break;
    case 4: System.out.println("Enter Birth Year Name");
           input1=kb.nextInt();
              System.out.println("first\t\tLast\teMail Address   \tBirth Year\tBirth Month\tBirth Day\tUniversity\n");          
           for(i=0;i<count;i++)
                  if(input1==year[i])
                        System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);


              break;
   case 5: System.out.println("Enter Birth Month Name");
           input1=kb.nextInt();
              System.out.println("first\t\tLast\teMail Address   \tBirth Year\tBirth Month\tBirth Day\tUniversity\n");          
           for(i=0;i<count;i++)
                  if(input1==month[i])
                        System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);


              break;
   case 6: System.out.println("Enter Birth Day Name");
           input1=kb.nextInt();
              System.out.println("first\t\tLast\teMail Address   \tBirth Year\tBirth Month\tBirth Day\tUniversity\n");          
           for(i=0;i<count;i++)
                  if(input1==day[i])
                        System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);


              break;
   case 7: System.out.println("Enter University Name");
           input=kb.next();
              System.out.println("first\t\tLast\teMail Address   \tBirth Year\tBirth Month\tBirth Day\tUniversity\n");          
           for(i=0;i<count;i++)
                  if(input.compareToIgnoreCase(univ[i])==0)
                        System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);

              break;
    case 8:System.out.println("first\t\tLast\teMail Address   \tBirth Year\tBirth Month\tBirth Day\tUniversity\n");          
          for(i=0;i<count;i++)
             System.out.printf("%10s %10s %20s %6d %4d %4d %s\n",first[i],last[i],mail[i],year[i],month[i],day[i],univ[i]);

      }
        
}
}
public static int menu(Scanner in)
{int num;
System.out.println("\n\nWhat field do you want to lookup on? ");
System.out.println("1 - First Name");
System.out.println("2 - Last Name");
System.out.println("3 - eMail Address");
System.out.println("4 - Birth Year");
System.out.println("5 - Birth Month");
System.out.println("6 - Birth Day");
System.out.println("7 - University");
System.out.println("8 - Print all records");
num=in.nextInt();
return num;
}

 public static String part(String input, char delim)
 {String out="";
 int j;
 for(j=i;j<input.length();j++)
     if(input.charAt(i)!=delim)
         out+=input.charAt(i++);
      else
         {i++;
          j=input.length();
          }
return out;
  }    
}



Note to all members 1. Please 1 question per post 2. Show respect to your fellow members by rating all answers. 3. When rating remember that the first answer is not necessarily the best answer. 4. When answering questions, explain what you are doing, so that the asker will learn, don't just give a meaningless number. 5. Your answers should be your work. Don't copy from another member, this is Karma abuse and possible disciplinary actions against you. and GOOD LUCK to you all!!
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »