10 Replies - 33272 Views - Last Post: 07 April 2008 - 02:22 AM Rate Topic: -----

#1 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Unexpected type. Required variable, found value.

Posted 06 April 2008 - 03:09 AM

Well, I'm very new to java and trying to learn as quickly as possible. I've been coding all day, going through my textbook.
I'm near the end of a chapter about loops and if statements and one of the self-assessment questions is creating an encrypted string.
Basically I'm asking the user to input a string, and then "encode" [I know, it's not really encoding] the string but more user inputed variables.

public static void main(String[] args) 
	{
		Scanner sc;
		String toEncode;
		String userEncoded;
		String tempDelim;
		int offset;
		int flag;
		char delim;
		char currChar;
		boolean punctBool;
		int coded_place;
		
		coded_place = 0;
		sc = new Scanner(System.in);
		
		System.out.print("Please enter a string to encode: ");
		toEncode = sc.nextLine();
		System.out.print("Please enter the delimiter you wish to use: ");
		tempDelim = sc.nextLine();
		delim = tempDelim.charAt(0);
		System.out.print("Please enter the amount you wish to shift by: ");
		offset = sc.nextInt();
		System.out.print("True or false -- Would you like to include punctuation: ");
		punctBool = sc.nextBoolean();
	
		System.out.println("\nEncoding \"" + toEncode + "\"");
		System.out.println("Letters will be shifted by \"" + offset + "\"");
		System.out.println("Words delimited by \"" + delim + "\"");
		System.out.println("Including punctuation: \"" + punctBool + "\"");
		
		userEncoded = toEncode;			
		userEncoded = userEncoded.toUpperCase();
		
		for (int i = 0; i < toEncode.length()-1; i++)
		{  
		currChar = toEncode.charAt(i);
		flag = 0;
		
		if ((currChar >= 48) && (currChar <= 57))
		{
			userEncoded.charAt(coded_place) = currChar;
			flag = 1;
		} 
		if ((currChar >= 65) && (currChar <= 90))
		{
			if (currChar + offset > 90)
		{
			userEncoded.charAt(coded_place) = (char)((currChar + offset)-90 + 65);
		}
			else
			{
				userEncoded.charAt(coded_place) = (char)(currChar + offset);
			}
			flag = 	1;
		}
		
		if (flag)
		{
			if (punctBool)
				userEncoded.charAt(coded_place) = currChar;
				
			else
			coded_place = coded_place - 1;
		}
		
		coded_place = coded_place++;
		}
		
		
		System.out.println(userEncoded);
		
	}
		
}


This isn't the finished code, but I decided I should test what I have so far. I've been reading through the API for so long it feel like my brain is melting! :blink:
So yeah, basically I'm getting a few compile errors and I was wondering if anyone could help me?
I'm just going to move on to the next questions, but I'm interested in knowing why this isn't working >_<

Thanks in advance.

This post has been edited by jayman9: 06 April 2008 - 04:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Unexpected type. Required variable, found value.

#2 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Unexpected type. Required variable, found value.

Posted 06 April 2008 - 04:23 AM

Hello there,
Could you please post the errors from the compile?It will be more easy for all i think to detect where the errors are. :D
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: Unexpected type. Required variable, found value.

Posted 06 April 2008 - 07:29 AM

If this is all the code, that static main method needs to belong to a class.
Was This Post Helpful? 0
  • +
  • -

#4 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Unexpected type. Required variable, found value.

Posted 06 April 2008 - 02:48 PM

Sorry, I didn't include the class, but it does belong to one. I have also imported the Scanner method. The errors I get are as follows:

Line 54, 61, 65, 73: Unexpected type. Required variable, found value.

Line 70: found int, required boolean.

As I was typing this I realized I should change the int flag to boolean. I'm using that so I can create a loop so that if flag is set to true, punctuation needs to be handled [it's set to true at the end of the other loops].

But, please help with the other errors! :D
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Unexpected type. Required variable, found value.

Posted 06 April 2008 - 04:17 PM

View Postskin_, on 6 Apr, 2008 - 02:48 PM, said:

Sorry, I didn't include the class, but it does belong to one. I have also imported the Scanner method. The errors I get are as follows:

Line 54, 61, 65, 73: Unexpected type. Required variable, found value.

Line 70: found int, required boolean.

As I was typing this I realized I should change the int flag to boolean. I'm using that so I can create a loop so that if flag is set to true, punctuation needs to be handled [it's set to true at the end of the other loops].

But, please help with the other errors! :D


How can we reference the line number if we do not have all the code ?
You will have to post your whole .java file
Was This Post Helpful? 0
  • +
  • -

#6 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Unexpected type. Required variable, found value.

Posted 06 April 2008 - 06:07 PM

Ah sorry, I didn't even think about that. I'm at work at the moment but the problem is in the lines that include the statement:
userEncoded.charAt(coded_place) = currChar;

I don't think java likes the coded_place int being in the parenthesis after charAt.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Unexpected type. Required variable, found value.

Posted 06 April 2008 - 06:28 PM

View Postskin_, on 6 Apr, 2008 - 06:07 PM, said:

Ah sorry, I didn't even think about that. I'm at work at the moment but the problem is in the lines that include the statement:
userEncoded.charAt(coded_place) = currChar;

I don't think java likes the coded_place int being in the parenthesis after charAt.


The String method charAt(int n) RETURNS the character at position n in the String
it cannot be used to SET the character at position N
Was This Post Helpful? 1

#8 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Unexpected type. Required variable, found value.

Posted 06 April 2008 - 08:32 PM

fixed

This post has been edited by skin_: 07 April 2008 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Unexpected type. Required variable, found value.

Posted 06 April 2008 - 09:06 PM

   for (int i = 0; i < toEncode.length()-1; i++)
   {  
		currChar = toEncode.charAt(i);
	}
		
	if (currChar == ' ')
	{
		currChar = delim;
		System.out.println(userEncoded);
	}



So what are you doing here ?
You pass thru all the characters of toEncode (not all you go to toEncode.lenght() - 1
Why -1 ?

So you fetch all the characters, but the last one, and put it in currChar

once that is done, for the last one you fetch
you check if it is ' '
if it is the case
you replace it in currChar by delim
and you print your original String

may be you want something like:

   userEncoded = toEncode;			
   userEncoded = userEncoded.toUpperCase();
   String outputStr = "";

   for (int i = 0; i < toEncode.length(); i++)
   {  
	   currChar = toEncode.charAt(i);
		if (currChar == ' ')
		{
	currChar = delim;
		 }
		 outputStr += currChar;
   }
   System.out.println(outputStr);



That is your stripped down version without the shift and all the others cipher actions
Was This Post Helpful? 0
  • +
  • -

#10 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Unexpected type. Required variable, found value.

Posted 07 April 2008 - 01:28 AM

,,,,,,,,,,,,,,,,,,

This post has been edited by skin_: 07 April 2008 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#11 skin_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 05-April 08

Re: Unexpected type. Required variable, found value.

Posted 07 April 2008 - 02:22 AM

............................

This post has been edited by skin_: 07 April 2008 - 07:43 PM

Was This Post Helpful? 1

Page 1 of 1