Encoder/Decoder Assignment

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 582 Views - Last Post: 07 October 2013 - 03:35 PM Rate Topic: -----

#1 flapsbro1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-October 13

Encoder/Decoder Assignment

Posted 06 October 2013 - 02:02 PM

Hello, Im new to java, ive found this cool site that it provides sample problems, and im stuck on exercise 4 the last one on the page. I just dont know what to do next. any tips, hints, or something that could help me out would be great.

http://chortle.ccsu....ercises29B.html

this is what i have now.

public class Decoder 
{

	public static void main(System[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		
		String message, number = null;
		
		Decoder secret = new Decoder();
			
		File file = new File("");
		
         }
}



This post has been edited by macosxnerd101: 06 October 2013 - 02:10 PM
Reason for edit:: Please use a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Encoder/Decoder Assignment

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 02:21 PM

You're given a secret phrase and a sequence of indices. You simply find charAt(index), for the given index. The String of those characters is your plaintext.
Was This Post Helpful? 0
  • +
  • -

#3 flapsbro1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-October 13

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 02:29 PM

so i would use like an enhanced for lop with charAt(index)?

and how do i store the first line and the numbers in a string from a file, is it like message.nextLine(); and number.hasNext(); and how is it stored in the object? Im so confused
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 02:38 PM

You can't use an enhanced for loop with Strings.

Quote

and how do i store the first line and the numbers in a string from a file, is it like message.nextLine(); and number.hasNext(); and how is it stored in the object? Im so confused

You would use a Scanner, yes. You will have to play around with the various methods for getting input. Here is an idea though- you read a number, get the character from the key, then repeat. Look at the Scanner docs. How do you determine if there is more input?
Was This Post Helpful? 0
  • +
  • -

#5 flapsbro1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-October 13

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 03:28 PM

so looking at the scanner doc, i use hasNextLine() right?

and to store the stuff from a file would i do

message = kb.nextLine();
number = kb.hasNextLine();

and since i cant use a enhanced for loop do i use the normal one?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 03:30 PM

Use whatever loop (that is syntactically allowed) that you feel works best. It's great to get some help, but take a jump and try working out the logic yourself some. Try and get something going. You might surprise yourself as to how much you can actually accomplish. :)
Was This Post Helpful? 0
  • +
  • -

#7 flapsbro1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-October 13

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 08:53 PM

public class Khodjasarian_Decoder
{
	public static void main(String [] args) throws IOException
	{
Scanner kb = new Scanner(System.in);
          String secretMessage;
          System.out.print("filename: ");
          String filename = kb.nextLine();

          File file = new File(filename);
          Scanner inputFile = new Scanner(file);

          String line1 = inputFile.nextLine();
          String line2 = inputFile.nextLine();

          inputFile.close();

          for(int index = 0; index < line1.length(); index++)
          {
        	  for(int index1 = 0; index1 < line2.charAt(line1); index++)
        	  {
        		  char letter = line1.charAt(index1);
        		    System.out.println (letter); 
        	  }
        		 
          }

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 08:54 PM

Is there a question here?
Was This Post Helpful? 0
  • +
  • -

#9 flapsbro1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-October 13

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 08:55 PM

i accidentally pressed enter, but anyway, i have that and my head is about to explode, so now apperently you cant used charAt with a string. what am i supposed to do, and i think my for loop is not correct
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 08:59 PM

Quote

so now apperently you cant used charAt with a string

That's not true. The charAt() method is defined in the String class.

Quote

what am i supposed to do, and i think my for loop is not correct

What makes you think that? What specific problems or errors are you encountering? Walk (me) through your logic. Now is as good a time as any to work through debugging! :)
Was This Post Helpful? 0
  • +
  • -

#11 flapsbro1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-October 13

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 09:12 PM

This is what i get:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method charAt(int) in the type String is not applicable for the arguments (String)


and my loop is supposed look up each integer in the key-phrase and output the corresponding character. but i dont think thats what my loop is doing, what would i need to do?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 09:16 PM

Why are you using nested for loops? Why are you passing line1 to the charAt() method? The charAt() lookup is like an array lookup. So someArray[index] looks up the value in someArray located at index. The same applies to charAt(), which is why you need an int.

You should be:
-Looping through the ints from the File
-For each int, lookup charAt(currentIndex) from the key String
-Output that character

I would just use one loop.
Was This Post Helpful? 0
  • +
  • -

#13 flapsbro1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-October 13

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 09:40 PM

for(int index = 0; index < line1.length(); index++)
          {
        	 do
        	 {
        		 
        		 
        	 }while(true);
        		 


would something like this work?
or can i just get an example on how its done?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 09:47 PM

Quote

would something like this work?

Probably not. Why are you using nested loops? You should be able to do this in one loop. I've outlined the logic. Focus on each piece. Don't just throw code at this until you get it right. That's a good way to just confuse yourself.

Quote

or can i just get an example on how its done?

No. We're not in the business of finishing assignments for people.
Was This Post Helpful? 0
  • +
  • -

#15 flapsbro1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-October 13

Re: Encoder/Decoder Assignment

Posted 06 October 2013 - 09:51 PM

btw this isnt an assignment just want to finish this its bothering me. never tooken a java class, im not even at the level yet lol
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2