Results 1 to 5 of 5
  1. #1
    VIhuri is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Problems with code/cannot find symbol

    A total newbie to Java here (although have managed to make three programs succesfully! )

    I need to create a program that asks for a word and if the number of letters is uneven, it should print out first, last and the "median" letter. If not, it should print the first and last letters. This is the code I've written thus far, mostly trying to emulate my other program which dealt with mere numerics and copy-pasting from the lecture powerpoints:

    Java Code:
    import java.util.Scanner;
    
    public class demo1_6 {
    public static void main(String[] args) {
    
    Scanner lukija = new Scanner (System.in);	
    System.out.println ("anna sana ");    
    string a = lukija.nextLine();
    int b = a.length ();
    if (a % 2 /= 0) {
    System.out.println (a.charAt (0) + a.charAt (b / 2) + a.charAt (b)); 
    }
    else {
    System.out.println (a.charAt (0) + a.charAt (b));
    }
    lukija.close();
    }
    }
    When trying to build the file, it gives error messages on the lines where string a and int b are about "cannot find symbol". It's also doubtful my program does anywhere near what it's supposed to do even without error messages. I managed to succesfully create a program which requested user to give three numbers and then print out the median one.

    A poor statistics sod shouldn't be forced to learn Java.

    Thanks for all the help

  2. #2
    VIhuri is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Problems with code/cannot find symbol

    First problem found: obviously it's String, not string.

    Anyway, now it claims the operandi % isn't cool. It's however what the instructions told me to use.

  3. #3
    VIhuri is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Problems with code/cannot find symbol

    Okey, old problems solved, new problems arise.

    Java Code:
    import java.util.Scanner;
    
    public class demo1_6 {
    public static void main(String[] args) {
    
    Scanner lukija = new Scanner (System.in);	
    System.out.println ("anna sana ");    
    String a = lukija.nextLine();
    int b = a.length ();
    if (b % 2 == 0) {
    System.out.println (a.charAt (0) + a.charAt (b));
    }
    else {
    System.out.println (a.charAt (0) + a.charAt (b / 2) + a.charAt (b)); 
    }
    lukija.close();
    }
    }
    Now this file can be built but when ran and given a word, it tells that

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.charAt(String.java:658)
    at demo1_6.main(demo1_6.java:22)
    Now what?

  4. #4
    VIhuri is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Problems with code/cannot find symbol

    Aaaaaand I made it work!

    Java Code:
    import java.util.Scanner;
    
    public class demo1_6 {
    public static void main(String[] args) {
    
    Scanner lukija = new Scanner (System.in);	
    System.out.println ("anna sana ");    
    String a = lukija.nextLine();
    int b = a.length ();
    if (b % 2 == 0) {
    System.out.print (a.charAt(0)); 
    System.out.print (a.charAt(b-1));
    }
    else {
    System.out.print (a.charAt (0));  
    System.out.print (a.charAt (b/2)); 
    System.out.print (a.charAt (b-1)); 
    }
    lukija.close();
    }
    }

    Programming is tedious but when it works it's fun! Yay! Didn't even need help.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Problems with code/cannot find symbol

    Well, for me, the first step should be to indent your code properly, as I can't follow the flow of that.
    Java Code:
    public void someMethod() {
        for (int i = 0; i < someArrayLength; i++) {
            doSomething();
        }
        if (somethingIsTrue) {
            doThis();
        } else {
            doThat();
        }
    }
    as an example.

    As for your error, you are attempting to read the 6th character of a String that has less than 6 characters.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. cannot find symbol
    By FREDISCO in forum New To Java
    Replies: 2
    Last Post: 03-27-2012, 02:11 AM
  2. Replies: 8
    Last Post: 06-15-2011, 05:26 AM
  3. Cannot find symbol
    By Johanis in forum New To Java
    Replies: 19
    Last Post: 11-04-2010, 08:13 PM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •