3 Replies - 1145 Views - Last Post: 03 December 2009 - 06:31 PM Rate Topic: -----

#1 doodads  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-December 09

How to check an array if a user input is there, then how to add to it.

Post icon  Posted 03 December 2009 - 04:31 PM

I had to load up a .txt file into an array, and now I have to scan it for a name that the user is asked to input. Is this right so far? And how do I go about using a for loop to check if the user input is present?

// look up CASE SWITCH function and 'hasNextInt()' method

import java.lang.*;
import type.lib.Student;
import java.util.*;
import java.io.*;
import java.util.Scanner;

public class StudentDbase {

   public static void main (String[] args) throws java.io.IOException{

	 final int TOTAL_RECORDS = 10;
		 Scanner inputFile = new Scanner(new File("a2.txt"));
	int sizeOfArray = inputFile.nextInt();
	int[] a = new int[sizeOfArray];

	for(int i = 0; inputFile.hasNextInt() && i < a.length; i++){
	  a[i] = inputFile.nextInt();
	}
	 
	 
   {
	 PrintStream output = System.out;
	Scanner input = new Scanner(System.in);
	
	
	
	String []menu = { "1 - Add Courses and Grades" ,
					  "2 - View a Course Grade",
					  "3 - View GPA",
					  "4 - View all Courses and Grades",
					  "5 - View student record",
					  "6 - Output entire databse to a file",
						 "Quit - Exit databse"};

for (int i = 0; i < menu.length; i++)
System.out.println(menu[i]);


System.out.print("Enter menu option: ");
String choice = input.next();

if (choice.equals("1")) {output.println("Enter the full name of the student:");
String name = input.next();}



while (!choice.equals("Quit"))
{
try
{
int intChoice = Integer.parseInt(choice);
System.out.println("Choice was: " + intChoice);
if ((intChoice < 1) || (intChoice > 4))
System.out.println("Invalid choice made. Choose again.");
}
catch (Exception e)
{
System.out.println("Invalid choice made. Choose again.");
}

System.out.print("Enter menu option: ");
choice = input.next();


   }
}
}
}


This post has been edited by doodads: 03 December 2009 - 09:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to check an array if a user input is there, then how to add to it.

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,902
  • Joined: 20-September 08

Re: How to check an array if a user input is there, then how to add to it.

Posted 03 December 2009 - 04:38 PM

An array is not particularly good for doing this as it's inherently fixed. Try the following approach instead

http://technojeeves..../74-string-list
Was This Post Helpful? 0
  • +
  • -

#3 doodads  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-December 09

Re: How to check an array if a user input is there, then how to add to it.

Posted 03 December 2009 - 04:42 PM

I guess I should have mentioned it in the original post but I have to do like I did. There are a few requirements for this program, I'll copy and paste all the hints given to us:


The total number of records should be stored in a variable of type final int.

Only one for loop is need to load the data from the file a2.txt into the Student
array. Make sure you read the total number of records before starting the execution of the
for loop.

Once your data is loaded, use one main for loop to display the main menu and handle
the selection from the user. Inside this main for loop, use control structures based on the
user’s selection to determine the operations to be performed.

In order to know if a record (i.e., full name) is in the database or not, use a for loop to
navigate from record to record and stop the loop once a matching record has been found.
HINT2: The body of this for loop should be empty.

To navigate from course to course in a Student object, use the methods
getFirstCourse() and getNextCourse() in a for loop. For example:
for(String c = s.getFirstCourse(); c != null; c = s.getNextCourse())…

See examples of executions on the next pages.

All input are case sensitive (e.g., file names, full names in the database, and menu
selections). Assume the user is entering the correct inputs.

Use the StringTokenizer() class to read the input file (comma delimited).

This post has been edited by doodads: 03 December 2009 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 doodads  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-December 09

Re: How to check an array if a user input is there, then how to add to it.

Posted 03 December 2009 - 06:31 PM

Don't mean to spam/bump but I pretty much changed up my question, should be easier for someone to respond.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1