I am getting illegal start of expression [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
import java.util.Scanner;
//trying to print pyramid shape
class try
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of lines:");
int n=in.nextInt();
for(int i=n;i>0;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(" ");
}
j=n-(i-1);
for(int k=0;k<j;k++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
But iam not able to print solve.

try is a reserved keyword in Java. You cannot use it as a class name. Simply rename your class - i.e. to MyTry
Reference: Java Language Keywords

try is a key word you have to change your class name first. (Use MyClass)
How you can do this, You have to define and initialize the variable before use them.
for(int j=1;j<=i;j++){
System.out.print(" "); // j visible inside for loop only
}
j=n-(i-1); // access j from outside for-loop is not possible
You can make this mistake correct by your own. Don't you use an IDE for coding?

Here is the solution of your problem.
import java.util.Scanner;
//trying to print pyramid shape
class Try
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of lines:");
int n=in.nextInt();
int i,j;
for(i=n;i>0;i--)
{
for( j=1;j<=i;j++)
{
System.out.print(" ");
}
j=n-(i-1);
for(int k=0;k<j;k++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Related

Java error about turning sentence into camelCase [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I'm starting to code and trying to do a challenge of turning a sentence into camelCase. After some experimenting on my own, got to the following code:
package teste;
import java.util.Scanner;
public class Teste {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Insert the sentence to be turned into camelCase: ");
String entry = keyboard.nextLine();
System.out.print("Insert the character that is used as space: ");
String space = keyboard.nextLine();
char current;
char next;
String output = null;
for (int i=0; i<=entry.length(); i++){
current = entry.charAt(i);
next = entry.charAt(i+1);
if (i == entry.length()){
output += Character.toLowerCase(current);
} else if (entry.substring(i, i+1).equals(space)){
output += Character.toUpperCase(next);
i++;
} else {
output += Character.toLowerCase(current);
}
}
System.out.println("This sentence in camelCase is: " + output);
}
}
There is an error I can't seem to avoid with the last index of the input, even with the first if structure made especially for it, and I can't find out why. Could anyone explain to me what I did wrong?
you should avoid StringIndexOutOfBoundsException exception in line 15 "entry.charAt(i+1)",
This code will fix your error.
import java.util.Scanner;
public class Teste {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Insert the sentence to be turned into
camelCase: ");
String entry = keyboard.nextLine();
System.out.print("Insert the character that is used as
space: ");
String space = keyboard.nextLine();
char current;
char next;
String output = "";
for (int i=0; i<entry.length()-1; i++){
current = entry.charAt(i);
next = entry.charAt(i+1);
if (i == entry.length()-2){
output += Character.toLowerCase(current);
} else if (entry.substring(i, i+1).equals(space)){
output += Character.toUpperCase(next);
i++;
} else {
output += Character.toLowerCase(current);
}
}
//here we test the last character
int len=entry.length();
current = entry.charAt(len-1);
if(!entry.substring(len-1, len).equals(space)){
output += Character.toLowerCase(current);
}
System.out.println("This sentence in camelCase is: " +
output);
}
}

Getting an integer to go to a different line [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
This is my code for reversing the digits of an integer,
but the result gives each digit reversed but in a new line,
like if you give 341 to it , it gives you:
1
4
3
But I want to make it all come to the same line , is there a way to do it without changing the main code?
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.println(i);
}
System.out.println("Number of digits: "+l);
}
You could use print instead of println:
System.out.print(i);
Answer :
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.print(i);
}
System.out.println("Number of digits: "+l);
}
I think your code has few errors. It should be
int i = 0;
while(a>0) {
i=a%10;
a/=10;
System.out.print(i+" ");
i++;
}
In your code you are just picking digits and printing it in reversed order instead of really reversing it.
public static void main(String[] args)
{
int i,l=0,num=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0)
{
i=a%10;
a/=10;
num=(num*10)+i;
l++;
}
System.out.println("Number of digits: "+l);
System.out.println("Reversed Number: "+num);
}
Try this code instead.....

Creating a random output from an user input array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
public class decisionMaker {
public static void main(String args[]) {
String option[] = new String[10];
// Output
for (int i = 0; i <= 9; i++) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the next option:");
option[i] = input.next();
System.out.println(" ");
}
for (int i = 0; i <= 9; i++) {
System.out.println("option: ");
System.out.println("option[i]+" ");
}
// Output
}
I'm trying to figure out how to add a count to the options, exit and end the program after entering a certain letter or number, and how to create a random output from the user input. I want it to give me one option that I had input at random. Can anyone help me with one or a few of these things. I'm trying to learn to code on my own, and I'm stuck on these.
Randomness
You can generate random numbers using java.util.Random;:
import java.util.Random;
public class SomeClass{
static Random rand = new Random();
public static void main(String args[]){
System.out.println(rand.nextInt());
}
}
About some broken code:
If you want to print out the value of a variable with System.out.println() then you need only type the variable without any quotation marks. The code you've written below will not compile:
System.out.println("option: ");
System.out.println("option[i]+" ");
Assuming that's what you want to do, it should instead be written as:
System.out.println("option: ");
System.out.println(option[i]);
Or even System.out.println("option: \n"+option[i]);
(The escape sequence \n when placed inside of quotation marks just indicates to the console to add a new line.)
Scanner:
Additionally, as nick zoum pointed out, your Scanner object should be initialized outside of the for loop, such as right underneath of the main() method.
Please comment below if you need clarification or if I misunderstood what you were looking for. It was very hard to understand your question.
You could try something like this:
public class DecisionMaker {
public static void main(String[] args) {
// output
Scanner scanner = new Scanner(System.in);
int size = getInt(scanner);
String option[] = new String[size];
for (int index = 0; index < size; index++) {
System.out.print("Enter the next option:");
option[index] = scanner.next();
}
int index = (int) (Math.random() * size);
System.out.println(option[index]);
scanner.close();
// output
}
public static int getInt(Scanner scanner) {
int size = 0;
while (size <= 0) {
if (scanner.hasNext()) {
if (scanner.hasNextInt()) {
size = scanner.nextInt();
}
}
if (size <= 0) {
System.out.println("The input: " + scanner.next() + " is not a valid value.");
}
}
return size;
}
}
How the program works:
The Scanner is initialized in the beginning and there is only
one instance of it.
Then the program will wait until the user inserts a valid number for
the size of options.
The next 5 lines were essentially copied from your code.
Finally we get a random Integer in the range of 0 - (size - 1) and print
the String of the array with that index.

Java: What is wrong with this code? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to print out the sum of each individual number in a number but it is not giving the correct answer. What is wrong with my code?
import javax.swing.*;
public class Tallist {
public static void main(String[]args) {
int sum = 0;
String tal = JOptionPane.showInputDialog(null, "skriv ett tal");
for(int X = 0; X<=15; X++) {
sum += tal.charAt(X);
System.out.println(sum);
}
}
}
First of all, if you want to iterate through the entire string, you should not fix the number of iterations to 15 in your for-loop.
for(int X = 0; X<=15; X++)
Could be written as:
for(int x=0; x<tal.length(); x++) //user lower case for non-final variables
Next, you are summing up the ASCII value of the input string, not the numbers.
So if "123" was input into the InputDialog, you are summing up ASCII for 123: (49 + 50 + 51) instead of adding up (1+2+3).
Just convert it to integers before summing up:
sum += Character.getNumericValue(tal.charAt(x));
If I understood you correctly you want to calculate the crossfoot of a number.
Here the code you need with user input via input dialog:
public class Crossfoot
{
public static void main(String[] args)
{
while (true)
{
String userInput = JOptionPane.showInputDialog("Enter number:");
if (!isInteger(userInput))
{
continue;
}
int crossfoot = 0;
for (int i = 0; i < userInput.length(); i++)
{
crossfoot += Character.getNumericValue(userInput.charAt(i));
}
System.out.println(crossfoot);
}
}
public static boolean isInteger(String string)
{
try
{
Integer.parseInt(string);
return true;
} catch (NumberFormatException numberFormatException)
{
return false;
}
}
}
As another user already wrote, you should iterate over the whole string and be careful with the ASCII values of the characters.

Writing a simple loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Hey so I'm writing a program where, the user inputs exactly 10 words, and the computer outputs each word that has an A or a in it. I wrote the program with some help and now I need to now how how make it where the user can input 10 words back to back with a while loop. Here is what I have so far.
import java.util.Scanner;
public class ReadASH{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String name;
String S="";
System.out.println("Please enter any word.");
S = in.nextLine();
if(S.contains("a")||S.contains("A"))
System.out.println(S);
else System.out.println("The word you entered contains no a's or A's");
}
}
You need to actually have a loop, something like:
import java.util.Scanner;
public class ReadASH{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String name;
String S;
for (int i=0; i<10; i++) {
System.out.println("Please enter any word.");
S = in.nextLine();
if (S.contains("a")||S.contains("A"))
System.out.println(S);
else System.out.println("The word you entered contains no a's or A's");
}
}
}
Updated code based on comment:
import java.util.Scanner;
public class ReadASH{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String name;
int wordsToRead = 10;
String words[] = new String[wordsToRead];
for (int i=0; i< wordsToRead; i++) {
System.out.println("Please enter any word.");
words[i] = in.nextLine();
}
for (int i=0; i< wordsToRead; i++) {
if (words[i].contains("a")||words[i].contains("A"))
System.out.println(words[i]);
}
}
}

Categories