Keep getting error: '.class' expected line 40 [duplicate] - java

This question already has an answer here:
What does "error: '.class' expected" mean and how do I fix it
(1 answer)
Closed 4 years ago.
import java.util.*;
public class Lab04B {
public static String toMeters (int unitNumber) {
String value;
switch (unitNumber) {
case 1:
value = "Meter";
break;
case 2:
value = "Nautical mile";
break;
case 3:
value = "Furlong";
break;
case 4:
value = "Mil";
break;
case 5:
value = "Rod";
break;
case 6:
value = "Vershok";
break;
case 7:
value = "Sheppey";
break;
case 8:
return 1.702;
default:
return -1;
}
{
public static double fromMeters (int unitNumber)
{
switch (unitNumber)
{
case 1:
return 1;
case 2:
return 1/1852.0;
case 3:
return 1/201.168;
case 4:
return 1/0.0254;
case 5:
return 1/5.029;
case 6:
return 1/0.04445;
case 7:
return 1/1408.0;
case 8:
return 1/1.702;
default:
return -1;
}
{
public static String getUnitName (int unitNumber)
{
String value;
switch (unitNumber)
{
case 1:
value = "Meter";
case 2:
value = "Nautical mile";
case 3:
value = "Furlong";
case 4:
value = "Mil";
case 5:
value = "Rod";
case 6:
value = "Vershok";
case 7:
value = "Sheppey";
case 8:
value = "Smoot";
default:
value = "faulty input";
}
{
public static void main (String[] args)
Scanner input = new Scanner (System.in);
System.out.println("Converting Measurements");
System.out.println("By: Ashleigh Pacewicz");
System.out.println("1.\tMeter");
System.out.println("2.\tNautical Mile");
System.out.println("3.\tFurlong");
System.out.println("4.\tMil");
System.out.println("5.\tRod");
System.out.println("6.\tVershok");
System.out.println("7.\tSheppey");
System.out.println("8.\tSmoot");
System.out.println("From what unit would you like to convert? ");
int = input.nextInt();
System.out.println("To what unit would you like to convert? ");
int = input.nextInt();
System.out.print("What measurement would you like to convert? ");
double = input.nextDouble();
}
}
}
I am just learning how to code. I'm trying to write a program to convert meters but I keep receiving error on line 40 and line 63 and line 96. Error:
'.class' expected.
What am I doing wrong?

First of all
int = input.nextInt();
System.out.println("To what unit would you like to convert? ");
int = input.nextInt();
You didn't give them a name
and look at your braces
}
{
public static String getUnitName (int unitNumber)
{
It's the same at every method
it should be like this
public void methodName() {
}
but you're doing this
{
public void methodName()
{
and you forgot breaks;
1 more thing you should really use an IDE

I want to be honest. I do not know why you are getting this error...
I've just copied your code and compiled. After removing 2 to 3 braces and adding one, your code compiled without errors. I'm sure you are getting the error, you pasted into your question, from somewhere else.
You have to apply some fixes:
System.out.println("To what unit would you like to convert? ");
int NAME_YOUR_VARS = input.nextInt();
And in a few places you are placing open braces infront of method headers:
{
public static double fromMeters(int unitNumber){
Or you forgot to close method bodys:
public static String toMeters (int unitNumber) {
switch(unitNumber) {
/* case statements were cut out here*/
}
//<- Here you forgot a brace!
Keeping track of code blocks and brace placing is very important!

Related

How can I make the program stop after entering a value from a user?

I am trying to write a program that receives the number of sides from the
user and determines the type of figure using switch structure and a while sentinel-controlled loop, but every time I get an infinite loop. How can that be fixed?
import java.util.Scanner;
public class P1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of sides:");
int s = input.nextInt();
while ( s!=-1)
{
switch (s)
{
case 1: System.out.println("Line");
break;
case 2:System.out.println("Angle");
break;
case 3:System.out.println("Triangle");
break;
case 4:System.out.println("Quadrilateral");
break;
case 5:System.out.println("Pentagon ");
break;
case 6:System.out.println("Hexagon");
break;
case 7:System.out.println("Heptagon");
break;
case 8:System.out.println("Octagon");
break;
case 9:System.out.println("Nonagon");
break;
case 10:System.out.println("Decagon");
break;
default: System.out.println("Enter a valid value:");
}
}
}
}
The while loop is written to continue as long as s!=-1; so you need to change s so that this expression is no longer true.

Converting a letter to a number using a switch statement within a method - Java

I am trying to convert a roman numeral entered by the user into the correct value it represents.
My code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter the a roman numberal (I, V, X, L, C, D, M): ");
String romanNumeral = in.nextLine();
romanNumeralToInt(romanNumeral);
System.out.println(romanNumeral);
}
public static int romanNumeralToInt(String romanNumeral) {
switch (romanNumeral) {
case "I":
romanNumeral = 1;
break;
case "V":
romanNumeral = 5;
break;
case "X":
romanNumeral = 10;
break;
case "L":
romanNumeral = 50;
break;
case "C":
romanNumeral = 100;
break;
case "D":
romanNumeral = 500;
break;
case "M":
romanNumeral = 1000;
break;
}
}
I have tried numerous different ways of converting this. I tried to use char instead of string but the same problem came up.
I have also tried to use the line:
public static int romanNumeralToInt(String romanNumeral, int romanDecimal) {
and then using:
case "I":
romanDecimal = 1;
break;
Which didn't work.
I have tried printing out just System.out.println(romanNumeral) which also didn't work.
I am kinda stuck and don't really understand how I go about returning a value as an integer when it was inputted as a string in a method
The current code you have pasted here does not seem right. No return written for the method, even though it clearly has an int return type. Was it compiled correctly?
Anyhow, change your method to have a return;
public static int romanNumeralToInt(String romanNumeral) {
int intNumeral = 0;
switch (romanNumeral) {
case "I":
intNumeral = 1;
break;
case "V":
intNumeral = 5;
break;
case "X":
intNumeral = 10;
break;
case "L":
intNumeral = 50;
break;
case "C":
intNumeral = 100;
break;
case "D":
intNumeral = 500;
break;
case "M":
intNumeral = 1000;
break;
}
return intNumeral;
}
Then in the main method let the methods return variable be stored in an int type variable;
int intNumericConverted = romanNumeralToInt(romanNumeral);
Welcome to stackoverflow!
My first thought is that you're not using your return value from your function, to do this, add a return statement instead of a break statement.
it should read
case "I":
return 1;
then return from it:
System.out.println(romanNumeralToInt("I"));
Or , simply make sure youre assigning strings instead of integers inside your function :
case "I":
romanNumeral = "1";
break;
but to accomplish this, you'll need to change the scope of romanNumeral to global if im not mistaken and this wouldnt be the best practice, in my opinion
You don't need to convert it you can simply return integer values like this:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter the a roman numberal (I, V, X, L, C, D, M): ");
String romanNumeral = in.nextLine();
romanNumeralToInt(romanNumeral);
System.out.println(romanNumeral);
}
public static int romanNumeralToInt(String romanNumeral) {
switch (romanNumeral) {
case "I":
return 1;
case "V":
return 5;
case "X":
return 10;
case "L":
return 50;
case "C":
return 100;
case "D":
return 500;
case "M":
return 1000;
}
}
Integer class has static method toString() - you can use it:
int i = 1234;
String str = Integer.toString(i);
Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.

error with switch cases...code not returning anything

The assignment for the project that I am working on is for the user to be able to input a zip code and for the program to provide the bar code in the output. For example, the bar code for 95014 right now should be "|:|:::|:|:||::::::||:|::|". This is all based on a table I have that denotes what each number should be (shown in the case statements). This is the code I wrote for that portion:
public class Zipcode{
private String zipcode = "";
private String barcode = "";
private int zipnum = 0;
public Zipcode(int zip_number){
zipnum = zip_number;
}
public void createBarcode(){
while (zipnum > 0){
switch (zipnum % 10)
{
case 0:
barcode = "||:::";
break;
case 1:
barcode = ":::||";
break;
case 2:
barcode = "::|:|";
break;
case 3:
barcode = "::||:";
break;
case 4:
barcode = ":|::|";
break;
case 5:
barcode = ":|:|:";
break;
case 6:
barcode = ":||::";
break;
case 7:
barcode = "|:::|";
break;
case 8:
barcode = "|::|:";
break;
case 9:
barcode = "|:|::";
break;
default:
break;
}
barcode += barcode;
zipnum = zipnum / 10;
}
}
public String getBarcode(){
return barcode;
}
}
However, when my tester class calls this class after the user inputs a zip code, nothing comes up as the result. Please help! Why isn't anything being returned? I don't know what I am doing wrong.
Notice that you re-assign barcode variable in each switch-case block and append itself again in the end.
You can do it with a temporary variable inside the method:
public void createBarcode(){
String tempBarcode = "";
...
case 0:
tempBarcode = "||:::";
break;
....
barcode += tempBarcode;
But I think the cleanest solution would be to use it as an utility method without any class variable dependencies:
public static String calculateBarcode(int zipNumber) {
StringBuilder barcode = new StringBuilder();
while (zipNumber > 0) {
switch (zipNumber % 10) {
case 0:
barcode.append("||:::");
break;
case 1:
barcode.append(":::||");
break;
...
...
default:
break;
}
zipNumber = zipNumber / 10;
}
return barcode.toString();
}
Simple testcase:
public static void main(String[] args) {
System.out.println(ZipcodeUtil.calculateBarcode(95014));
/* Output: :|::|:::||||::::|:|:|:|:: */
}
You aren't appending the zip value on every iteration correctly. The value is getting lost as you are overriding the value here on this step
...
case 0:
barcode = "||:::";
break;
...
Instead use a temporary variable to store the current iterations zip code value
public class Zipcode {
private String zipcode = "";
private String barcode = "";
private int zipnum = 0;
public Zipcode(int zip_number) {
zipnum = zip_number;
}
public void createBarcode() {
String temp_barcode = "";
while (zipnum > 0) {
System.out.println(zipnum % 10);
switch (zipnum % 10) {
case 0:
temp_barcode = "||:::";
System.out.println(0);
break;
case 1:
temp_barcode = ":::||";
System.out.println(1);
break;
case 2:
temp_barcode = "::|:|";
System.out.println(2);
break;
case 3:
temp_barcode = "::||:";
System.out.println(3);
break;
case 4:
temp_barcode = ":|::|";
System.out.println(4);
break;
case 5:
temp_barcode = ":|:|:";
System.out.println(5);
break;
case 6:
temp_barcode = ":||::";
System.out.println(6);
break;
case 7:
temp_barcode = "|:::|";
System.out.println(7);
break;
case 8:
temp_barcode = "|::|:";
System.out.println(8);
break;
case 9:
temp_barcode = "|:|::";
System.out.println(9);
break;
default:
break;
}
barcode += temp_barcode;
System.out.println(barcode);
zipnum = zipnum / 10;
}
}
public String getBarcode() {
return barcode;
}
public static void main(String args[]) {
Zipcode z = new Zipcode(95014);
z.createBarcode();
System.out.println(z.getBarcode()); // output: :|::|:::||||::::|:|:|:|::
}
}
Add barcode this way
case 0:
barcode += "||:::";
break;
case 1:
barcode += ":::||";
break;
case 2:
barcode += "::|:|";
break;
case 3:
.....
.....
.....
and remove this line
barcode += barcode;
This will solve your issue.
Your barcode variable overwrite in your code so add local variable in createBarcode method. try this:
public class Zipcode{
private String zipcode = "";
private String barcode = "";
private int zipnum = 0;
public static void main(String[] a){
Zipcode z = new Zipcode(1568);
System.out.println(z.zipnum);
z.createBarcode();
System.out.println(z.zipnum);
System.out.println(z.getBarcode());
}
public Zipcode(int zip_number){
zipnum = zip_number;
}
public void createBarcode(){
while (zipnum > 0){
String barcode;
switch (zipnum % 10)
{
case 0:
barcode = "||:::";
break;
case 1:
barcode = ":::||";
break;
case 2:
barcode = "::|:|";
break;
case 3:
barcode = "::||:";
break;
case 4:
barcode = ":|::|";
break;
case 5:
barcode = ":|:|:";
break;
case 6:
barcode = ":||::";
break;
case 7:
barcode = "|:::|";
break;
case 8:
barcode = "|::|:";
break;
case 9:
barcode = "|:|::";
break;
default:
barcode = "";
break;
}
this.barcode += barcode;
zipnum = zipnum / 10;
}
}
public String getBarcode(){
return barcode;
}
}
First create a Zipcode instance, Zipcode code = Zipcode(9)
Then call createBarcode() for that instance, so in my case code.createBarcode()
Then you can print out the getBarcode() method.
REMEMBER, getBarcode() only returns a String with the barcode but it does not print it out. You must use print to print it out.
EXAMPLE: System.out.println(code.getBarcode())

On Switch How to use logic operator on case JAVA

i have a problem i dont know what to put on case section, when ever the user input their grades from 0-100 there are output corresponds to their grades failed,good,verygood,excellent.
import java.util.Scanner;
public class ProgTestI {
public static void main (String args[]){
Scanner pao = new Scanner(System.in);
System.out.print("Grades: ");
String grades = pao.next();
int grado = Integer.parseInt(grades);
switch (grado){
case =<74: /* iwant to put 0 to 74*/
System.out.println("Failed");
case : /* 75-80*/
System.out.println("bellow average");
case : /*81-85*/
System.out.println("average");
case : /*86-90*/
System.out.println("Good");
case : /*91-96*/
System.out.println("VeryGood");
default:
}
}
}
You cannot use switch for ranges, you need to replace this chunk of code with proper if/else blocks.
Switch works only on numeric values, but it works like
if(numericVal == 40)
So writing it for ranges is... waste of code, and not readable.
You need to rewrite it:
if( g <= 74){
...
}else if( g > 74 && g <= 80 ){
...
Your case code is incorrect, you can do as Beri mentioned.
If you want to implement switch statement in your application, then you can do as follows:
public static void main(String[] args) {
Scanner pao = new Scanner(System.in);
System.out.print("Grades: ");
String grades = pao.next();
int grado = Integer.parseInt(grades);
int checkedCase=0;
if(grado<=74){
checkedCase=1;
}
else if(grado>=75&&grado<=80){
checkedCase=2;
}
else if(grado>=81&&grado<=85){
checkedCase=3;
}
else if(grado>=86&&grado<=90){
checkedCase=4;
}
else if(grado>=91&&grado<=96){
checkedCase=5;
}
switch (checkedCase){
case 1: /* iwant to put 0 to 74*/
System.out.println("Failed");
break;
case 2: /* 75-80*/
System.out.println("bellow average");
break;
case 3: /*81-85*/
System.out.println("average");
break;
case 4: /*86-90*/
System.out.println("Good");
break;
case 5: /*91-96*/
System.out.println("VeryGood");
break;
default: System.out.println("Please enter a value in range 0-96");
break;
}
}

ArrayList java program without using generics

I had been given an assignment to implement ArrayList and LinkedList without using generics. The problem is with the insertnode() method. Though I try to read from commandline using a scanner, the method returns without waiting.
import static java.lang.System.out;
import java.util.Scanner;
class Arraylist
{
public static final int LIST_SIZE=30;
static Scanner input = new Scanner(System.in);
static Object list[];
static int top = -1;
static int typeoflist;
public static void displaymenu()
{
int choice;
do{
out.print("\n Basic operations on a linked list:");
out.print("\n 1. Create list \n 2. Insert node \n 3. Delete node \n 4. Modify node \n 5. Search value \n 6. Print list\n Else. Exit \n Choice:");
choice = input.nextInt();
switch(choice)
{
case 1:
list = createlist();
break;
case 2:
insertnode();
break;
case 3:
//deletenode();
break;
case 4:
//modifynode();
break;
case 5:
//searchnode();
break;
case 6:
printlist();
break;
default:
return;
}
}while(true);
}
public static Object[] createlist()
{
int typeoflist;
out.println("Enter your choice of list datatype: \n 1. int \n 2. float \n 3. char \n 4. String \n 5. UserDefined \n Choice:");
typeoflist = input.nextInt();
switch(typeoflist)
{
case 1:
list = new Integer[LIST_SIZE];
break;
case 2:
list = new Float[LIST_SIZE];
break;
case 3:
list = new Character[LIST_SIZE];
break;
case 4:
list = new String[LIST_SIZE];
break;
}
return (Object[])list;
}
public static void insertnode()
{
Object o;
top++;
out.println("Enter the value to insert:");
switch(typeoflist)
{
case 1:
o = (Integer)input.nextInt();
list[top] = o;
break;
case 2:
o = (Float)input.nextFloat();
list[top] = o;
break;
case 3:
//o = (Character)input.next(); //
//list[top] = o;
break;
case 4:
o = (String)input.next();
list[top] = o;
break;
}
}
public static void printlist()
{
for(int i =0; i<top; i++)
{
out.println(list[i]);
}
}
public static void main(String[] args)
{
displaymenu();
}
}
Hint: typeoflist in createList() is hiding the static member variable.

Categories