Results 1 to 14 of 14
  1. #1
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Problem with very simple program

    Hello everybody

    I am very new to java and my professor have given me an exercise for the weekend.

    The exercise says the following things

    Write a program which reads an arbitrary amount of characters. As the termination criterion use the input of a dot. (Realization once with the while-loop and once with the repeat-loop.)

    I tried to make it (with simple commands that i know), but it doesn't complies it because I have some mistakes and I don't know how to correct them.

    Please help me if you can

    code

    class kastora {

    public static void main(String[] args) {

    char word='a';

    while ( word!=".") {

    System.out.println("Enter a Character:");

    word = System.in.read();

    System.out.print("You entered ");

    System.out.println(word);
    }
    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by iratus View Post
    but it doesn't complies it because I have some mistakes and I don't know how to correct them.
    Please paste the full errors, including line numbers, here.

    I suspect it has to do with your code word!="a". word is a char type and "a" is a String type.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by iratus View Post
    Hello everybody

    I am very new to java and my professor have given me an exercise for the weekend.

    The exercise says the following things

    Write a program which reads an arbitrary amount of characters. As the termination criterion use the input of a dot. (Realization once with the while-loop and once with the repeat-loop.)

    I tried to make it (with simple commands that i know), but it doesn't complies it because I have some mistakes and I don't know how to correct them.

    Please help me if you can

    code

    class kastora {

    public static void main(String[] args) {

    char word='a';

    while ( word!=".") {

    System.out.println("Enter a Character:");

    word = System.in.read();

    System.out.print("You entered ");

    System.out.println(word);
    }
    }
    }
    1. first of all the method read() returns a int. so define your word as type int.
    2. the method read() throws an IOException, so you must catch this exception.
    3. adapt all your assignment and operations for word to be a type int.

  4. #4
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Hello again

    I can not copy the errors cause i am working with CMD (not eclipse)---sorry

    Questions

    1) so if i define the 'word' as int will be able somebody to type characters ???
    2) I don't really know what is an exception and IOException (too newbie) !!!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by iratus View Post
    Hello again

    I can not copy the errors cause i am working with CMD (not eclipse)---sorry
    Yes you can: right click the title bar and see what shows up.

    kind regards,

    Jos

  6. #6
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Microsoft Windows [Version 6.1.7600]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\FUCK-OFF>cd C:\Master_Geomatics\Semester_1\Java\excersises\Arr ay

    C:\Master_Geomatics\Semester_1\Java\excersises\Arr ay>C:\Master_Geomatics\Semeste
    r_1\Java\JAVA_COMPILER\jdk1.6.0_22\bin\javac kastora.java
    kastora.java:7: incomparable types: char and java.lang.String
    while ( word!=".") {
    ^
    kastora.java:11: possible loss of precision
    found : int
    required: char
    word = System.in.read();
    ^
    2 errors

    C:\Master_Geomatics\Semester_1\Java\excersises\Arr ay>




    Thanks it worked !!!!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As usual the error message is telling you what is wrong:

    kastora.java:7: incomparable types: char and java.lang.String
    while ( word!=".") {
    word is a char and "." is a String and the compiler is telling you that you can't compare the two with !=. Better to compare word with another char such as '.'.

    Nice subdirectory name by the way. ;)

  8. #8
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    You confused me a little bit !!!

    Can you explain this with one example please ???

    Thanks

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by iratus View Post
    You confused me a little bit !!!

    Can you explain this with one example please ???

    Thanks
    One example coming up:

    Java Code:
    public class Fu4 {
       public static void main(String[] args) {
          char word = 'z';
          
          if (word != 'a') {
             System.out.println("word is not 'a'");
          }
          
          if (word != "a") {
             System.out.println("this won't compile since word is not a String but rather a char");
          }
       }
    }
    Again the problem above and with your code is that "a" is not a char but a String, and you can't compare a char with a String but instead need to compare a char with a char.

    ¿Comprendes?

  10. #10
    ST34LTH is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    instead of
    Java Code:
    char word = "a"
    try

    Java Code:
    String word = "a"
    I think, im not a pro so sorry if its wrong ;)

  11. #11
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    So the dot '.' is a string or a char ???

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One other problem is that you need to use a Scanner or a BufferedReader to get user input and shouldn't directly get user input from System.in.

  13. #13
    ST34LTH is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    ive tried it out and i came up with this
    Java Code:
    import java.util.Scanner;
    public class Kastora 
    {
    	
    
    		public static void main(String[] args) 
    		{
    		Scanner UserInput = new Scanner(System.in);
    		
    		String word = "a";
    		String dot = ".";
    		
    		
    		
    		
    
    		
    		while (word != ".")
    		{
    			if (word.equals(dot))
    			{
    				System.out.println("Invalid Entry");
    				break;
    			}
    			System.out.println("Please enter next character");
    			
    			word = UserInput.next();
    			
    			System.out.println("You entered " + word);
    			
    		}
    
    		}
    		 
    }
    You cant use
    Java Code:
    word != "."
    to comapre letters I think you can only use it for numbers, since it will carry on even if you enter "."

    I just dont know how to put it in the while condition

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Simple program help
    By jtyler in forum New To Java
    Replies: 3
    Last Post: 09-20-2010, 07:43 AM
  2. simple program
    By blastoff in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 11:25 PM
  3. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 02:35 PM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 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
  •