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;
Related
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");
}
}
}
I just saw some of my work today, and i want to enter the user how many alphabet they want and continue converting. Can i have help? It would be fantastic. I tried anything. I'm just new in java programming that's why. Thanks :)
This is my whole programming
import java.util.Scanner;
public class Try2 {
public static void main(String[] args) {
int counter = 0;
int it = 0;
Scanner keyboard = new Scanner(System. in );
System.out.println("Enter words to digits: ");
String alpha = keyboard.nextLine();
alpha = alpha.toLowerCase();
String num = (" ");
while (counter < alpha.length()) {
switch (alpha.charAt(it)) {
case 'A':
case 'a':
case 'B':
case 'b':
case 'c':
case 'C':
num += "2";
counter++;
break;
case 'D':
case 'E':
case 'F':
case 'd':
case 'e':
case 'f':
num += "3";
counter++;
break;
case 'G':
case 'H':
case 'I':
case 'g':
case 'h':
case 'i':
num += "4";
counter++;
break;
case 'J':
case 'K':
case 'L':
case 'j':
case 'k':
case 'l':
num += "5";
counter++;
break;
case 'M':
case 'N':
case 'O':
case 'm':
case 'n':
case 'o':
num += "6";
counter++;
break;
case 'P':
case 'R':
case 'S':
case 'p':
case 'r':
case 's':
num += "7";
counter++;
break;
case 'T':
case 'U':
case 'V':
case 't':
case 'u':
case 'v':
num += "8";
counter++;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
case ' ':
num += "9";
counter++;
break;
}
if ((counter % 4) == 3) {
num += "-";
}
it++;
}
System.out.println(num);
}
}
You can wrap the read line code with another while loop:
Scanner keyboard = new Scanner(System.in);
while(true) {
System.out.println("Enter words to digits: ");
String alpha = keyboard.nextLine();
alpha = alpha.toLowerCase();
String num = " ";
int counter = 0;
int it = 0;
while (counter < alpha.length()) {
...
}
//new line (optional)
num += "\n";
}
You can check if the user entered a flag to end input:
//You can choose any flag you want
if(alpha.equals("finish")) {
break;
}
About your switch:
You can remove all the upper case letters because you wrote: alpha = alpha.toLowerCase();
You forgot 'q'
If alpha don't match: [a-zA-z ] (contains only letters and spaces) an IndexOutOfBoundsException will be thrown from alpha.charAt(it) because you don't increase counter and the loop don't exit in time.
To prevent the last problem you can remove counter and it and change the while loop to for loop:
for (int i = 0; i < alpha.length(); i++) {
if ((i % 4) == 3) {
num += "-";
}
switch (alpha.charAt(i)) {
...
}
}
import java.util.Scanner;
public class LetterGrade
{
public static void main(String[] args)
{
char grade;
String input;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter grade awarded");
input = keyboard.nextLine();
grade = input.charAt(0);
switch (grade)
{
case 'A':
System.out.println("Grade was between " + 89.5 - 100);
break;
case 'B':
System.out.println("Grade was between " + 79.5 - 89.45);
break;
case 'C':
System.out.println("Grade was between " + 69.5 - 79.49);
break;
case 'D':
System.out.println("Grade was between " + 59.5 - 69.49);
break;
case 'F':
System.out.println("Grade was below " + 59.5);
break;
System.out.println("Invalid grade inputted ");
}
}
}
4 errors found:
File: C:\Users\Raj\Downloads\Java\LetterGrade.java [line: 19]
Error: The operator - is undefined for the argument type(s) java.lang.String, int
File: C:\Users\Raj\Downloads\Java\LetterGrade.java [line: 22]
Error: The operator - is undefined for the argument type(s) java.lang.String, double
File: C:\Users\Raj\Downloads\Java\LetterGrade.java [line: 25]
Error: The operator - is undefined for the argument type(s) java.lang.String, double
I fixed the big errors i had, sorry about that. I really wasn't thinking straight and I understand i need to read more, which i will be doing tonight, but I'm have a different error now.
Problems:
1. grade = keyboard.next.Int(); should be grade = keyboard.nextInt();
2. Case A;
a. Case should be case
b. What is A you switch an int but there is no variable A that
holds any int value. Also if you mean the char A the you should
change it to 'A'
You need a : instead of ;after case. So something like case 'A':
3. switch (grade); remove the ;. Change it to switch (grade)
4. Not sure about this Any other character; Hopefully its meant to be a comment
First Issue:
grade = keyboard.next.Int();
change to
grade = keyboard.nextInt();
Second Issue:
your The switch Statement is clearly wrong
for example
switch (grade); <--- wrong
{
Case A; <--- wrong
your grade is type int but your case has type char
Case in wrong, you should have case
Look at this sample as your blue print
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
Source of Code and Read more about Switch Statement
There should not be a semi-colon at the end of line 13. (The "switch" line).
Once you have fixed that, you will then see that the syntax of the "case" clauses is wrong.
And "any other character" is nonsense from the Java perspective. (I'm surprised that the Java compiler didn't throw a house-brick at you!)
I'm not sure what's wrong with what I'm doing.
You are trying to write Java without bothering to learn it properly. That is wrong on many levels.
I recommend that you read the Oracle Java Tutorial. For this example, read the Switch Statement page.
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();
}
}
I have a double split up into an int array, however the problem I am facing, is that I need it to fill in from the back, like this:
int[] arrayA = new int[5];
int[] arrayB = new int[5];
x = 23456.08;
//code here
//what im left with:
arrayA [0,2,3,4,5,6];
arrayB [0,8];
Heres how im cutting up the double:
public static void main(System args[])
{
Scanner input = new Scanner(System.in);
String answer = "";
int count = 0;
int thatone = 0;
int[] splitD = new int[5]; //main num
int[] splitDec = new int[1]; //decimal
//Enter the Number
System.out.print("Enter a number to convert: ");
double num = input.nextDouble();
// convert number to String
String convert = num + "";
// split the number
String[] split = convert.split("\\.");
String firstPart = split[0];
char[] charArray1 = firstPart.toCharArray();
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
// the decimal part
if (split.length > 1)
{
String secondPart = split[1];
char[] charArray2 = secondPart.toCharArray();
splitDec = new int[charArray2.length];
for (int i = 0; i < charArray2.length; i++)
{
// convert char to int
splitDec[i] = Character.getNumericValue(charArray2[i]);
}
}
for(int i =0; i<count;i++)
{
if(i ==0) // x00000.00 or 000x00.00
{
if(splitD[0] != "0")
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else
{
answer+= "";
}
}
else if(i ==1)//this goes with i =2 //0x0000
{
if(splitD[i] == 1)
{
switch (splitD[i+1])
{
case 9: answer+="Nineteen"; break;
case 8: answer+="Eighteen"; break;
case 7: answer+="Seventeen"; break;
case 6: answer+="Sixteen"; break;
case 5: answer+="Fifteen"; break;
case 4: answer+="Fourteen"; break;
case 3: answer+="Thirteen"; break;
case 2: answer+="Twelve"; break;
case 1: answer+="ten"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
thatone = 1;
}
else
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirty"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
}
}
else if(i == 2) //00x000
{
if(thatone ==0)
{
switch (splitD[i])
{
case 9: answer+=" Nine"; break;
case 8: answer+=" Eight"; break;
case 7: answer+=" Seven"; break;
case 6: answer+=" Six"; break;
case 5: answer+=" Five"; break;
case 4: answer+=" Four"; break;
case 3: answer+=" Three"; break;
case 2: answer+=" Two"; break;
case 1: answer+=" One"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
}
else
{
}
}
else if(i ==3)
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else if(i ==4) //0000x0
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirdy"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
answer+= " ";
}
else if(i ==5) //00000x
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
}
}
if(splitDec[0] == 0)
{
answer += " and 00/100 Dollars";
}
else if(splitDec[1] == 0)
{
answer += " and " +splitDec[0] + "0/100 Dollars";
}
else
{
answer += " and " +splitDec[0] +splitDec[1] +" /100 Dollars";
}
System.out.println(answer);
}
}
What should I do to make the array add 0 in the appropriate places?
such as if I typed in 54.00 I would get:
int[] SplitD = {0,0,0,0,5,4};
Thanks!
Instead of re-initializing the array:
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
Use the existing (with length = 5), iterating from the back:
if(charArray1.length > 5) // heavy hard-code, would prefer something better
throw new IllegalArgumentException("Maximum 5 digits, on both decimal sides.");
// watch out of ArrayIndexOutOfBoundsException
for (int i = charArray1.length - 1, j = splitD.length -1 ;
i >=0 && j >=0; i--, j--)
{
// convert char to int
splitD[j] = Character.getNumericValue(charArray1[i]);
count++;
}
Recall, int arrays are filled with zeros, at initialization.
Think creating an array of String, one String for each character (including the decimal point), would be a better approach:
String[] chars = String.valueOf(num).split("(?<=.)");
Job done.
Edit:
To use a switch, don't even split:
for (byte b : String.valueOf(num).getBytes()) {
switch(b) {
case '.':
case '1':
}
}