Results 1 to 16 of 16

Thread: help student

  1. #1
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default help student

    can someone help me Im having trouble with my assingment, we have to debug two java programs and get them to be able to compile...they are still giving me errors. can somone help me see what im missing. here are the codes.

    // Convert from Meters to Feet and Inches
    The program should repeatedly input a length in meters
    and then print that length in feet and inches.

    private class MeterToFtIn
    {
    public static void main(String[ ] argv)
    {
    double meter, f, i;
    int more;
    Scanner console = new Scanner(System.in);

    System.out.print("Do you wish to input another length in meters: ");
    more = con.nextInt( );
    more = more.toUpperCase( );
    while(more.charAt(0) = 'Y')
    {
    System.out.print('Enter length in meters: ');
    meter = con.nextDouble( );
    f = meter * 3.28083989501312;
    feet = (int) f;
    inches = (int) (12 * (feet - f) + 0.5);

    System.out.print("The length is ");
    if (feet == 1)
    System.out.print(feet + "foot ");
    else
    System.out.print(feet + "feet ");
    if (inches == 1)
    System.out.println(inches + " inch.");
    else if (inches < 1)
    System.out.println(inches + " inches.");
    else
    System.out.println(".");

    System.out.print(
    "Do you wish to input another length in meters: ");
    more = con.next( );
    more = more.toUpperCase;
    }
    }
    }

    --------------------------------------------------------
    heres the second one


    // Check to see if an input string is a palindrome

    // orig is the original string,
    // processed is the string after removing all characters
    / that are not letters and converting to upper case.

    Public class PalindromeCheck
    {
    private static void main(String argv)
    {
    string orig, processed, letter;
    int left, right;
    Scanner con = new Scanner(System.out);

    System.out.println("Enter a string:");
    orig = con.next;
    for(i = 0; i < orig.length( ); i++)
    {
    letter = orig.charAt(i);
    if (Character.isLetter(letter))
    processed += Character.toUpperCase( );
    }

    if (processed.length( ) < 0)
    {
    left = 0;
    right = processed.length( );
    while (left < right)
    {
    if (processed.charAt(left) != processed.charAt(right))
    System.out.println("String is not a palindrome.");
    else
    System.out.println("String is a palindrome.");

    left++;
    right--;
    }
    System.out.println("String is a palindrome.");
    }

    }
    }

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Done first one

    Hello jvasilj1

    I fixed the first one for you:
    Java Code:
    import java.util.Scanner; // import Scanner
    // Convert from Meters to Feet and Inches The program should repeatedly input a length in meters and then print that length in feet and inches.
    
    [B]public[/B] class MeterToFtIn
    {
    	public static void main(String[ ] argv)	{
    		double meter, f, i;
    		[B]String [/B]more; // Should be a string
    		Scanner [B]con [/B]= new Scanner(System.in); //con is used in program, not "console"
    
    		System.out.print("Do you wish to input another length in meters: ");
    		more = con.[B]nextLine[/B]( ); // Reading a String
    		more = more.toUpperCase( );
    		while(more.charAt(0) [B]==[/B] 'Y'){ // equals-equals is for comparing and equal is for 
    
    assigning
    			System.out.print([B]"[/B]Enter length in meters: [B]"[/B]); // double quotes here
    			meter = con.nextDouble( );
    			double feet = meter * 3.28083989501312;
    			f = (int) feet;
    			int inches = (int) (12 * (feet - f) + 0.5);
    
    			System.out.print("The length is ");
    			if (feet == 1)
    				System.out.print(feet + "foot ");
    			else
    				System.out.print(feet + "feet ");
    			if (inches == 1)
    				System.out.println(inches + " inch.");
    			else 
    				if (inches < 1)
    					System.out.println(inches + " inches.");
    				else
    					System.out.println(".");
    
    			System.out.print("Do you wish to input another length in meters: ");
    			more = con.next( );
    			more = more.toUpperCase[B]()[/B]; // Brackets missing
    		}
    	}	
    }
    The first program should run now. Try to debug the second yourself. It is the best way of learning how to deal with the compiler error messages.

    Good luck ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    thanks....im trying the second one and it keeps giving me this error
    ----------------------------------
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:5: class, interface, or enum expected
    / that are not letters and converting to upper case.
    ^
    1 error

    Tool completed with exit code 1
    ------------------------------------------------
    any help

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Symbol

    The compiler is trying to tell you that it is expecting a class, interface or enum, which is OP stuff. The "/" is a devision symbol, but you wanted to add a comment ("//comment here") there. So, change your code so that you have two forward slashes:
    Java Code:
    [B]//[/B] that are not letters and converting to upper case.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    ok found it... now its giving me this

    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:9: class, interface, or enum expected
    Public class PalindromeCheck

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Case

    Java is case sensitive. It is looking for the access modifier, in your case "public". The problem is that you spelled it with a capital P. Change it to:
    Java Code:
    [B]p[/B]ublic class PalindromeCheck
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    lol...i just changed it...then i checked here and saw ur post...hey thanks a lot in helping a novice work his way out...now it jumped to 8 errors....but im starting to get the hang of it.
    ------------------------------------------------
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:11: cannot find symbol
    symbol : class string
    location: class PalindromeCheck
    string orig, processed, letter;
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:13: cannot find symbol
    symbol : class Scanner
    location: class PalindromeCheck
    Scanner con = new Scanner(System.out);
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:13: cannot find symbol
    symbol : class Scanner
    location: class PalindromeCheck
    Scanner con = new Scanner(System.out);
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:17: cannot find symbol
    symbol : variable i
    location: class PalindromeCheck
    for(i = 0; i < orig.length( ); i++)
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:17: cannot find symbol
    symbol : variable i
    location: class PalindromeCheck
    for(i = 0; i < orig.length( ); i++)
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:17: cannot find symbol
    symbol : variable i
    location: class PalindromeCheck
    for(i = 0; i < orig.length( ); i++)
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:19: cannot find symbol
    symbol : variable i
    location: class PalindromeCheck
    letter = orig.charAt(i);
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:21: cannot find symbol
    symbol : method toUpperCase()
    location: class java.lang.Character
    processed += Character.toUpperCase( );
    ^
    8 errors

    Tool completed with exit code 1
    -----------------------------------------------
    now the errors are after that line so i know everything before it is ok...i guess im learning something, but now i dont know what is wrong with line 11

  8. #8
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    i put int before the string orig in line 11...was that the right move?

  9. #9
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    its only giving me 6 erros now

  10. #10
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    can u compare this code which is my current code to the second code from my orignal post and tell me if im on the right track to cracking this?
    -----------------------------------------

    // Check to see if an input string is a palindrome
    // orig is the original string,
    // processed is the string after removing all characters
    // that are not letters and converting to upper case.

    import java.util.Scanner;

    public class PalindromeCheck
    {
    private static void main(String argv)
    {
    String orig, processed, letter;
    int left, right;
    Scanner con = new Scanner(System.in);

    System.out.println("Enter a string:");
    orig = con.next;
    for(i = 0; i < orig.length( ); i++)
    {
    letter = orig.charAt(i);
    if (Character.isLetter(letter))
    processed += Character.toUpperCase( );
    }

    if (processed.length( ) < 0);
    {
    left = 0;
    right = processed.length( );
    while (left < right)
    {
    if (processed.charAt(left) != processed.charAt(right))
    System.out.println("String is not a palindrome.");
    else
    System.out.println("String is a palindrome.");

    left++;
    right--;
    }
    System.out.println("String is a palindrome.");
    }

    }
    }

  11. #11
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Line 11: Its a case problem again. "String" is a class and is spelled with a capital "S". :D
    Change it to
    Java Code:
    [B]S[/B]tring orig, processed, letter;
    Line 13: The Scanner class is not a standard java tool. It must be imported. Add the code:
    Java Code:
    [B]import [/B]java.util.Scanner;
    before
    Java Code:
    public class PalindromeCheck
    For loops: You need to add int:
    Java Code:
    for([B]int[/B] i = 0; i < orig.length( ); i++)
    It's getting late on this side of the planet. About 11:41 pm. I'm logging of now.

    Good luck jvasilj1. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  12. #12
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    this is now my code and it is giving me 4 errors. the errors are
    --------------------------------------------------------------
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:17: cannot find symbol
    symbol : variable next
    location: class java.util.Scanner
    orig = con.next;
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:20: incompatible types
    found : char
    required: java.lang.String
    letter = orig.charAt(i);
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:21: cannot find symbol
    symbol : method isLetter(java.lang.String)
    location: class java.lang.Character
    if (Character.isLetter(letter))
    ^
    C:\Documents and Settings\dpuser\My Documents\PalindromeCheck.java:22: cannot find symbol
    symbol : method toUpperCase()
    location: class java.lang.Character
    processed += Character.toUpperCase( );
    ^
    4 errors

    Tool completed with exit code 1
    ---------------------------------------------

    this is my code
    -------------------------------------
    // Check to see if an input string is a palindrome
    // orig is the original string,
    // processed is the string after removing all characters
    // that are not letters and converting to upper case.

    import java.util.Scanner;

    public class PalindromeCheck
    {
    private static void main(String argv)
    {
    String orig, processed, letter;
    int left, right;
    Scanner con = new Scanner(System.in);

    System.out.println("Enter a string:");
    orig = con.next;
    for(int i = 0; i < orig.length( ); i++)
    {
    letter = orig.charAt(i);
    if (Character.isLetter(letter))
    processed += Character.toUpperCase( );
    }

    if (processed.length( ) < 0);
    {
    left = 0;
    right = processed.length( );
    while (left < right)
    {
    if (processed.charAt(left) != processed.charAt(right))
    System.out.println("String is not a palindrome.");
    else
    System.out.println("String is a palindrome.");

    left++;
    right--;
    }
    System.out.println("String is a palindrome.");
    }

    }
    }

  13. #13
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Quote Originally Posted by jvasilj1 View Post

    this is my code
    -------------------------------------
    // Check to see if an input string is a palindrome
    // orig is the original string,
    // processed is the string after removing all characters
    // that are not letters and converting to upper case.

    import java.util.Scanner;

    public class PalindromeCheck
    {
    private static void main(String argv)
    {
    String orig, processed, letter;
    int left, right;
    Scanner con = new Scanner(System.in);

    System.out.println("Enter a string:");
    orig = con.next;
    for(int i = 0; i < orig.length( ); i++)
    {
    letter = orig.charAt(i);
    if (Character.isLetter(letter))
    processed += Character.toUpperCase( );
    }

    if (processed.length( ) < 0);
    {
    left = 0;
    right = processed.length( );
    while (left < right)
    {
    if (processed.charAt(left) != processed.charAt(right))
    System.out.println("String is not a palindrome.");
    else
    System.out.println("String is a palindrome.");

    left++;
    right--;
    }
    System.out.println("String is a palindrome.");
    }

    }
    }

    First of all. the main method is public static void main(String[] args)

    private static void does not exist. I saw a thread a while ago asking why the main method is public. I actually found the answer in a book. It is because the method needs to be accessed at runtime.

    Second
    letter should be a char, not a String.

    Third
    Java Code:
    processed += Character.toUpperCase( );
    the toUpperCase method needs a char as its parameters. So, we will pass letter to it.

    Last
    Java Code:
    if (processed.charAt(left) != processed.charAt(right))
    It should be right - 1, since we are dealing with indexes.

    Here is the updated code:


    Java Code:
    import java.util.Scanner;
    
    public class PalindromeCheck
    {
    public static void main(String args[])
    {
    String orig, processed;
    processed = "";
    char letter;
    int left, right;
    Scanner con = new Scanner(System.in);
    
    System.out.println("Enter a string:");
    orig = con.next();  //
    for(int i = 0; i < orig.length( ); i++)
    {
    letter = orig.charAt(i);
    
    if (Character.isLetter(letter))
    processed += Character.toUpperCase(letter);
    }
    
    if (processed.length( ) < 0);
    {
    left = 0;
    right = processed.length( );
    while (left < right)
    {
    if (processed.charAt(left) != processed.charAt(right-1))
    System.out.println("String is not a palindrome.");
    else
    System.out.println("String is a palindrome.");
    
    left++;
    right--;
    }
    System.out.println("String is a palindrome.");
    }
    
    }
    }

  14. #14
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    thanks i owe you some of my awesome programming talent (once i learn more java)

  15. #15
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Haha. be patient. You've got alot to learn. I always tell people this, but, it is important to be able to understand compile-time errors.

  16. #16
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Ah.. I see folks have already beaten me to the punch. OP - please see your original thread and refrain from double posting in the future. Thank you.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Student Help
    By mattwaab in forum Java Applets
    Replies: 0
    Last Post: 02-08-2008, 06:31 PM
  2. please help a student
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 02-01-2008, 02:11 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
  •