Results 1 to 14 of 14
Thread: Problem with very simple program
- 11-07-2010, 03:33 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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);
}
}
}
- 11-07-2010, 03:37 PM #2
- 11-07-2010, 03:45 PM #3
- 11-07-2010, 03:53 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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) !!!
- 11-07-2010, 03:55 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 11-07-2010, 03:59 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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 !!!!
-
As usual the error message is telling you what is wrong:
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 '.'.kastora.java:7: incomparable types: char and java.lang.String
while ( word!=".") {
Nice subdirectory name by the way. ;)
- 11-07-2010, 04:11 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
You confused me a little bit !!!
Can you explain this with one example please ???
Thanks
-
One example coming up:
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.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"); } } }
¿Comprendes?
- 11-07-2010, 04:16 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
instead of
tryJava Code:char word = "a"
I think, im not a pro so sorry if its wrong ;)Java Code:String word = "a"
- 11-07-2010, 04:21 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
So the dot '.' is a string or a char ???
-
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.
- 11-07-2010, 04:37 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
ive tried it out and i came up with this
You cant useJava 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); } } }to comapre letters I think you can only use it for numbers, since it will carry on even if you enter "."Java Code:word != "."
I just dont know how to put it in the while condition
-
Similar Threads
-
Simple program help
By jtyler in forum New To JavaReplies: 3Last Post: 09-20-2010, 07:43 AM -
simple program
By blastoff in forum New To JavaReplies: 5Last Post: 04-14-2010, 11:25 PM -
Simple Program
By TheRocket in forum Advanced JavaReplies: 15Last Post: 12-30-2008, 02:35 PM -
help with simple program in java
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 08:03 PM -
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks