java hw

Joined
Nov 26, 2006
Messages
620
hello so Im taking my first java programming class this semester and I have no idea what im doing. Anyway so the problem I have for hw reads "Write a program that reads a sentence from a dialog box. The sentence has been encrypted as follows: only the first five even-numbered characters should be counted; all other characters should be discarded. Decrypt the sentence and output the results. For example, if the user inputs 'Hiejlzl3ow', your output should be 'Hello'."

so all I can figure out how to do is create the input and output dialoge boxes, I have no idea how to go about decrypting the message. Anyway heres what ive got so far.

import javax.swing.JOptionPane;

public class Project2
{
public static void main(String[] args)
{
String encryptedmessage = JOptionPane.showInputDialog(null,"Please input the encrypted message");
JOptionPane.showMessageDialog(null,encryptedmessage);
}

}
 
you should easilly be able to take it a bit further than that

look into String's toCharArray() method, try setting up a loop to iterate through each character in the array until you get the 5 you want. consider using an "if(i % 2 == 0)" statement in the loop to decide whether or not you're on an even number character, also consider looping through in increments greater than 1 to avoid landing on characters you want to avoid in the first place.
 
It looks like you are on the right track. Perhaps the language of the problem is tripping you up. You're not really "decrypting" the string, rather you're just pulling out some characters in certain positions of the string.

Take a look at the String API [1]. Look for methods that deal with the underlying characters in the string.

Also, have you learned about loops or conditional statements yet?

[1] http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
 
ok so the following is what I have now, but I still don't know how to form the conditional statement for this.

import javax.swing.JOptionPane;

public class Project2
{
public static void main(String[] args)
{
String encryptedmessage = JOptionPane.showInputDialog(null,"Please input the encrypted message");
char[] decryptedmessage = encryptedmessage.toCharArray();
JOptionPane.showMessageDialog(null,decryptedmessage);
}

}
 
Well, depending on how you write the code, you'll either need a loop & a conditional statement or multiple conditional statements.

You'll also need some way of building the final string.
 
Well, depending on how you write the code, you'll either need a loop & a conditional statement or multiple conditional statements.

You'll also need some way of building the final string.

yea I don't know how to do that. Im a complete noob at this.
 
ive been misunderstanding this problem. Anyway heres what Ive got so far except that I dont know what method to call in StringBufffer in order to delete every other character.

import javax.swing.JOptionPane;

public class Project2
{
public static void main(String[] args)
{
String encryptedmessage = JOptionPane.showInputDialog(null,"Please input the encrypted message");
StringBuffer sb = new StringBuffer();
sb.append(encryptedmessage) *NOTE: I have no idea what method to call here*;
String decryptedmessage = sb.toString();
JOptionPane.showMessageDialog(null,decryptedmessage);
}
 
Don't worry about StringBuffer for now..focus on getting the concepts down, then worry about making it efficient.

Your task is to "decrypt" the message. The replies in this thread point you in the right direction: you need some way to iterate over the contents of the String to get the characters you want, right?

Your instructor gave you the rest: you need the first five even -numbered characters in the String.

So you need a loop -- it'd be easiest to use a "for" loop -- look it up on google or in your text book. The first reply explained that you could either use a conditional ("if" statement) or just increment the loop variant appropriately (by what value for even numbers?).
 
ive been misunderstanding this problem. Anyway heres what Ive got so far except that I dont know what method to call in StringBufffer in order to delete every other character.

Think of it another way, instead of trying to "delete" some characters, why not try to "pull out" the ones you want and put them into a new string?
 
ok so again Im a serious noob to this. anyway here is how far ive gotten. Am I getting on the right track? the code posted is incomplete it wont compile but I assumed as much

import javax.swing.JOptionPane;

public class Project2
{
public static void main(String[] args)
{
String encryptedmessage = JOptionPane.showInputDialog(null,"Please input the encrypted message");
char[] array = encryptedmessage.toCharArray();
for(int pos = 0; pos % 2 == 0; pos++);
*something missing*
JOptionPane.showMessageDialog(null,decryptedmessage);
}

}
 
It's close.

What you want to learn to do early is use what is called pseudocode. All pseudocode is is English(or your native language) describing what you want to do, and I hope you know how to write regular sentences.

For example, for this problem you might write something like:


Input a string into a dialog box
Get the string from the dialog box into a local variable.
Iterate through each character in the string
Pull out every other char (the even ones)
Put that character into another string
end the iteration when I am done with the original string
return the new string


That's what pseudocode looks like.

You would then go through the above and turn each sentence into code, only concerning yourself with one sentence at a time. When you have no more sentences, you should be done, or very close to it.
 
You're close. You need to tell the compiler what the 'decryptedMessage' is -- right now it doesn't know that. Also, you don't need both of "pos % 2 == 0; pos++"

In a for loop, the first item between the parenthesis lets you declare your loop variant:
Code:
for (int i = ...
'i' will be the the variable you use vary what you do in the loop, since the loop will handle changing it for you. You assign it the value you want to start your loop at. Be mindful that in most languages, indexed data structures are numbered from 0 to n-1 instead of 1 to n.

The second is the terminating condition:
Code:
for (int i = ...; i < ...
That tells the compiler (or runtime, depending on the language) when to stop looping -- if you didn't have this, your program would get stuck forever! Maybe you should stop the loop when you're out of characters to read..?

The last is how you want to modify the 'i' variable:
Code:
//get the 'encrypted' message somehow
...
for (int i = ...; i < ...; i...)
{
    //do something here to get the right characters and store them in the decrypted message
    ... 
}
//show the finished message? what if it's longer than 5 characters?  :)
...
It's probably best that you only increment 'i' by one, since you should handle the case where the length of the string is odd. You also need to make sure you terminate the loop when there's no more string to iterate over (you don't want to assume a length longer than what is provided as input to the program, right?).

I hope that helps.
 
how would I go about extracting the characters from the array into a string? ive tried a few different methods (arraytoString and toString) but cant seem to make any headway. BTW I really appreciate all the help!
 
the java String class has a constructor that accepts a char[] as an argument, and creates the string from that. It even has another version that lets you specify an offset & length to use.

Since your 'encrypted' string is (at most) 5 characters, you could use a char[5] to store the values, and if it winds up being less than that, pass in the smaller length to the String constructor.
 
Well, the data extraction depends on the source datatype. For example, if you're extracting characters from an array, you might try indexing into the array (lists and other datatypes are different in Java, you'll use methods to access their data. In C#, you can typically just index them like you would an array in Java):

Code:
char[] someCharacters ..
String message = "";

for (.......)
{
    message = message + someCharacters[i];
}

someCharacters will implicitly have toString called on it, in this case.

If you're extracting the data directly from a string, it'd be more like:

Code:
String someString ..
String message = "";

for (.......)
{
    message = message + someString.charAt(i);
}

Java strings are immutable. That means that every time you append something to a String using the + operator, memory is reallocated, etc. That makes this process inefficient, hence the use of StringBuilder, etc. as suggested by others in this thread. My suggestion to you is to solve the task at hand and learn the principles behind the problem (that is iterating over a data structure to capture only the data you're interested in) and worry about the details related to the language later (after you've solved the problem). Languages are tools, in my opinion, so use the correct language for the job (there might be many). Having said that, it's still important to know these language details so when you do have to use such a tool for a given task, you can implement it in the best way possible.

This page might be helpful to you:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
 
I finally got it to work correctly, everyone thank you for your help! heres a copy of the final code tell me if this is what you had in mind.

import javax.swing.JOptionPane;

public class Project2
{
public static void main(String[] args)
{
String encryptedmessage = JOptionPane.showInputDialog(null,"Please input the encrypted message");
int n = encryptedmessage.length();
String decryptedmessage = "";
for (int i=0; i<n; i+=2){
decryptedmessage += encryptedmessage.charAt(i);
}
JOptionPane.showMessageDialog(null,decryptedmessage);
}

}
 
I finally got it to work correctly, everyone thank you for your help! heres a copy of the final code tell me if this is what you had in mind.

import javax.swing.JOptionPane;

public class Project2
{
public static void main(String[] args)
{
String encryptedmessage = JOptionPane.showInputDialog(null,"Please input the encrypted message");
int n = encryptedmessage.length();
String decryptedmessage = "";
for (int i=0; i<n; i+=2){
decryptedmessage += encryptedmessage.charAt(i);
}
JOptionPane.showMessageDialog(null,decryptedmessage);
}

}

Use code tags -- it helps us. Actually, that code will probably choke and throw an exception if your instructor gives it a string of odd length. Also, you're not returning only the first five characters of th decrypted message. See the page I linked to earlier to see how to get part of a string.

Edit: I shrunk/italicized the invalid info -- you should ignore it. Thanks to LuminaryJanitor for catching my goof.
 
Where, in the loop? It shouldn't ever execute if the index is out of bounds. Looks fine to me.

You're right. My mistake. For whatever reason I was envisioning it running one iteration longer (whoops). I think the OP still needs to throw out some characters in the case that his final message is too long though, based on his/her description of the problem.
 
yah based on the info he could probably just run the loop from i=0 to 8

its working though, thats the important part
 
yah, for some reason i got the impression that the length of the unencrypted string was always fixed at 5, but that's a big assumption. better safe than sorry.
 
OP, take a look at the "break" statement to get around this one :)
...Why?

You use break statements to bail out from the middle of a loop. Since this one has only one line of code, it doesn't have a middle. So you'll only be bailing out at the start or at the end, and you'll always be able to get the loop condition to take care of it.

What I'm trying to say is that
Code:
while(condition1)
{
    if(condition2)
        break;
    ...
}
is functionally equivalent to
Code:
while(condition1 && condition2)
{
    ...
}
 

You could use it in either place -- if he put it at the top of the loop, he could use it to check the length of the string and break out or he could do it as part of the terminating condition as well. There are multiple ways to solve the problem. I guess I'm just saying I agree but there's more than one way to "skin a cat" -- the terminating condition is probably better (cleaner).
 
Back
Top