Sorting and Searching String (Please help me)
Hi, Could somn please be of any help here. I am trying to sort this string in alphabetical order and then search for data available but I am not makingprogress with the code. Does anyone have any advise or better still the code to solve this problem... This is hat I have at the moment
import java.util.*;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;
public class coursework2
public static final int MAX_RECORDS = 20;
public static String lastName[] = new String[MAX_RECORDS];
public static String firstName[] = new String[MAX_RECORDS];
public static String telNumber[] = new String[MAX_RECORDS];
public static String emailAddress[] = new String[MAX_RECORDS];
public static int read_in_file(String file_name)
Scanner read_in;
Scanner line;
int record_count = 0;
String record;
try
read_in = new Scanner(new File(file_name));
// read in one line at a time
read_in.useDelimiter(System.getProperty("line.separator"));
while (read_in.hasNext())
// Test to see if there are too many records in the file
if (record_count == MAX_RECORDS)
System.out.printf("Only %d records allowed in file", MAX_RECORDS);
System.exit(0);
// read in record
record = new String(read_in.next());
// Split the record up into its fields and store in
// appropriate arrays
line = new Scanner(record);
line.useDelimiter("\\s*,,\\s*");
lastName[record_count] = line.next();
firstName[record_count] = line.next();
telNumber[record_count] = line.next();
emailAddress[record_count] = line.next();
// Increment record count
record_count++;
catch (FileNotFoundException e)
e.printStackTrace();
return record_count;
public static void write_out_file(int no_of_records, String filename)
PrintStream write_out;
int counter;
try
// Create new file
write_out = new PrintStream(new File(filename));
// Output all reacords
for(counter = 0; counter < no_of_records; counter++)
// Output a record
write_out.print(lastName[counter]);
write_out.print(",,");
write_out.print(firstName[counter]);
write_out.print(",,");
write_out.print(telNumber[counter]);
write_out.print(",,");
write_out.println(emailAddress[counter]);
// Close file
write_out.close();
catch (FileNotFoundException e)
e.printStackTrace();
// Your 'functions' go here
//This code sorts out the record after loaded into alphabetical order using
//the selection sort method
public static void sort()
// Your 'main' code goes here
//The code below uses a binary search method to search for record because
//record have been sorted and this would make search faster and more efficient
public static boolean linearSearch(String strFirstName, String strLastName)
//Set-up data_input and declare variables
//This linear search searches for record in the contact application.
//Search for a record using surname only
int i = 0;
boolean found = false;
while (!found && i < lastName.length)
if(strFirstName.equals("") && strLastName.equals(""))
return true;
if(lastName.equalsIgnoreCase(strLastName)){
//List records from surname only
System.out.println("Enter your search criteria (last name only):");
System.out.print(lastName[i] + "\t" + firstName[i] + "\t" + telNumber[i] +"\t" + emailAddress[i]);
System.out.println("");
if (lastName[i].compareTo(strLastName) == 0)
//Compare the last name values and type to make sure they are same.
found = true;
i++;
System.out.print(lastName[i] + "\t" + firstName[i] + "\t" + telNumber[i] +"\t" + emailAddress[i]);
System.out.println("");
return found;
//Search for a record using first name only
public static void main(String[] args)
// Set-up data input
Scanner data_input = new Scanner(System.in);
// Declare Variables
int number_of_records;
String filename;
int counter;
// Get filename and read in file
System.out.print("Enter the masterfile file name: ");
filename = data_input.next();
number_of_records = read_in_file("data2.dat");
//Get new filename and write out the file
System.out.print("Enter new masterfile file name: ");
filename = data_input.next();
//System.out.println("Enter your search criteria (first or last name):");
linearSearch("*", "");
Am very sorry this is long, the file should be sorted after it is loaded and I have that, You can make up some data with last ane, first name, tel and email to show me an example. Thanks alot
Joseph
Hi Monica, Thanks for you patience with me.
I have tried writing a selection sort method but am having some errors. Could you kindly have a lookand correct me please.
This is the code i wrote
public static void selectionSort(Comparable [] data)
String strFirstName;
String strLastName;
Comparable temp;
for(int i = 0; i < lastName.length; i++)
lastName = index;
for (int j = i; j < lastName.length; j++)
if (lastName[j].compareTo(lastName) < 0)
lastName[j] = lastName[i];
//Swap the string values
temp = lastName[j];
lastName[j] = lastName[i];
lastName[i] = temp;
As you requested, below are 4 error messages I had
E:\coursework2.java:101: cannot find symbol
symbol : variable index
location: class coursework2
lastName = index;
^
E:\coursework2.java:107: cannot find symbol
symbol : variable j
location: class coursework2
temp = lastName[j];
^
E:\coursework2.java:108: cannot find symbol
symbol : variable j
location: class coursework2
lastName[j] = lastName[i];
^
E:\coursework2.java:109: incompatible types
found : java.lang.Comparable
required: java.lang.String
lastName[i] = temp;
^
4 errors
JCompiler done.
JCompiler ready.
Joseph
Similar Messages
-
''locking as a duplicate - https://support.mozilla.com/en-US/questions/793163''
@ weeks ago the view, copy, edit, files, tools, bar at the top of the page disappeared. Then last week the next row that contained back, refresh, and a search bar disappeared. Please help me fix this, as I am unable to clear cookies, cache, history or use my printer.See [[menu bar is missing]].
-
Sort a string :: Please help.
Hello Everyone,
I am having this very simple problem of sorting a String. please help.
static String sortString(String str){
List list = Arrays.asList(str);
Collections.sort(list);
for(int i=0, n=list.size();i<n;i++){
System.out.println(","+ list.get(i));
The function is supposed to take a String and sort it & print it out. This should be simple. Where am I making mistakes? Please help!!Hello Everyone,
I am having this very simple problem of sorting a String. please help.
static String sortString(String str){
List list = Arrays.asList(str);
Collections.sort(list);
for(int i=0, n=list.size();i<n;i++){
System.out.println(","+ list.get(i));
return str;
The function is supposed to take a String and sort it & print it out. This should be simple. Where am I making mistakes? Please help!! -
I need to pass a query in form of string to DBMS_XMLQUERY.GETXML package...the parameters to the query are date and varchar ..please help me build the string .Below is the query and the out put. ( the string is building fine except the parameters are with out quotes)
here is the procedure
create or replace
procedure temp(
P_MTR_ID VARCHAR2,
P_FROM_DATE IN DATE ,
P_THROUGH_DATE IN DATE ) AS
L_XML CLOB;
l_query VARCHAR2(2000);
BEGIN
l_query:= 'SELECT
a.s_datetime DATETIME,
a.downdate Ending_date,
a.downtime Ending_time,
TO_CHAR(ROUND(a.downusage,3),''9999999.000'') kWh_Usage,
TO_CHAR(ROUND(a.downcost,2),''$9,999,999.00'') kWh_cost,
TO_CHAR(ROUND(B.DOWNUSAGE,3),''9999999.000'') KVARH
FROM
(SELECT s_datetime + .000011574 s_datetime,
TO_CHAR(S_DATETIME ,''mm/dd/yyyy'') DOWNDATE,
DECODE(TO_CHAR(s_datetime+.000011574 ,''hh24:'
||'mi''), ''00:'
||'00'',''24:'
||'00'', TO_CHAR(s_datetime+.000011574,''hh24:'
||'mi'')) downtime,
s_usage downusage,
s_cost downcost
FROM summary_qtrhour
WHERE s_mtrid = '
||P_MTR_ID||
' AND s_mtrch = ''1''
AND s_datetime BETWEEN TO_DATE('
||P_FROM_DATE||
',''DD-MON-YY'') AND (TO_DATE('
||P_THROUGH_DATE||
',''DD-MON-YY'') + 1)
) a,
(SELECT s_datetime + .000011574 s_datetime,
s_usage downusage
FROM summary_qtrhour
WHERE s_mtrid = '
||P_MTR_ID||
' AND s_mtrch = ''2''
AND s_datetime BETWEEN TO_DATE('
||P_FROM_DATE||
',''DD-MON-YY'') AND (TO_DATE('
||P_THROUGH_DATE||
','' DD-MON-YY'') + 1)
) B
where a.S_DATETIME = B.S_DATETIME(+)';
SELECT DBMS_XMLQUERY.GETXML('L_QUERY') INTO L_XML FROM DUAL;
INSERT INTO NK VALUES (L_XML);
DBMS_OUTPUT.PUT_LINE('L_QUERY IS :'||L_QUERY);
END;
OUTPUT parameters are in bold (the issue is they are coming without single quotes otherwise th equery is fine
L_QUERY IS :SELECT
a.s_datetime DATETIME,
a.downdate Ending_date,
a.downtime Ending_time,
TO_CHAR(ROUND(a.downusage,3),'9999999.000') kWh_Usage,
TO_CHAR(ROUND(a.downcost,2),'$9,999,999.00') kWh_cost,
TO_CHAR(ROUND(B.DOWNUSAGE,3),'9999999.000') KVARH
FROM
(SELECT s_datetime + .000011574 s_datetime,
TO_CHAR(S_DATETIME ,'mm/dd/yyyy') DOWNDATE,
DECODE(TO_CHAR(s_datetime+.000011574 ,'hh24:mi'), '00:00','24:00', TO_CHAR(s_datetime+.000011574,'hh24:mi')) downtime,
s_usage downusage,
s_cost downcost
FROM summary_qtrhour
WHERE s_mtrid = N3165 AND s_mtrch = '1'
AND s_datetime BETWEEN TO_DATE(01-JAN-13,'DD-MON-YY') AND (TO_DATE(31-JAN-13,'DD-MON-YY') + 1)
) a,
(SELECT s_datetime + .000011574 s_datetime,
s_usage downusage
FROM summary_qtrhour
WHERE s_mtrid = N3165 AND s_mtrch = '2'
AND s_datetime BETWEEN TO_DATE(01-JAN-13,'DD-MON-YY') AND (TO_DATE(31-JAN-13,' DD-MON-YY') + 1)
) B
where a.S_DATETIME = B.S_DATETIME(+)The correct way to handle this is to use bind variables.
And use DBMS_XMLGEN instead of DBMS_XMLQUERY :
create or replace procedure temp (
p_mtr_id in varchar2
, p_from_date in date
, p_through_date in date
is
l_xml CLOB;
l_query VARCHAR2(2000);
l_ctx dbms_xmlgen.ctxHandle;
begin
l_query:= 'SELECT
a.s_datetime DATETIME,
a.downdate Ending_date,
a.downtime Ending_time,
TO_CHAR(ROUND(a.downusage,3),''9999999.000'') kWh_Usage,
TO_CHAR(ROUND(a.downcost,2),''$9,999,999.00'') kWh_cost,
TO_CHAR(ROUND(B.DOWNUSAGE,3),''9999999.000'') KVARH
FROM
(SELECT s_datetime + .000011574 s_datetime,
TO_CHAR(S_DATETIME ,''mm/dd/yyyy'') DOWNDATE,
DECODE(TO_CHAR(s_datetime+.000011574 ,''hh24:'
||'mi''), ''00:'
||'00'',''24:'
||'00'', TO_CHAR(s_datetime+.000011574,''hh24:'
||'mi'')) downtime,
s_usage downusage,
s_cost downcost
FROM summary_qtrhour
WHERE s_mtrid = :P_MTR_ID
AND s_mtrch = ''1''
AND s_datetime BETWEEN TO_DATE(:P_FROM_DATE,''DD-MON-YY'')
AND (TO_DATE(:P_THROUGH_DATE,''DD-MON-YY'') + 1)
) a,
(SELECT s_datetime + .000011574 s_datetime,
s_usage downusage
FROM summary_qtrhour
WHERE s_mtrid = :P_MTR_ID
AND s_mtrch = ''2''
AND s_datetime BETWEEN TO_DATE(:P_FROM_DATE,''DD-MON-YY'')
AND (TO_DATE(:P_THROUGH_DATE,'' DD-MON-YY'') + 1)
) B
where a.S_DATETIME = B.S_DATETIME(+)';
l_ctx := dbms_xmlgen.newContext(l_query);
dbms_xmlgen.setBindValue(l_ctx, 'P_MTR_ID', p_mtr_id);
dbms_xmlgen.setBindValue(l_ctx, 'P_FROM_DATE', to_char(p_from_date, 'DD-MON-YY'));
dbms_xmlgen.setBindValue(l_ctx, 'P_THROUGH_DATE', to_char(p_through_date, 'DD-MON-YY'));
l_xml := dbms_xmlgen.getXML(l_ctx);
dbms_xmlgen.closeContext(l_ctx);
insert into nk values (l_xml);
end; -
Pass username and password ADFS without using query string, Please help.
pass username and password ADFS without using query string, Please help.
I used query string , but it is unsecured to pass credentials over url, with simple tool like httpwatch , anyone can easily get the password and decrypt it.Hi,
According to your post, my understanding is that you had an issue about the ADFS.
As this issue is related to ADFS, I recommend you post your issue to the forum for ADFS.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=Geneva
The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.
Thank you for your understanding and support.
Thanks,
Jason
Forum Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
[email protected]
Jason Guo
TechNet Community Support -
I don't know exactly when this started, but Winamp became my default search and homepage.. The homepage issue was easy, but the search issue is bigger than I expected. It ALWAYS uses Winamp (Powered by AOL) as my default search. Please help. Thank you.
reinstall it but make sure to uncheck the box next to "make winamp my default search engine"
attached is screenshot from the part of installation proces where that checbox should be unchecked
hope this helps -
I have an itunes account on my home PC. I want to use my same account but on my laptop. How do i do this with out wiping out my phone and backing up all my Pictures and Apps? Please Help.
Copy everything from the old computer or your backup copy of your old computer to your new one.
Type "move itunes library from pc to mac" or similar into the google search bar. -
Mail 5.1 -- sorting and searching by sender
I just upgraded to Lion OS X, and I took the opportunity to move from Entourage to Mail 5.1. I may have to move over to Outlook for Mac if I can't figure this out. I'd like to be able to sort and search by sender without using the search bar, which requires me to use the mouse. Please don't ask me why; suffice to say I like keystrokes, as I find them much faster!
In every other email program that I ever used, I could search quickly for a person's emails by (1) clicking on the "From" column to sort by sender, and then (2) typing the person's first name. For example, once I had sorted by "From," if I started to type "Michael," the cursor would move immediately to the first "Michael" in the list of senders. Or if I just typed "M," the cursor would move to the first "M" in the list of senders. Etc.
In Mail, as a default rule, the program does something else: Even though I've sorted by "From," it finds the first reference to what I've typed -- e.g., Michael -- in the list of subjects, rather than in the list of senders. This is downright absurd. I have 10,000 emails in my inbox. When I'm trying to find one, I'm looking for the author. I don't remember the first word of the subject line. I remember the author. In any event, the program ought to look through the column by which I've sorted. Once I've sorted by "From," it should look through the "From" column, not the "Subject" column.
Is there any way to change this default rule? If this is a bug, can someone please fix it? Thanks!Additional information - I am continually getting a dialog box that pops up and says "Address Book wants to use 'login' keychain" I enter it and the box goes away for a while. Then comes back for no apparent reason. Does this make any sense as to the cause? Another aspect of this, which I've seen elsewhere in the forum, is regular crashing when trying to search mail. My MacBook has the same OS and it works perfectly in all respects.
-
My appstore wont let me download anything. It says i have to agree to the terms and i agree and again it pop up the same thing over and over! Please help me
I thought i was the only one!! but its happening on my ipad! and i dont even want to try on my iphone uhhh its annoying!
-
"Connect to iTunes to Use Push Notifications" comes almost in every app that I open and it does not notify me when I got message in Whatsapp and Viber. Please help me to overcome it, it is becoming quite annoying!
Did you get any help on this? From what I can see from surfing this site, it's a BUG that no one has offered a solution to. It sometimes happens after a 'reset' and going to ITunes does not offer a way to 'set up push notifications'.
-
I'm having multiple crashes on my new 5s. I can't connect to my Mac Pro!! Power cord broken. How can I fix this without connecting to my MacBook? Very frustrating and disappointing. Please help!!!
Hi there Rlong50,
You may want to try force closing all open apps and resetting the device as an initial troubleshooting step. Take a look at the articles below for more information.
iOS: Force an app to close
http://support.apple.com/kb/ht5137
iPhone, iPad, iPod touch: Turning off and on (restarting) and resetting
http://support.apple.com/kb/ht1430
-Griff W. -
I have a problem with itunes when I sync the saved music and want to sync my iphone 5 ios 7.0.6 no longer passes the music thing is that just stays on "waiting for sync" and not worry please help are more than 400 songs that do not want to hear who are wi
Plawexki wrote:
... do you know if the contacts, photos, messages etc will be wiped?
Yes... Everything will be Wiped and Replaced with what is currently on Your Mac.
SYNCING with iTunes
See here > http://support.apple.com/kb/HT1386
From Here > http://www.apple.com/support/iphone/syncing/
You may find this information of interest...
Have a read here...
https://discussions.apple.com/message/18409815?ac_cid=ha
And See Here...
How to Use Multiple iDevices with One Computer -
iPhone 4 went into recovery mode when trying to restore it. Now the only option iTunes allows is for "restore & upgrade", it is not offer an option to restore from a backup, says that must first do the upgrade, when attempting to do the upgrade the iPhone won't accept it.
How can I restore from a back up in recovery mode when the only option is "restore and upgrade"?
Please help.You don't you must restore your phone first and then you will have the option to restore from your backup.
If you can't update or restore your iPhone, iPad, or iPod touch - Apple Support -
My iPod shuffle will not turn on, nor is it recognized by the computer to pull up iTunes. I have order new USB cords and nothing. Please help! Not sure of the generation of iPod shuffle, I think 3rd.
first: why did you download the iOS Beta 8? If you are a developer, go to the Apple Developers website and check what they can do for your case.
second: setup an appointment to a Genius Bar for service. -
The enter key is not working in firefox but it has working fine in chrome and IE so please help me to rid out from this.
Hello gokulaan, '''try Firefox Safe Mode''' to see if the problem goes away. Safe Mode is a troubleshooting mode, which disables most add-ons.
''(If you're not using it, switch to the Default theme.)''
* You can open Firefox 4.0+ in Safe Mode by holding the '''Shift''' key when you open the Firefox desktop or Start menu shortcut.
* Or open the Help menu and click on the '''Restart with Add-ons Disabled...''' menu item while Firefox is running.
''Once you get the pop-up, just select "'Start in Safe Mode"''
'''''If the issue is not present in Firefox Safe Mode''''', your problem is probably caused by an extension, and you need to figure out which one. Please follow the [[Troubleshooting extensions and themes]] article for that.
''To exit the Firefox Safe Mode, just close Firefox and wait a few seconds before opening Firefox for normal use again.''
''When you figure out what's causing your issues, please let us know. It might help other users who have the same problem.''
Thank you.
Maybe you are looking for
-
Problems with images greyed out. Downloaded folder not showing up in Left Panel
Downloaded folder and it showed up in Lightroom. Next day images greyed out Folder doesnt show in Left Panel. Lightroom says no images to Import. What do I do?
-
Order cannot be delivered (vl01n)
I have a problem in which I get the above error message. Problem description: At PLANT P1 3 storage locations (SL1,SL2,SL3). There's a sales order for 1 EA of material M1. If M1 is in stock at SL1 (the first in the list of S.LOCs for PLANT P1) i'm al
-
Data load InfoPackage gets shot dumps
This is related to the solution Vijay provided in the link What is the functionality of 0FYTLFP [Cumulated to Last Fiscal Year/Period I encounter the problem again for Data Mart load that I created different initial load InfoPackages with different d
-
Hello everybody: I need some help, this is my situation: Every day when I arrive to my job I have to kill inactive session from other days, e.g Today I had to kill a lot of Inactive Session of Yesterday. How could I did this by other way. Thanks
-
Turned on for the 1st time but don't want 2 se up yet
Ok, so this is a dumb question but i just got my macbook pro about 30min ago and don't want to mess anything up! I already plugged it in to charge and i turned it on and its ready for the set up process but i cant do it until i get home. is there a w