Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    sanjayreddy is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default simple program code please

    the number of occurrences of a specified character in a string

    i tried to do the program occurrences in a given string and i tried the code as below one can anyone let me know the code error please i tried to fix it..but not getting i am new to java please help me out....



    thanks,
    jay


    code:
    import java.util.*;

    public class Occurrence
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a word: ");
    String str = input.nextLine();
    System.out.print("Enter a letter: ");
    char a = input.nextChar();
    int letterCheck = Occurrence.count(str, a);
    System.out.println("The word was: " + str);
    System.out.println("The letter " + a + " was found this many times: " + letterCheck);

    }
    public static int count(String str, char a)
    {
    int count = 0;
    for (int i = 0; i < str.length(); i++)
    {
    if (str.charAt(i) == a)
    {
    count++;
    }
    }
    return count;
    }
    }

  2. #2
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: simple program code please

    include java.Scanner;
    add above library to your program.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: simple program code please

    Quote Originally Posted by rahil.khan View Post
    include java.Scanner;
    add above library to your program.
    Did you try that before you posted?

    Also, Scanner isn't a 'library' and Java isn't C

    db
    Last edited by DarrylBurke; 10-16-2014 at 07:41 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: simple program code please

    Quote Originally Posted by sanjayreddy View Post
    can anyone let me know the code error please
    If you know there's an error, there must have been an error message. Would you like to share that, or it this a guessing game?

    Also, use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: simple program code please

    Java Code:
    public class MyClass
    {
    static String str;
    static char a;
    
    public static void main(String []args)
    {
    Scanner input = new Scanner(System.in);
     System.out.print("Enter a word: ");
     String str = input.nextLine();
     System.out.print("Enter a letter: ");
     char a = input.next().charAt(0);
     int letterCheck = MyClass.count(str, a);
     System.out.println("The word was: " + str);
     System.out.println("The letter " + a + " was found this many times: " + letterCheck);
    
     }
     public static int count(String s, char b)
     {
     int count = 0;
     for (int i = 0; i < s.length(); i++)
     {
     if (s.charAt(i) == b)
     {
     count++;
     }
     }
     return count;
    }
    }
    try the above code. I don't have java compiler on my machine was trying to compile it online but I got failed to do so. reply please after compiling, sorry for the above mistake.
    Last edited by rahil.khan; 10-16-2014 at 09:47 AM.

  6. #6
    sanjayreddy is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: simple program code please

    Quote Originally Posted by DarrylBurke View Post
    If you know there's an error, there must have been an error message. Would you like to share that, or it this a guessing game?

    Also, use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    db


    you asked me the error message. so this is the error sir...

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method nextChar() is undefined for the type Scanner

    at Occurrence.main(Occurrence.java:10)

  7. #7
    sanjayreddy is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: simple program code please

    i use eclipse to run this program
    and i found the error above posted.

  8. #8
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: simple program code please

    Compile my program... which I posted..

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: simple program code please

    Quote Originally Posted by rahil.khan View Post
    Compile my program... which I posted..
    Please don't spoonfeed.
    It should be possible to guide the poster to the solution.

    sanjayreddy: that error means that the compiler cannot find the nextChar() method in the Scanner class.
    A look at the Scanner API will show you that the compiler is quite correct.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: simple program code please

    inside the count meth you are using str intead of s which is null..

    make as s and try ...learn why its null ..just a test for u :-)

    if (s.charAt(i) == b)
    {
    count++;
    }
    Ramya:cool:

  11. #11
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: simple program code please

    its null because string takes null as default value.

    i told you that i don't have any compiler on my machine, JDK is not allowed here for security purposes....

    compile it for me.

  12. #12
    Ramya Baskar is offline Member
    Join Date
    Sep 2014
    Posts
    1
    Rep Power
    0

    Default Re: simple program code please

    Hey the problem is, nextChar() method is not suported by Scanner.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: simple program code please

    Quote Originally Posted by rahil.khan View Post
    compile it for me.
    You'd better ask nicely; we have a Dutch saying that goes (basically translated): command your little dog and bark yourself.

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: simple program code please

    Why do you declare class variables (i.e. static) and then re-declare them as local inside of main?
    And why should someone compile a program for you that you obviously can't run?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: simple program code please

    Quote Originally Posted by JosAH View Post
    You'd better ask nicely; we have a Dutch saying that goes (basically translated): command your little dog and bark yourself.

    Jos
    Plz compile it for me. is that ok nw. I am asking nicely.. :)

  16. #16
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: simple program code please

    Quote Originally Posted by jim829 View Post
    Why do you declare class variables (i.e. static) and then re-declare them as local inside of main?
    And why should someone compile a program for you that you obviously can't run?

    Regards,
    Jim
    I am still new to Java, I m still far away from optimizing the code.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: simple program code please

    Quote Originally Posted by rahil.khan View Post
    Plz compile it for me. is that ok nw. I am asking nicely.. :)
    Why should I? I have a bunch of code that implements lisp-like evaluation without an actual model (it fiddle-diddles with strings); are you going to test it for me if I ask nicely? Pleeez? pretty pleeeez with sugar on top?

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  18. #18
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: simple program code please

    Quote Originally Posted by JosAH View Post
    Why should I? I have a bunch of code that implements lisp-like evaluation without an actual model (it fiddle-diddles with strings); are you going to test it for me if I ask nicely? Pleeez? pretty pleeeez with sugar on top?
    Jos
    I think you are not speaking the correct language. Let me translate that for you:
    Why shd I? I hv a bnch of cod that implmnts lisp-lke eval w/o an act model (it f/d wth str); R u gng 2 tst it 4 me f I ask nc?Plzzz? Pr plzzz w/ sgr on tp.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  19. #19
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: simple program code please

    Quote Originally Posted by JosAH View Post
    Why should I? I have a bunch of code that implements lisp-like evaluation without an actual model (it fiddle-diddles with strings); are you going to test it for me if I ask nicely? Pleeez? pretty pleeeez with sugar on top?

    Jos
    if I have any knowledge about that code or anything I will love to help just not u, I will help anybody who asked it for.....

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: simple program code please

    Quote Originally Posted by rahil.khan View Post
    if I have any knowledge about that code or anything I will love to help just not u, I will help anybody who asked it for.....
    I don't know if you realized it, but this is a forum; we help people with their code here; this forum is not an online compiler (you can download a compiler (and more) for free).

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  2. Replies: 5
    Last Post: 11-23-2012, 02:00 AM
  3. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  4. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  5. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 03:27 AM

Tags for this Thread

Posting Permissions

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