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

    Default Couple of newbie questions

    I have been trying to write a program that asks a sentence and then looks if there's a even amount of characters in that. Here's what i have:

    Java Code:
    public class IsEven {
    
    	public static void isEven() {
    		System.out.println("Give the sentence: ");
    		java.util.Scanner sc = new java.util.Scanner(System.in);
    		String sentence = sc.next();
    		boolean even = ((sentence.length() % 2) == 0);
    		System.out.println(even);
    	}
    }
    What's wrong there?

    Second i have is a problem with method that asks lower limit and upper limit and then gives a random number between them (those two numbers included).

  2. #2
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    please specify how are you running this prog


    are you typing

    java IsEven on command prompt

    than you are doing it wrong
    DevZ;)

  3. #3
    ananasman is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by DevzAbhi View Post
    please specify how are you running this prog


    are you typing

    java IsEven on command prompt

    than you are doing it wrong
    I want to to what's wrong with that code not how to run it.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    The input you are using is not the correct one. Scanner only reads the first part of the sentence (up to the first space). To read the rest of the sentence, you would have to use a loop. You may want to look at the BufferedReader method to read in the whole sentence.
    Character Streams (The Java™ Tutorials > Essential Classes > Basic I/O)
    Sample:
    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    sentence = br.readLine();
    NOTE: Bufferedreader requires try/catch

    Hope this helps,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    ananasman is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    Could you tell me how to write the missing or wrong parts so this will work.

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    change sc.next() to sc.nextLine()

    You don't need a buffered reader for this you just need the proper method. Scanner.next() will only return upto the first space where Scanner.nextLine() will return upto the next carriage feed(new line command)

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default yep... your're right

    xcallmejudasx.. you're correct... the scanner class does have a nextLine() method... I should have checked that ... since I've used the BufferedReader a couple of times, that why I suggested it, but the nextLine() method would be easier to implement.
    Anyway, here's the scanner class with it's methods:
    Scanner (Java Platform SE 6)

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    ananasman is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    It wont work: "Exception in thread..."

    Could you modify the whole thing again so it will work.

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    whats the rest of the error. exception in thread doesn't tell us anything except an error occurred.

  10. #10
    sai
    sai is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    import java.io.*;
    public class ScannerDemo {
    public static void main(String[] args) {


    System.out.println("Give the sentence: ");
    String a="hello hai";
    java.util.Scanner sc = new java.util.Scanner(a);
    String sentence = sc.nextLine();
    boolean even = ((sentence.length() % 2) == 0); System.out.println(even);
    }

    }

    Its working but it counting space also and the answer for this is false.
    can anyone tell me how to enter command line arguments when we are using Eclipse IDE
    Thanks in advance

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default To the both of you ...

    Sai:
    • You are correct in stating that the even/odd (true/false) result is including the sentence's blank spaces, which to a program are also a characters. If it is needed that the even/odd result ONLY include the characters from a-z (excluding blank spaces), a little bit of logic would solve that.
    • As for your other question, I think that you have to open a new post (it's considered rude to hijack somebody's post with unrelated questions).


    ananasman:
    • You know, the tone of your comments/questions sound VERY demanding. This is a volunteer forum... showing some effort (like posting the modified code and complete error msgs), not asking "gimme the whole working thing", and an occasional "thank you" would really go a long way in helping you solve the problems your having with your code. Remember, one of the goals of this forum is for everybody to learn from other people's experience.


    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    sai
    sai is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Hi guys , sry for doing that ,its my mistake,i am new member,i forgot to post my request from my form.
    Sry once again

Similar Threads

  1. Replies: 9
    Last Post: 10-18-2008, 04:31 PM
  2. Some newbie questions about shortcuts
    By nefigah in forum Eclipse
    Replies: 2
    Last Post: 07-29-2008, 08:25 AM
  3. Newbie with questions
    By buzzdsm in forum New To Java
    Replies: 14
    Last Post: 06-05-2008, 04:11 PM
  4. Java assignment - couple methods don't know how to figure out
    By Snowboardmylife in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:52 AM
  5. Couple of Problems
    By joz_12345 in forum Java 2D
    Replies: 2
    Last Post: 03-06-2008, 04:13 PM

Posting Permissions

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