here is the method class
import java.util.Scanner;
public class Methods extends Basic {
Scanner keyboard = new Scanner(System.in);
public void Nome(){
String name = keyboard .nextLine();
System.out.println("what is your name");
switch(name){
case "john":
if(name.length() < 5){
System.out.println("your name is average");
break;
}
case "jason":
if(name.length()>5){
System.out.println("your name is not average");
break;
}
}
}
public void Dates(){
System.out.println("Enter a number and we will tell you what month it is:");
int jay = keyboard.nextInt();
switch(jay){
case 1:
System.out.println("january");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
default:
System.out.println("that aint in the month dawg");
}
}
}
and here is the main class
public class Basic {
public static void main(String args[]) {
Methods dateo = new Methods();
Methods nomalo = new Methods();
nomalo.Nome();
dateo.Dates();
}
}
so yea, I am a complete noob. and I am really struggling when learning Java :( A lot of my friends who started learning java can now make their own 2 games and here I am struggling with a basic concept...
so what is the problem in this code?
Edit - Forget what I said here. Your code is working fine aparently. Just put your
println before you make the scan.
System.out.println("what is your name");
String name = keyboard.nextLine();
So problems is you are reading the data from keyboard before you printed 'what is your name'
The Methods class does not need to extend Basic. the name method does not need to have switch case.
So the Methods class would look like below,
public class Methods {
Scanner keyboard = new Scanner(System.in);
public void name(){
System.out.println("what is your name");
String name = keyboard.nextLine();
if(name.length() < 5){
System.out.println("your name is average");
} else {
System.out.println("your name is not average");
}
}
public void Dates(){
System.out.println("Enter a number and we will tell you what month it is:");
int jay = keyboard.nextInt();
switch(jay){
case 1:
System.out.println("january");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
default:
System.out.println("that aint in the month dawg");
}
}
}
Related
I am trying to validate switch case statement to make sure user do not enter any character or string types. How do i achieve that? I have been trying using while statement but i am unable to validate it.
public static boolean showMenu()
{
// Print welcome message
System.out.println("\nApplication Menu : ");
System.out.println("[1] Help");
System.out.println("[2] Add");
System.out.println("[3] Subtract");
System.out.println("[4] Multiply");
System.out.println("[5] Divide");
System.out.println("[6] Quit");
int SelectMenu = jin.nextInt();
switch(SelectMenu)
{
case 1:
showChoice();
break;
case 2:
add();
break;
case 3:
subtract();
break;
case 4:
multiply();
break;
case 5:
divide();
break;
case 6:
return false;
}
return true;
}
You can use a loop as follows :
String line = jin.nextLine();
while(!line.matches("\\d")){
System.out.println("Nope, please enter a digit");
line = jin.nextLine();
}
int selectMenu = Integer.parseInt(line);
Take a look at this simple example and adapt it to your code:
/**
*
* #author Momir Sarac
*/
public class SwitchExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("\nApplication Menu : ");
System.out.println("[1] Help");
System.out.println("[2] Add");
System.out.println("[3] Subtract");
System.out.println("[4] Multiply");
System.out.println("[5] Divide");
System.out.println("[6] Quit");
while (!scanner.hasNextInt()) scanner.next();
int number = scanner.nextInt();
switch (number) {
case 1:
clickedHelp();
break;
case 2:
clickedAdd();
break;
case 3:
clickedSubtract();
break;
case 4:
clickedMultiply();
break;
case 5:
clickedDivide();
break;
default:
clickedSomething();
}
}
public static void clickedHelp(){
System.out.println("Help!");
}
public static void clickedAdd(){
System.out.println("Add");
}
public static void clickedSubtract(){
System.out.println("Subtract");
}
public static void clickedMultiply(){
System.out.println("Multiply");
}
public static void clickedDivide(){
System.out.println("Divide");
}
public static void clickedSomething(){
System.out.println("Exit");
System.exit(0);
}
}
Hope you need to do something like this
public static boolean showMenu()
{
while(true) {
// Print welcome message
System.out.println("\nApplication Menu : ");
System.out.println("[1] Help");
System.out.println("[2] Add");
System.out.println("[3] Subtract");
System.out.println("[4] Multiply");
System.out.println("[5] Divide");
System.out.println("[6] Quit");
int SelectMenu = jin.nextInt();
switch(SelectMenu)
{
case 1:
showChoice();
break;
case 2:
add();
break;
case 3:
subtract();
break;
case 4:
multiply();
break;
case 5:
divide();
break;
case 6:
return false;
}
}
}
I have created a display menu and want to add a loop to continuously display the menu until the last option is selected. Not sure if I am doing it right.
Let me know if there is anywhere I can add on thanks!
import java.util.Scanner;
public class loopTest
{
public void displayMenu()
{
System.out.println("A. Option #A");
System.out.println("B. Option #B");
System.out.println("C. Option #C");
System.out.println("D. Option #D");
System.out.println("X. Exit");
System.out.println("Please enter your choice: ");
}
public void start()
{
Scanner console = new Scanner(System.in);
String s = "";
while(s < size())
{
displayMenu();
console.nextLine();
switch (s.charAt(0))
{
case 'A': System.out.println("A. Option #A"); break;
case 'B': System.out.println("B. Option #B"); break;
case 'C': System.out.println("C. Option #C"); break;
case 'D': System.out.println("D. Option #D"); break;
case 'X': System.out.println("X. Exit"); break;
default: System.out.println("Error, please enter a valid
character");
}
}
s++;
}
}
consider using a boolean variable
boolean wantToExit = false;
while (!wantToExit ) {
.... // switch
case 'X':
wantToExit = true;
System.out.println("X. Exit");
break;
}
note
s is a string, there is no < comparator nor s++ incrementor
Also, you are not assign a value to s from the Console input
I'm having some problems with some java code so I figured I'd ask it here. I am asked to add a line of code to take care of lower case input, but I'm not sure on how to implement it. I'm probably greatly over looking a very easy fix, but any help is greatly appreciated.
import java.util.Scanner;
public class Worksheet_7
{
public static void main(String[]args)
{
Scanner Fred = new Scanner(System.in);
System.out.println("Today we practice using switch statements \n\n");
char Ans;
System.out.println("What kind of car do you drive");
System.out.println("(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda");
String Word = Fred.next();
Ans=Word.charAt(0);
switch(Ans)
{
case 'C': System.out.println("Chevy-the heartbeat of America");
System.out.println("Buy a ford");
break;
case 'F': System.out.println("Ford tough");
System.out.println("Nice job cowboy");
break;
case 'B': System.out.println("Buick-Luxury you can afford");
System.out.println("Seriously? A Buick? That's not Luxury");
break;
case 'M': System.out.println("Mazda-Zoom, Zoom");
System.out.println("Can't really say anything");
break;
case 'H': System.out.println("Honda-Always dependable");
System.out.println("Yeah, yeah, yeah");
break;
default: System.out.println("Need a better car...?");
}
System.out.println("\n\n done for now..");
}
}
Fred.next() is a String
You need to call the String method of public String toUpperCase() somewhere.
Note: It returns a new String, and not capitalizes in place.
Or there is a Character method public static Character toUpperCase(char ch)
Or you can update the cases
case 'c':
case 'C':
...
break;
case 'f':
case 'F':
...
break;
Try this, user String instead of char:
public static void main(String[]args)
{
Scanner Fred = new Scanner(System.in);
System.out.println("Today we practice using switch statements \n\n");
String Ans;
System.out.println("What kind of car do you drive");
System.out.println("(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda");
String Word = Fred.next();
Ans=Word.substring(0, 1).toUpperCase();
switch(Ans)
{
case "C": System.out.println("Chevy-the heartbeat of America");
System.out.println("Buy a ford");
break;
case "F": System.out.println("Ford tough");
System.out.println("Nice job cowboy");
break;
case "B": System.out.println("Buick-Luxury you can afford");
System.out.println("Seriously? A Buick? That's not Luxury");
break;
case "M": System.out.println("Mazda-Zoom, Zoom");
System.out.println("Can't really say anything");
break;
case "H": System.out.println("Honda-Always dependable");
System.out.println("Yeah, yeah, yeah");
break;
default: System.out.println("Need a better car...?");
}
System.out.println("\n\n done for now..");
}
You could just change the switch to
switch (ans.toUpperCase()){
this is the code-
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
Scanner kb=new Scanner(System.in);
System.out.println("enter a four digit number");
int number=kb.nextInt();
int digit2=number;
switch (digit1)
{
case 1: System.out.println("One");break;
case 2: System.out.println("Two");break;
case 3: System.out.println("Three");break;
case 4: System.out.println("Four");break;
case 5: System.out.println("Five");break;
case 6: System.out.println("Six");break;
case 7: System.out.println("Seven");break;
case 8: System.out.println("Eight");break;
case 9: System.out.println("Nine");break;
case 0: System.out.println("Zero");break;
default: System.out.println("");break;
}
switch (digit2)
{
case 1: System.out.println("One");break;
case 2: System.out.println("Two");break;
case 3: System.out.println("Three");break;
case 4: System.out.println("Four");break;
case 5: System.out.println("Five");break;
case 6: System.out.println("Six");break;
case 7: System.out.println("Seven");break;
case 8: System.out.println("Eight");break;
case 9: System.out.println("Nine");break;
case 0: System.out.println("Zero");break;
default: System.out.println("");break;
}
}
}
And this error is coming
2 errors
sh-4.3$ javac HelloWorld.java
HelloWorld.java:10: error: cannot find symbol
switch (digit1)
^
symbol: variable digit1
location: class HelloWorld
HelloWorld.java:10: error: illegal start of type
switch (digit1)
^
2 errors
pls help me
You have not defined a variable named digit1.
Here's some rough code to do what you want:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter a four digit number");
String numStr = kb.next();
int number = 0;
try {
number = Integer.parseInt(numStr);
} catch (Exception e) {
System.out.println("Input was not a number!");
System.exit(1);
}
if (number < 1000) {
System.out.println("Number must be at least four digits!");
System.exit(1);
}
for (int digitNdx = 0; digitNdx < 4; digitNdx++) {
switch (numStr.charAt(digitNdx)) {
case '1':
System.out.println("One");
break;
case '2':
System.out.println("Two");
break;
case '3':
System.out.println("Three");
break;
case '4':
System.out.println("Four");
break;
case '5':
System.out.println("Five");
break;
case '6':
System.out.println("Six");
break;
case '7':
System.out.println("Seven");
break;
case '8':
System.out.println("Eight");
break;
case '9':
System.out.println("Nine");
break;
case '0':
System.out.println("Zero");
break;
default:
System.out.println("");
break;
}
}
}
}
Your code
Scanner kb = new Scanner(System.in);
System.out.println("enter a four digit number");
int number = kb.nextInt();
int digit2 = number;
switch (digit1) {
Error occur due to
here is no any variable assigned for digit1
Example
int digit1 = 5;
int digit2 = number;
I only need help with the input part. If the user inputs a number I need the program to read and output a case that equals the number that was input.
//This program will display the months of the year
public class MonthsOfTheYear {
public static void main(String[] args)
throws java.io.IOException{
int month;
System.out.println("Please enter a Month Number: ");
month = (int) System.in.read(); //Get an integer
switch (month) {
case 1: System.out.println("January");
break;
case 2: System.out.println("February");
break;
case 3: System.out.println("March");
break;
case 4: System.out.println("April");
break;
case 5: System.out.println("May");
break;
case 6: System.out.println("June");
break;
case 7: System.out.println("July");
break;
case 8: System.out.println("August");
break;
case 9: System.out.println("September");
break;
case 10: System.out.println("October");
break;
case 11: System.out.println("November");
break;
case 12: System.out.println("December");
break;
default: System.out.println("Invalid Month");
break;
}
System.out.println();
}
}
Try using the Console class instead: http://docs.oracle.com/javase/6/docs/api/java/io/Console.html to obtain user input, then convert the String to a number with Integer.parseInt(String)
Also, be aware that the next version of Java (8) will support Strings in case blocks.
class vehicle
{
int passengers;
int fuelcap;
int mpg;
}
import java.io.*;
class Vehicle_Demo
{
public static void main (String args[]) throws java.io.IOException
{
vehicle obj1 = new vehicle();
obj1.passengers=12;
obj1.fuelcap=9;
obj1.mpg=78;
System.out.println(obj1.passengers, obj1.fuelcap, obj1.mpg);
}
}
You can use Scanner to read your System.in
Scanner input = new Scanner(System.in);
month = input.nextInt();
Output:
Please enter a Month Number:
1
January
See related
Edit:
as noahz pointed out there is a Console class that covers the same functionality. For an idea of the difference between the two, read this.
Try to use TextIO Input Functions
This is better than scanner input
You need to compile TextIO.java fist as a prerequisite so that TextIO.class must be found in the same folder.
use this input function instead:
month = TextIO.getChar();
Please let me know if you have problems on this method.
Thanks!
Correct me if I'm wrong, but I'm pretty sure it will still work with "system.in.read". You're switch cases do not have single quotes around the input you want to be processed. If you want case one to be run when you type '1' your case should be case '1', not case 1.
There problem here was Java have cast a character to an integer, therefore you get an ASCII value.
For instance, when you type 5 you've got 53 in ASCII. Check it taking the variable value in System.out.println();
I've tried to solve that changing month to a char type and then passing a char value to the switch control. But the problem is, getting System.in.read(); method you just can get a single character. At last you just can select between 1 to 9.
I suspect the solution is using a buffer reader. Until then, I leave you here my code:
public class MonthsOfTheYear {
public static void main(String[] args)
throws java.io.IOException{
char month;
System.out.println("Please enter a Month Number: ");
month = (char) System.in.read(); //Get an integer
//System.out.println("Actual value of :" + month);
switch (month) {
case '1': System.out.println("January");
break;
case '2': System.out.println("February");
break;
case '3': System.out.println("March");
break;
case '4': System.out.println("April");
break;
case '5': System.out.println("May");
break;
case '6': System.out.println("June");
break;
case '7': System.out.println("July");
break;
case '8': System.out.println("August");
break;
case '9': System.out.println("September");
break;
/*case '10': System.out.println("October");
break;
case '11': System.out.println("November");
break;
case '12': System.out.println("December");
break;*/
default: System.out.println("Invalid Month");
break;
}
System.out.println();
}
}