Help with Scanner class - Chars and Strings

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 49
Reputation: Laidler is an unknown quantity at this point 
Solved Threads: 1
Laidler Laidler is offline Offline
Light Poster

Help with Scanner class - Chars and Strings

 
0
  #1
Nov 24th, 2008
import java.util.Scanner;

class TestTelevisionV3
{
public static void main(String [] args)
{
Television tv = new Television (4, 8, true);
tv.printImage();
System.out.println("");
System.out.println("Press P to turn Power On or Off");
System.out.println("Press C to Change the Channel");
System.out.println("Press V to Change the Volume");
System.out.println("Press Q to quit");
System.out.println("");

Scanner myInput = new Scanner(System.in);
String Input1 = myInput.next();

if(Input1 == "P")
{
tv.setPower();
}
if(Input1 == "C")
{
tv.setChannel();
}
if(Input1 == "V")
{
tv.setVolume();
}
if(Input1 == "Q")
{
tv.setPower();
}
else
{
System.out.println("");
System.out.println("Error - Input not recognised!");
System.out.println("");
}
}
}

I am a university students studying software development.
I have been given a task which involves using the code shown so the user inputs P C V or Q the following method is run.

However whenever i run the code my error message appears.

I am aware that the problems has something to do with the scanning method not being able to find string or character inputs..

can someone help me modify the scanner so that inputting P will run the tv.setPower Method
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Help with Scanner class - Chars and Strings

 
1
  #2
Nov 24th, 2008
Don't worry everybody does that mistake. And I am tired of replying to it.

Just use the equals method.

When you use '==' to compare String (remember Strings are Objects) you compare to see if they are the SAME objects. They are not, but they have the same value:

  1. if (Input1.equals("P")) {
  2. .....
  3. }

  1. String s1="a";
  2. String s2="a"; //2 Different objects.
  3.  
  4. (s1==s2) //false
  5. (s1.equals(s2)) //true

BUT

  1. String s1 = "a";
  2. String s2 = s1; //they are the same
  3.  
  4. (s1==s2) //true
  5. (s1.equals(s2)) //true

Also you will need to have all of your IFs as 'else if':

  1. if () {
  2.  
  3. } else if () {
  4.  
  5. } else if () {
  6.  
  7. } .... {
  8.  
  9. } else {
  10. System.out.println("");
  11. System.out.println("Error - Input not recognised!");
  12. System.out.println("");
  13. }
Last edited by javaAddict; Nov 24th, 2008 at 11:28 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 49
Reputation: Laidler is an unknown quantity at this point 
Solved Threads: 1
Laidler Laidler is offline Offline
Light Poster

Re: Help with Scanner class - Chars and Strings

 
0
  #3
Nov 24th, 2008
I have implemented what you have just stated but it hasnt solved my problem

Here is the code

import java.util.Scanner;

class TestTelevisionV3
{
public static void main(String [] args)
{
Television tv = new Television (4, 8, true);
tv.printImage();
System.out.println("");
System.out.println("Press P to turn Power On or Off");
System.out.println("Press C to Change the Channel");
System.out.println("Press V to Change the Volume");
System.out.println("Press Q to quit");
System.out.println("");

Scanner myInput = new Scanner(System.in);
String Input1 = myInput.next();

String s1="P";
String s2="C";
String s3="V";
String s4="Q";

if(Input1.equals (s1))
{
tv.setPower();
}
if(Input1.equals (s2))
{
tv.setChannel();
}
if(Input1.equals (s3))
{
tv.setVolume();
}
if(Input1.equals (s4))
{
tv.setPower();
}
else
{
System.out.println("");
System.out.println("Error - Input not recognised!");
System.out.println("");
}
}
}

Here is the output

Press P to turn Power On or Off
Press C to Change the Channel
Press V to Change the Volume
Press Q to quit

P

Error - Input not recognised!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Help with Scanner class - Chars and Strings

 
0
  #4
Nov 24th, 2008
Originally Posted by Laidler View Post
if(Input1.equals (s1))
{
tv.setPower();
}
if(Input1.equals (s2))
{
tv.setChannel();
}
if(Input1.equals (s3))
{
tv.setVolume();
}
if(Input1.equals (s4))
{
tv.setPower();
}
else
{
System.out.println("");
System.out.println("Error - Input not recognised!");
System.out.println("");
}
!
it does work, but you didn't implement everything.
you programmed it so, that if Input1 contains the same value as s4, the error message is not displayed, otherwise it is.
just reread JavaAddict's post and put the else's where needed
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 49
Reputation: Laidler is an unknown quantity at this point 
Solved Threads: 1
Laidler Laidler is offline Offline
Light Poster

Re: Help with Scanner class - Chars and Strings

 
0
  #5
Nov 24th, 2008
ok thanks for that bit

however the inputs for C V and Q still dont work

also i have a problem with my setPower method which i hill need help with afterwards
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Help with Scanner class - Chars and Strings

 
0
  #6
Nov 24th, 2008
define or clarify:
Originally Posted by Laidler
...do not work
show your code now, using Code-tags, and explain what it does wrong
that might give us some more to go on
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 49
Reputation: Laidler is an unknown quantity at this point 
Solved Threads: 1
Laidler Laidler is offline Offline
Light Poster

Re: Help with Scanner class - Chars and Strings

 
0
  #7
Nov 24th, 2008
  1. import java.util.Scanner;
  2.  
  3. class TestTelevisionV3
  4. {
  5. public static void main(String [] args)
  6. {
  7. Television tv = new Television (4, 8, true);
  8.  
  9. tv.printImage();
  10.  
  11. System.out.println("");
  12. System.out.println("Press P to turn Power On or Off");
  13. System.out.println("Press C to Change the Channel");
  14. System.out.println("Press V to Change the Volume");
  15. System.out.println("Press Q to quit");
  16. System.out.println("");
  17.  
  18. Scanner myInput = new Scanner(System.in);
  19. String Input1 = myInput.next();
  20.  
  21. String s1="P";
  22. String s2="C";
  23. String s3="V";
  24. String s4="Q";
  25.  
  26. if(Input1.equals(s1))
  27. {
  28. tv.setPower();
  29. tv.printImage();
  30. }
  31. else if(Input1.equals(s2))
  32. {
  33. tv.setChannel();
  34. tv.printImage();
  35. }
  36. else if(Input1.equals(s3))
  37. {
  38. tv.setVolume();
  39. tv.printImage();
  40. }
  41. else if(Input1.equals(s4))
  42. {
  43. tv.setPower();
  44. tv.printImage();
  45. }
  46. else
  47. {
  48. System.out.println("");
  49. System.out.println("Error - Input not recognised!");
  50. System.out.println("");
  51. }
  52. }
  53. }

ok the program allows me to enter "P" as the input

However it does not allow me to enter "C" "V" or "Q" as the input

  1. [h8063984@linux15 Version3]$ java TestTelevisionV3
  2. __________________________________
  3. | ______________________________ |
  4. | |Channel: 4 | |
  5. | |Volume: 8 | |
  6. | | | |
  7. | | | |
  8. | | | |
  9. | | | |
  10. | | | |
  11. | |____________________________| |
  12. | * |
  13. |________________________________|
  14.  
  15. Press P to turn Power On or Off
  16. Press C to Change the Channel
  17. Press V to Change the Volume
  18. Press Q to quit
  19.  
  20. C
  21. C
  22. Exception in thread "main" java.util.InputMismatchException
  23. at java.util.Scanner.throwFor(Scanner.java:840)
  24. at java.util.Scanner.next(Scanner.java:1461)
  25. at java.util.Scanner.nextInt(Scanner.java:2091)
  26. at java.util.Scanner.nextInt(Scanner.java:2050)
  27. at Television.setChannel(television.java:28)
  28. at TestTelevisionV3.main(TestTelevisionV3.java:33)
  29. [h8063984@linux15 Version3]$

that is the output which it gives me in the terminal. Note that i had to enter "C" twice
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 49
Reputation: Laidler is an unknown quantity at this point 
Solved Threads: 1
Laidler Laidler is offline Offline
Light Poster

Re: Help with Scanner class - Chars and Strings

 
0
  #8
Nov 24th, 2008
it will take a while to reply to any more replies since i am about top get on a bus to go home
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Help with Scanner class - Chars and Strings

 
0
  #9
Nov 24th, 2008
I've tried to run your code ... it's working. Try to rebuild your project.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Help with Scanner class - Chars and Strings

 
0
  #10
Nov 24th, 2008
As the printStacktrace states:

...
at java.util.Scanner.nextInt(Scanner.java:2050)
at Television.setChannel(television.java:28)
...
There is an error at the method: setChannel of the Television class at line: 28. And the problem is with the way you use the nextInt method of the Scanner class. Better use the 'next' method and convert what you get to int.
It wasn't very difficult to figure that out, so post only the code of the Television class.

AND USE CODE TAGS. Click the button: '#' that is above where you post your reply
Last edited by javaAddict; Nov 24th, 2008 at 12:42 pm.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC