System.out.print(" "); syntax error on token ".", # expected after this token - java

I'm getting this error on this piece of code and I can't figure out what's wrong.
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}

You need to put the code inside a method:
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}
}

The lines
Scanner sc = new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
should be in a code block such as a method rather than the class block

Try:
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main (String args[]) {
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}
}

Your lines:
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
Must be inside a method or a system initialiser block. Actually these are function calls which do not return anything. So cannot be in the class directly.
// In a method
public class enc {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
}
}
OR
// In a system initializer block
public class Test {
Scanner sc = new Scanner(System.in);
{
System.out.println("yoyo");
String s = sc.nextLine();
}
}
Actually those methods can be called directly in a class which actually return something. And must also be initialised to a variable of the respective type.

Related

"Scanner cannot be resolved to a type" how can i fix it?

I am taking an online course to learn java and currently we are learning "Getting User Input". this is what ive started with so far...
public class mama {
public static void main(String[] args) {
Scanner input;
}
}
however it keeps saying "Scanner input;"
what do i do?
import java.util.Scanner;
public class mama{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter input value ");
int a = sc.nextInt();
}
}
you can get input from the user just like this in your main function.
You have not assigned any value to the scanner variable, the first step to using a scanner is importing the library, then assigning the scanner a variable like "sc" or "keyboard" (which I use), then assign something the scanner variable.
Step by step breakdown:
You are missing:
import java.util.Scanner;
This is the first step, always remember
Then do this:
Scanner sc = new Scanner(System.in);
Lastly, assign the "sc" to something, either a string or Int variables.
For example:
int number = sc.nextInt();
or:
String x = sc.next();
Note: You need to understand what the scanner variables are for every type of variable you assign for example string or Int.
For String it is:
sc.next();
For Int:
sc.nextInt();
Same goes for double, float...etc, just change the Int to what you want
In the end, your code should look something like this:
import java.util.Scanner;
public class mama {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter desired input");
int input = sc.nextInt();
}
}

need a java program to detect if an input character is a vowel

Requirement:
Ask the user to type a single character from the alphabet.
Indicate then that this character is vowel or consonant, depending on the user's input.
If the user input is not a letter (between a and z or A and Z), or is a word with length> 1, type an error message.*
Problem:
can anyone help me to fix this code for the bold one , i have to use character not String.
public class Ush {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("put an character of alphabet : ");
char s = sc.next().charAt(0);
int s1 = s;
String s2 = Character.toString(s);
if(s2.length() > 1){
System.out.println(" Mistake !");
}
}
}```
There are lots of ways of doing it but you can simply check if there are more chars on your Scanner, ex:
if (sc.hasNext()) {
System.out.println(" Mistake !");
}
This way, if you scanner has another input, you will get the Mistake printed.
public class Ush {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("put an character of alphabet : ");
char s = sc.next().charAt(0);
int s1 = s;
String s2 = sc.next();
if(s2.length() > 1){
System.out.println(" Mistake !");
}
}
}
But you can optimize your program as below,
public class Ush {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("put an character of alphabet : ");
String s2 = sc.next();
char s = s2.charAt(0);
int s1 = s;
if(s2.length() > 1){
System.out.println(" Mistake !");
}
}
}

printout the length and words of the input

Sorry for my unclear questions, I want to input a single line text and print out the length of it, then printout the first word of it then the rest of the text.
Like if input "I am ruby", then the output would be:
9
I
am ruby
How should I make it? I have searched for the questions and got some similar but doesn't help, The following code is what I make so far
import java.util.Scanner;
public class lab {
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
String s = kbd.next();
String s1 = kbd.nextLine();
System.out.println(s);
System.out.println(s1);
}
}
import java.util.Scanner;
public class lab {
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
String s = kbd.next();
String s1 = kbd.nextLine();
System.out.println(s.length());
System.out.println(s);
System.out.println(s1);
}
}
Scanner does not have lenght(), Length() method is only for strings variables
Which IDE are you using (Eclipse, Netbeans, Intellij?) because the IDE usually shows you the error
I don't get what you want.
import java.util.Scanner;
public class lab
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
System.out.print("1st string: ");
String s = kbd.nextLine();
System.out.print("2nd string: ");
String s1 = kbd.nextLine();
System.out.println(s.length()); // this is for the length
System.out.println(s1); // this if for the input only
}
}
Still the same.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
System.out.print("Word:");
String s = kbd.nextLine();
System.out.println("Input:" +s); // print the string
System.out.println("Length:" + s.length()); // the length of the string
}
}
see the picture if its what u want to do.

ReplaceAll with string builder with user input

I have a question regarding StringBuilder. I'm trying to write a program that takes the user input : for example "DOG DOG CAT DOG DOGCAT", then asks the user to input a word they would like to change and what they would like to change it to. It should then replace all occurrences and print the result.
I have a code:
public class ChangeSentence
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Write text: ");
String text = sc.nextLine();
StringBuilder x = new StringBuilder(text);
System.out.println("Write which word would you like to change: ");
String rep = sc.nextLine();
System.out.println("For what do you want to change it: ");
String change = sc.nextLine();
System.out.println(Pattern.compile(x.toString()).matcher(rep).replaceAll(change));
}
}
How should I change it to achieve the result?
Thanks!
**Forgot to mention, I need to use the StringBuilder (without it i know how to write it).
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Write text: ");
original = sc.nextLine();
//StringBuilder x = new StringBuilder(text);
System.out.println("Write which word would you like to change: ");
String replacableWord = sc.nextLine();
System.out.println("For what do you want to change it: ");
String newWord = sc.nextLine();
String output = original.replace(replacableWord ,newWord);
System.out.println(output);
}
You just use the function replace on the original String and the
first parameter is the target String
the
second parameter is the replacement String
Last line should be replaced by following:
System.out.println(text.replaceAll(rep, change));
It's simple. You have to excercise a little

How do I count size of every word in this string?

I want to be able to output the letter size of each word. So far my code only outputs the letter size of the first word. How do I get it to output the rest of the words?
import java.util.*;
public final class CountLetters {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String words = sc.next();
String[] letters = words.split(" ");
for (String str1 : letters) {
System.out.println(str1.length() );
}
}
}
It's just because next returns only the first word (or also called the first 'token'):
String words = sc.next();
To read the entire line, use nextLine:
String words = sc.nextLine();
What you are doing should work then.
The other thing you can do is go ahead and use next all the way (instead of a split) because Scanner already searches for tokens using whitespace by default:
while(sc.hasNext()) {
System.out.println(sc.next().length());
}
Using sc.next() will only let the scanner take in the first word.
String words = sc.nextLine();
Iterate over all of the scanner values:
public final class CountLetters {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String word = sc.next();
System.out.println(word.length() );
}
}
}

Categories

Resources