Mel Ram

Greenhorn
+ Follow
since Sep 30, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Mel Ram

Andrew Tangen wrote:I can't help you without knowing what the problem is. Your second grouping of code will work fine to read the first/last names after only looking at it for 5 seconds, because its no different from your first batch there. You didn't even need that second class as you said you already had the second file in alpha order.

What was the expected behavior opposed to what actually happened?



Ok.. here's what the problem states. FYI, this problem is not taken from any textbook. This is a problem from the teacher himself.

Write a program that reads a file of names where each name is a string consisting of a person’s first name, followed by a blank, then followed by the person’s last name. (Assume that the file contains at most twenty names.) Create a new file with the same names, but where each name is in the form: last name, followed by a comma, followed by a space, then followed by the person’s first name. Assume that the original file is in no particular order, and write the new file in alphabetical order based on last name.


14 years ago
You guys have been great at helping figure things out so I'm appreciating your responses!!

My next problem is this, I am suppose to have a program that opens a file and read it as and prints it out. I've done that and that is great. But I then need to open a new file and have that print out as well. I've been going in circles with that. I've already created the new file. here's the code that I have now.



Here's what I thought I could do:



So what the second part of the program is suppose to do is read another file that contains the same names that were in the first file, but in alphabetical order. I'm suppose to show both. I already created that file, like I said, in alpha order, so it's all ready to go. sigh. Any takers?
14 years ago

Janeice DelVecchio wrote:Mel,

What is the output of your two lines of code?

If you need to assign the int value of the first instance of a variable, you need to use the "=" operator, as fred said -- instead of using the println() method. I would say "the variable k equals the integer result of the "indexOf" method when s1 is the parameter."

The application will only output 0cacoon (not 12cacoon) if you say this:



aha! That's what is reffering to!! I just didn't understand what the question stated. I got it now. awesome! thanks a lot to everyone!!
14 years ago

fred rosenberger wrote:the 'assignment' operator in java is the '=' symbol. you use this to assign a value to a variable.

when you decleare a variable, you must have two things - a type and a name. for example, you could have a 'double' type variable named 'fred'. you would declare it like this:

double fred;

if i then wanted to assign a value to it, i would us the operator mentioned above:

fred = 7.3;

i can also assign the value of one variable to another:

double bob;
bob = fred;

and, i can declare and assign a value in one step:

double george = 17.0;

note that anything that returns the correct type can be used on the right-hand side: a literal (like '12'), another variable (if it's the correct type) or even a method:

george = getValueOfPi();

(assuming there is such a method).



OK so what the problem is saying assign the integer k to a value let say 12... and this will replace the character 'c' to the number 12. So it would print out 12acooon. Is that right?
14 years ago

Paul Clapham wrote:

Mel Ram wrote:Usually in math or in programming, when they say k, it refers to any integer... that short program returns and prints an integer of 0.


Yeah, it does, doesn't it? So why did you write that program? It doesn't seem to have much to do with your original post where you said

Assign the index of the first occurrence of the character 'c' in s1 to an int variable k.



Well, because that is what I got from it. So I am guessing its not so could you point me in the right direction as to how to find the solution. I am not understanding the problem, evidently. lol

Thanks!
14 years ago

Paul Clapham wrote:Well, you didn't use the letter "k" anywhere in that... oh sorry, you just wanted a yes or no answer. Okay. Then the answer is no.



Usually in math or in programming, when they say k, it refers to any integer... that short program returns and prints an integer of 0. My question is whether the k that is referring to is what the program returns and prints OR something else that I can even form the question.

Any help? THanks!!
14 years ago
Assign the index of the first occurrence of the character 'c' in s1 to an int variable k.

So I need to right just a small portion of this program, but I do not know what it means "to an int variable k." Is this what it means?

String s1 = "cacoon";
System.out.println(s1.indexOf('c'));
14 years ago

John de Michele wrote:Mel:

This looks like a job for regular expressions.

John.



Thanks John! I feel you should be wearing a cape. The wheels are definitely turning!!! Now let me ask you this or anyone who is reading... the problem states that the user inputs the SSN number... and it also says to assume that 'D' is an interger. I am not sure if he was hinting at something there... is there a way to recognize the 'D' as any integer?

As an example: "434-23-4953".matches("DDD-DD-DDDD"); where 'D' can be any integer. Is there a method that will do that? or is there another way to approach it!?

***** Thinking to myself ***** wait... what if i used the replaceAll method to replace any integer to the letter 'D' and then use the matches method to to compare both strings....


HA!!! I DID IT !!! lol... wow... that was pretty easy!! thanks again John!!!

Problem... I am not seeing whats wrong here. if i put in a set string, it returns true. however from the user's input, it returns false. ummm... why?

Assume the user input 234-23-4224:

ssn.replaceAll("[0123456789]", "D");
System.out.println(ssn.matches("DDD-DD-DDDD"));
FALSE

String test = "123-34-2345".replaceAll("[0123456789]", "D");
System.out.println(test.matches("DDD-DD-DDDD"));
TRUE

UPDATE: Got it to work!
14 years ago
i keep forgetting to write back to thank people for helping me... didn't want to bring back the topic. Thanks to all!!
I'm having trouble with a program. I'm basically checking to see that a SSN is inserted correctly into this format "DDD-DD-DDDD." If its not in that format, then it returns false. My question is, what kind of method or constructor do I use to even begin? I'm having trouble finding one that points me into a direction. My initial thought is to have a method where it checks whether the fourth and seventh character has "-" and make sure the string has a length of 11 characters. But its the method to check for this "-" that i don't know what to do. I've looked at all the java.lang.String, Character, and StringBuilder and none of them seems to fit. A moose kick in the right direction will help out a lot so i could look into it!

Thanks!
14 years ago

Ernest Friedman-Hill wrote:Note that after you sort an array, duplicate ints will be adjacent to each other. Does that help you?



Ha! That's funny... I went to the teacher and said the exact same thing!! The reason I sorted the array was because I was following the advice of another classmate that mentioned that it's easier to remove if they're all lined up. But in fact, no.

So i spoke to my teacher and there's something he really helped me with. Yes, the idea is to create a loop where it returns true, and then have a method that invokes it if its true. Also, this loops is suppose to read each individual integer from one list to the other and when it finds one, set a count for it. However we couldn't get much into it but I'm a little slow at how to do this. I've never done methods, let alone loops that return a value. Any suggestions to go about it?

BTW, thanks everyone for all contributing in this! You guys are awesome!
14 years ago
So here's my dilemma. I am suppose to have 2 sets of arrays that the user inputs. The program will have to sort through the arrays and check for duplicate integers. For example, if an array has a set [2,3,4,4,5] ,it should read it and print it out as [2,3,4,5]. THEN, it tries to match with the second set of integers and check whether they're the same [2,3,4] [4,3,2] (order does not matter) ; different [2,3,4] [5,6,7] . There's more combinations but I am trying to go at it one by one. If you guys help me out here I would gladly appreciate it!

For now though, the first thing I need to know is to how I can eliminate duplicates. I think im suppose to use the linear search method.
I just don't know how to use the linear search method. The example in my homework is not very good in terms of how to use it. I'm so lost just in this part alone for the past 2 days now.

lol... sorry if im rambling. Im just stressed. Here's my code (which is a workable program btw! Thanks!

UPDATE: Shoot... I don't think It's the linear function i need to use to find duplicates....

14 years ago
HEEELLLOOOOOOOOOOOOOO!!! I am sorry. Was that too loud? Was I suppose to whisper? I don't know. I'm new. Anyway I need help this semester so I hope you guys bear with me. I have a horrible teacher and is making me lose interest as I typing. Hopefully you guys can help.

Basically what I am trying to to is adding numbers in a string that the user inputs.

Here's my exercise:

I am trying to calculate the checksum for an ISBN number. There's more into the formula than what the program below has, but that isn't the problem. The problem is dissecting each individual number the user inputs so it will represent d1,d2,d3,d4 and then fit into the formula.

Here's what I have so far...



import java.util.Scanner;

public class ISBNExercise {
public static void main(String[] args) {

//Create a Scanner
Scanner input = new Scanner(System.in);

System.out.printf("Enter the first 9 numbers of an ISBN: ");
int firstNumbers = input.nextInt();

//int calcFirstNumbers = (d1 * 10 + d2 * 9 + d3 * 8 + d4 * 7 + d5 * 6 + d6 * 5 + d7 * 4 + d8 * 3 + d9 * 2);
//System.out.println("Adding the first 9 numbers equals to: " + calcFirstNumbers);

System.out.print("Your 10-digit ISBN Number is: " + firstNumbers);

}
}


I appreciate the help! Thanks!
15 years ago