mercredi 25 novembre 2015

I need help writing 3 functions that will each do something different in Java, can someone show me how to make them?

I've written the base for the program. My question is, how can I write these functions, what tools, and if possible could someone provide an outline as how I could tackle the functions. The first function is:

public static void save_phonebook(String[][] data, String new_name, String new_number)

the file will add a new line to the input file, using input from input_new_entry it'll go from data[][] to the file

public static void print_spreadsheet(String[][] data) 

will print the data[][], it will essentially print what ever is on the file that is the input

public static void search_data(String[][] data) 

this will take user input and search through the data, I used a code for another program that i believe will be similar to what i need :

String searchString = player.toLowerCase();
for(int row = 1; row < data.length; row++){
    String playerInThisRow = data[row][0];
    if(playerInThisRow.toLowerCase().contains(searchString)){
        for(int column = 0; column < data[0].length; column++){
            String columnContent = data[row][column];
            String columnName = data[0][column];        
            System.out.printf("%20s: %s\n",columnName,columnContent);
        }  
    }
}

my current code for the entire program is as follows:

import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.PrintWriter;

public class phonebook
{
  // returns a phonebook:
  // either reads it from the phonebook.txt file, if the file exists, 
  // or returns an empty array.
 public static ArrayList<String> read_file(String filename)
 {
File temp = new File(filename);
Scanner input_file;
try
{
  input_file = new Scanner(temp);
}
catch (Exception e)
{
  System.out.printf("Failed to open file %s\n",
                    filename);
  return null;
}

ArrayList<String> result = new ArrayList<String>();
while(input_file.hasNextLine())
{
  String line = input_file.nextLine();
  result.add(line);
}

input_file.close();
return result;
} 
 public static String[][] read_phonebook(String filename)
{
ArrayList<String> lines = read_file(filename);
int rows = lines.size();
String[][] result = new String[rows][];         
for (int i = 0; i < lines.size(); i++)
{
  String line = lines.get(i);
  String [] values = line.split(",");
  result[i] = values;
}
return result;
}   
public static void save_phonebook(String[][] data, String new_name, String new_number)
{ 
} 
public static void print_spreadsheet(String[][] data)
{
}
public static void search_data(String[][] data)
{ 
}
public static String[][] input_new_entry(String[][] data)
{
Scanner in = new Scanner(System.in);

System.out.printf("\nEnter a name: ");
String name = in.nextLine();
System.out.printf("\nEnter a number: ");
String number = in.nextLine();
save_phonebook(data, name, number);
data = read_phonebook("phonebook.txt");
return data;
}  
public static String[][] process_option(String[][] data, String option)
{
if (option.equals("1"))
{
  print_spreadsheet(data);
}
else if (option.equals("2"))
{
  data = input_new_entry(data);
}
else if (option.equals("3"))
{
  search_data(data);
}
else if (option.equals("q"))
{
  System.exit(0);
}
else
{
  System.out.printf("Unrecognized option %s.\n", option);
}

return data;
}

public static String ask_option()
{
Scanner in = new Scanner(System.in);

System.out.printf("\n1: Print phonebook.\n");
System.out.printf("2: Input a new entry.\n");
System.out.printf("3: Search by name.\n");
System.out.printf("q: Quit program.\n");
System.out.printf("Please enter an option: ");
String option = in.next();
return option;
}
public static void main(String[] args)
{
String[][] data = read_phonebook("phonebook.txt");
print_spreadsheet(data);

while(true)
{
  String option = ask_option();
  data = process_option(data, option);
}
} 
}

I apologize for the poor formating, copy/paste doesn't work all too well here. The input file contains 2 columns separated by commas, the first is names the second is numbers.

Ex:

Dan, 4556
Beth, 76456
Etc...

Aucun commentaire:

Enregistrer un commentaire