| | |
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:
Solved Threads: 1
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
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
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:
BUT
Also you will need to have all of your IFs as 'else if':
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:
Java Syntax (Toggle Plain Text)
if (Input1.equals("P")) { ..... }
Java Syntax (Toggle Plain Text)
String s1="a"; String s2="a"; //2 Different objects. (s1==s2) //false (s1.equals(s2)) //true
BUT
Java Syntax (Toggle Plain Text)
String s1 = "a"; String s2 = s1; //they are the same (s1==s2) //true (s1.equals(s2)) //true
Also you will need to have all of your IFs as 'else if':
Java Syntax (Toggle Plain Text)
if () { } else if () { } else if () { } .... { } else { System.out.println(""); System.out.println("Error - Input not recognised!"); System.out.println(""); }
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
•
•
Join Date: Nov 2008
Posts: 49
Reputation:
Solved Threads: 1
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!
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!
•
•
•
•
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("");
}
!
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
•
•
Join Date: Nov 2008
Posts: 49
Reputation:
Solved Threads: 1
Java Syntax (Toggle Plain Text)
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(); tv.printImage(); } else if(Input1.equals(s2)) { tv.setChannel(); tv.printImage(); } else if(Input1.equals(s3)) { tv.setVolume(); tv.printImage(); } else if(Input1.equals(s4)) { tv.setPower(); tv.printImage(); } else { System.out.println(""); System.out.println("Error - Input not recognised!"); System.out.println(""); } } }
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
Java Syntax (Toggle Plain Text)
[h8063984@linux15 Version3]$ java TestTelevisionV3 __________________________________ | ______________________________ | | |Channel: 4 | | | |Volume: 8 | | | | | | | | | | | | | | | | | | | | | | | |____________________________| | | * | |________________________________| Press P to turn Power On or Off Press C to Change the Channel Press V to Change the Volume Press Q to quit C C Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at Television.setChannel(television.java:28) at TestTelevisionV3.main(TestTelevisionV3.java:33) [h8063984@linux15 Version3]$
that is the output which it gives me in the terminal. Note that i had to enter "C" twice
As the printStacktrace states:
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
•
•
•
•
...
at java.util.Scanner.nextInt(Scanner.java:2050)
at Television.setChannel(television.java:28)
...
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
![]() |
Similar Threads
- Finding length (Java)
Other Threads in the Java Forum
- Previous Thread: Problem installing WebSphere6.1.0
- Next Thread: do while loop
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






