how to correctly use toUpperCase(); - java

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()){

Related

Entering character for switch statement selection

This question is regarding switch statement. These are a few similar posts on this (below) but I am still having trouble understanding.
Using user-inputted characters in If/Switch statements
How do I used a char as the case in a switch-case?
Multiple characters in a switch statement?
Please consider the following:
public class main
{
public int selection;
public main()
{
System.out.println("MENU");
Scanner in = new Scanner(System.in);
do {
showMenu();
selection = in.nextInt();
switch (selection)
{
case 1:
doSomething();
break;
case 2:
case 3:
default:
System.out.println("Instruction is invalid");
}
}
while (selection !=7);
{ System.exit(0); }
}
public static void showMenu()
{
System.out.print('\u000c');
System.out.println("option 1 \n");
System.out.println("option 2 \n");
System.out.println("7 - exit.\n");
System.out.println("Select Option:\n");
}
}
So this is a switch statement is for the user to choose options within the do while loop. The user enters an integer from the printed list to choose an option, after completion of the case, it loops back to menu.
My teacher informs me that its better practice to use char instead of int to get user input for the switch. I expect it to look something like this, but it doesn't work and I'm not sure why.
public class main
{
public int selection;
public main()
{
System.out.println("MENU");
Scanner in = new Scanner(System.in);
do {
showMenu();
String menu = "";
char selection = menu.charAt();
switch (selection)
{
case 'A':
doSomething();
break;
case 2, 3, 4, 5, 6
default:
System.out.println("Instruction is invalid");
}
}
while (selection != 'QQ');
{ System.exit(0); }
}
In the second link posted there was an answer which i think suggested using
hello.charAt(0)
as the switch condition?
switch (hello.charAt(0))
{
case 'a': ... break;
}
I have three specific questions on this code:
1) My code doesn't work. Should my condition be hello.charAt(0) ?
2) I would like to use QQ as the quit option on the switch. Is possible with the code above? From the second link, I think it should be fine.
3) It is also shown here (switch statement using char in the case condition?) that the case statement should have double quotations. Could someone please clarify this as well?
Something like this should work better:
do {
showMenu();
String menu = in.nextLine(); // read a line of input
char selection;
if (menu.length>0) selection = menu.charAt(0); // extract the first char of the line read
else selection = '\0'; // special char when input is empty...
switch (selection) {
case 'A': case 'a':
doSomething();
break;
case 'Q': case 'q':
break;
default:
System.out.println("Instruction is invalid");
}
} while (selection != 'Q' && selection != 'q');
menu stores the full input line. selection would be the first char (if it exists) of the line.
To use .charAt() you need to supply the index - so if you want to use the first character then use 0, etc.
In general one read an entire line instead of a single keystroke:
The first char of a String is gotten by charAt(0). However the string
could have length 0. There is the if-expression CONDITION ? TRUEVALUE : FALSEVALUE.
String menu = in.readLine();
char selection = menu.isEmpty() ? ' ' : menu.charAt(0);
switch (selection) {
case 'A':
doSomething();
break;
case '2':
case '3':
case '4':
...
break;
default:
System.out.println("Instruction is invalid");
}
There is an even more succint solution:
String menuSelection = in.readLine();
switch (menuSelection) {
case "A":
doSomething();
break;
case "2":
case "3":
case "4":
...
break;
default:
System.out.println("Instruction is invalid");
}

Double Switch Method gone wild?

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");
}
}
}

How to put characters beyond 9 in superscript in java?

So I am making a program in Java on a BlueJ environment that computes Binary expansion. However, I can't seem to figure out how to add powers more than 9 in the output.
If I have an input power of anything more than 9 the program goes haywire, presumably because there are no cases after 9.
Also, I personally feel my program in general is extremely inefficient but I just did it this morning and this was the first approach I saw, so if you see a way to make it more efficient than using switch case, that'd be great too.
This is my code so far. It's not all mine, but I'm not sure if intellectual property and stuff applies on here, so just putting it out there.
import java.util.*;
class Binomial_Theorem_Expansion
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the value of x in (x+a)^n");
int x=s.nextInt();
System.out.println("Enter the value of a in (x+a)^n");
int a=s.nextInt();
System.out.println("Enter the value of n in (x+a)^n");
int n=s.nextInt();
System.out.println ("The expanded answer is");
int r=0;
int powx=n;
while (r<=n)
{
long nCr=calculatenCr(n,r);
if(nCr!=-1)
{
double y=Math.pow((double)x,(double)n-r);
double z=Math.pow((double)a,(double)r);
switch (powx)
{
case 0: System.out.print ("("+nCr*y*z);
break;
case 1: System.out.print ("("+nCr*y*z+"x");
break;
case 2: System.out.print ("("+nCr*y*z+"x\u00B2");
break;
case 3: System.out.print ("("+nCr*y*z+"x\u00B3");
break;
case 4: System.out.print ("("+nCr*y*z+"x\u2074");
break;
case 5: System.out.print ("("+nCr*y*z+"x\u2075");
break;
case 6: System.out.print ("("+nCr*y*z+"x\u2076");
break;
case 7: System.out.print ("("+nCr*y*z+"x\u2077");
break;
case 8: System.out.print ("("+nCr*y*z+"x\u2078");
break;
case 9: System.out.print ("("+nCr*y*z+"x\u2079");
break;
case 10: System.out.print ("("+nCr*y*z+"x\u2071\u00B2");
break;
}
switch (r) {
case 0: System.out.print (")");
break;
case 1: System.out.print ("y"+")");
break;
case 2: System.out.print ("y\u00B2"+")");
break;
case 3: System.out.print ("y\u00B3"+")");
break;
case 4: System.out.print ("y\u2074"+")");
break;
case 5: System.out.print ("y\u2075"+")");
break;
case 6: System.out.print ("y\u2076"+")");
break;
case 7: System.out.print ("y\u2077"+")");
break;
case 8: System.out.print ("y\u2078"+")");
break;
case 9: System.out.print ("y\u2079"+")");
break;
}
r++;
if (r<=n)
{
System.out.print ("+");
}
powx--;
}
}
}
public static long calculatenCr(int n,int r)
{
long res=1;
if(n>=r)
{
res=getFact(n)/(getFact(n-r)*getFact(r));
return res;
}
else return -1;
}
public static long getFact(int n)
{
long f=1;
for(int i=n;i>=1;i--)
{
f*=i;
}
return f;
}
}
Thanks for any constructive input. :)
presumably because there are no cases after 9.
Your code is using UNICODE superscript characters, and the cases that you have cover only numbers zero through ten for x and zero through nine for y.
You can fix this by defining a method that produces a superscript UNICODE conversion of a multidigit number, and calling it from both places where you need to produce such representation:
switch (powx) {
case 0: System.out.print ("("+nCr*y*z);
break;
case 1: System.out.print ("("+nCr*y*z+"x");
break;
default: System.out.print ("("+nCr*y*z+"x"+toSuperscript(powx));
break;
}
The other switch (i.e. switch (r)) should be converted in a similar way.
You can implement String toSuperscript(int n) by producing a decimal representation of n, and then replacing '0' with '\u2070', '1' with '\u00B9', and so on.

Using throws java.io.IOException and getting System.in.read(); to access a case via an integer

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();
}
}

how to add while loop in switch statement when using dialog boxes?

Here I have some problem when I use while loop in switch statement using dialog boxes. Some statements are unreachable and dialog boxes not appeared. Please help me! And also can do some correction on my code.
This the simple code that I made:
public static void main(String[] args)
{
// prompt and read first number from user
String no = JOptionPane.showInputDialog(null, "Enter the number");
int num = Integer.parseInt(no); //convert string to number
switch (num)
{
//display result
default: JOptionPane.showMessageDialog(null,"fail"); break;
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
}
}
The cases in a switch/case are evaluated in the order you put them. default matches all cases. Since you have that first and that case does something before breaking out of it, the other cases will never be reached. Try this instead:
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); break;
Your code does not show a while loop anywhere. Perhaps you can update with the code you attempted.
switch (num)
{
case 1:
while(!your condition)
{
JOptionPane.showMessageDialog(null,"c=a+b");
}
break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); break;
}
Retype the code:
// prompt and read first number from user
String no = JOptionPane.showInputDialog(null, "Enter the number");
int num = Integer.parseInt(no); //convert string to number
while (num<=4)
{
if
switch (num)
{
//display result
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); continue;
}
}// end method main
}// end class abc

Categories

Resources