Java User input related - java

I want to take input form user, i am sure my code is right but it don't work at all. Please help is there any thing that i am doing wrong?
`public void edit() throws IOException {
sll.insertAfter();
System.out.println("Enter text: ");
String sen;
sen = keyboard.next();
Object obj = sen;
sll.put(obj);
}
when i execute this an error appears at this line
sen = keyboard.next();

import java.util.*;
public class Example
{
public static void main(String[] args)
{
Edit();
}
public static void Edit()
{
Scanner scan = new Scanner(System.in);
String random;
System.out.print("Please input some text: ");
random = scan.nextLine();
System.out.println("You entered: " + random);
}
}
I don't know what your main method looks like so I can only assume it's empty That being said I can tell you why your current code doesn't work based off of the information you've given us.
Your edit method is not static, and in this situation assuming you've laid out your program simillar to this it must be static as it is in my example.
You've not setup a scanner, or maybe you did outside of your edit method but failed to make it static?
Scanner scan = new Scanner(System.in);
Why are you using Object, if you want to edit the string just use a for loop and substring.
Object
If you provide us with more information, your full code and the error you're getting we can better help you!

Related

Just some quick questions on using string

We have to make a program on printing initials, which seems pretty easy ok, but I don't know how to cut the string when the input is all on one line using the scanner class in.nextline();. I cant seem to find a way to cut the string using only string methods. Also, another problem arose when I have to also be able to adjust if there isn't a middle name either. if anyone can help me or lead me in the right direction that would be nice.
If you can use split function as you can see below:
String inputString=s.nextLine();
String [] str = inputString.split(" ");
If you want to have extract only first letter then -
import java.util.Scanner;
public class TestStringInput {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter the Name");
String input = scan.nextLine();
int value = input.indexOf(" ",input.indexOf(" "));
String result = input.substring(0,value);
System.out.println(result);
}
}
But if you want to Extract Starting 2 Initials then change this line-
int value = input.indexOf(" ",input.indexOf(" ")+1);
As Stephen mentioned, your question is about Java, consider re-tagging.
You can use a for loop to iterate through the string. Remember a string is an object. It would help a lot if you posted your code.
import java.util.Scanner;
public class HW03 {
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
String str = "";
System.out.println("What are your first, middle and last names?");
str = in.nextLine();
}
}

Can't get the scanner class to work in Java

I'm trying to get a program together that reads integers that a user inputs. I've been reading about the scanner class which seems to be the most common way to do this in java. However when I copy+paste the examples given on sites like this one I get some kind of error that I have no idea how to fix. Which is frustrating because all the stuff posted is supposed to be completed code that shouldn't have problems!
An example of some code that's supposed to work:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] arguments){
Scanner input = new Scanner(System.in);
String username;
double age;
String gender;
String marital_status;
int telephone_number;
// Allows a person to enter his/her name
Scanner one = new Scanner(System.in);
System.out.println("Enter Name:" );
username = one.next();
System.out.println("Name accepted " + username);
// Allows a person to enter his/her age
Scanner two = new Scanner(System.in);
System.out.println("Enter Age:" );
age = two.nextDouble();
System.out.println("Age accepted " + age);
// Allows a person to enter his/her gender
Scanner three = new Scanner(System.in);
System.out.println("Enter Gender:" );
gender = three.next();
System.out.println("Gender accepted " + gender);
// Allows a person to enter his/her marital status
Scanner four = new Scanner(System.in);
System.out.println("Enter Marital status:" );
marital_status = four.next();
System.out.println("Marital status accepted " + marital_status);
// Allows a person to enter his/her telephone number
Scanner five = new Scanner(System.in);
System.out.println("Enter Telephone number:" );
telephone_number = five.nextInt();
System.out.println("Telephone number accepted " + telephone_number);
}
}
Instead of the program running, it gives me two errors.
On the the line public class ScannerDemo { it gives me this error:
Illegal modifier for the local class ScannerDemo; only abstract or final is permitted
On the next line public static void main(String[] arguments){ I get this error:
The method main can not be declared static; static methods can only be declared in a static or top level type.
I have tried this with many different forms of scanners that are supposed to be all ready to go and get errors every time. What am I doing wrong? How can I fix it?
I am using Processing 3.
Please understand the difference between Java and Processing. Processing is its own language and editor with its own syntax rules, and you can't just copy-paste random Java code and expect it to work. You have to understand how Processing works, and then add code in a way that works in Processing.
Assuming you're using the Processing editor, then your main sketch file should not contain just a class, and it definitely shouldn't contain a main() method. It should contain a setup() and draw() function instead.
If you really want to use a class, then get rid of your main() method, encapsulate your logic in a function inside your class, and then add a setup() or draw() function that uses the class.
Or better yet, stop using a class and just use Scanner in your Processing code.
If you still can't get it working, please post a MCVE in a new question post, and we'll go from there. Good luck.
I believe as #Hovercraft has mentioned you just need this code in a file called ScannerDemo.java, Were guessing you have it in different file name.
Your class name and file name must be same. This is decision to first error.
public static void main(String[] arguments){
isn`t working because first error.

Java if (Yes or No Statement)

Hey there I got into some trouble with my java Code.
I try to code a bit around with java for a few hours and I dont know much thats why im asking. I learn best by trying but I get into so many problems.
So: I want the scanner to scan the next Statement and if its "ja" it should do the if thing etc.
The problem is, when i try to compile it it has an error with the = s.nextInt thing. In the console it says: "cannot find symbole". I tried so many things I dont know what to do. Allready tried so much.
import java.util.Scanner;
public class Brotcrunsher {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("A flag has more then 1 color right?");
String a = s.NextInt();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
} // end of if
else {
System.out.println ("You arentn a genie");
} // end of if-else
}
}
thanks in advance.
EDIT: Problem solved. Thank you for every awnser. I try my best to Tag my posts better and to format my code better
Here:
String a = s.NextInt();
You want a to be String (which makes sense, as you want to compare it against other Strings later on); so you better use:
String a = s.nextLine();
instead!
The other method a) does not exist and b) nextInt() ... returns a number, not a string
I can see two errors, firstly you are taking a string input from the command line user so your scanner must be "scanner.nextLine()" which takes a string, as it stands you are expecting an integer value.
Second your "s.scanner" is not calling anything, you have declared your scanner with the name "scan", so you need to change that to "scan".
import java.util.Scanner;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("A flag has more than one colour?");
String input = scan.nextLine();
if (input.equals("yes")) {
System.out.println("well done");
} else {
System.out.println("wrong answer");
}
}
Try:
import java.util.Scanner;
public class Brotcrunsher {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("A flag has more then 1 color right?");
String a = scan.nextLine();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
} // end of if
else {
System.out.println ("You arentn a genie");
} // end of if-else
}
}
You have got a compilation error that should be
String a = scan.next();
Since scan is your scanner object where you are using String a = s.NextInt(); which is not at all an object of scanner.
Two issues, one is a is a String not an int and the second is Scanner.nextLine() (or nextInt() or next()). And, the local reference is scan (not s). Like,
String a = scan.nextLine();
You can use like this.
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your rollno");
int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
sc.close();
}
}
refrence: http://www.javatpoint.com/Scanner-class

How to use Scanner.nextLine()

I use this code to input something in the String variable n, after I type what is asked, I don't want the program to print what I typed.
The code:
String n= Scanner.nextLine();
I feel is something you type after Scanner. , but I don't know what is it.
EDITED:
Thanks for answer, but I don't know how to Close the Scanner.
Here is the program...:
My program:
import java.util.Scanner;
public class A{
public static void main(String args[]){
String n;
Scanner bananas=new Scanner(System.in);
System.out.println("First Digit: ");
n=bananas.nextLine();
}
}
When I run the program, on the screen appears: "First Digit:",
then type, let's say I type "apple", the String n will be equals to
"apple", it's ok, that is what I want, what I don't like is that the
program prints on the screen the String n, I didn't want the program
to do that, this is what happens:
First Digit:
apples
What I want I the program to print is just:
"First Digit: ", without showing what I typed, just keep it.
Thanks.
First, you have to instantiate the Scanner, like so:
Scanner scanner = new Scanner(System.in); //if you want to read from the console
Then, you can receive your input.
String n = scanner.nextLine();
Make sure you close your scanner when you don't need it anymore:
scanner.close();
There is nothing tricky about it. You can do it like below and still your variable will hold your desired value.
import java.util.Scanner;
public class A{
public static void main(String args[]){
String n;
Scanner bananas=new Scanner(System.in);
System.out.println("First Digit: ");
n=bananas.nextLine(); // once you are done with input
bananas.close();
}
}
Even if you don't close scanner it won't make any difference as far as your code execution is concerned. Only a warning some where in your class will arise which doesn;t really affect your code. But still it is better to close the scanner once you are done with it. Its a good practice.

Scanner not working? [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 9 years ago.
Improve this question
I am new to Java and I am trying to make a Java app where it asks you to spell "Java" and if you spelled it correctly it will type "yes", however, it is typing "no", what am I doing wrong:
package quiz;
import java.util.Scanner;
public class quiz {
public static void main(String[] args) {
Scanner kirill = new Scanner(System.in);
System.out.println(kirill.next());
String kirill2 = "Java";
if (kirill.equals(kirill2)){
System.out.println("yes");
}else{
System.out.println("no");
}
System.out.println(kirill);
kirill.close();
}
}
Running code:
Java
Java
no
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=4][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]
if (kirill.equals(kirill2)){
kirill is the Scanner object, not the string. Try something like this:
Scanner kirill = new Scanner(System.in);
String userInput = kirill.next();
if (userInput.equals("Java")){
...
Also, note that your code will print "yes" if the user types "Java is a programming langauge." If you only want it to validate with just "Java," replace next with nextLine.
package quiz;
import java.util.Scanner;
public class quiz {
public static void main(String[] args) {
String kirill;
String kirill2 = "Java";
Scanner input = new Scanner(System.in);
kirill = input.next();
if (kirill.equals(kirill2)){
System.out.println("yes");
}else{
System.out.println("no");
}
System.out.println(kirill);
input.close();
}
}
Minor issue with your Scanner. You were trying to match a Scanner to a String. You can't to that silly!
Save what you're reading into a String instead of comparing the Scanner object with a String. Your main method should look something like
public static void main(String[] args) {
Scanner kirill = new Scanner(System.in);
String input = kirill.nextLine();
System.out.println(input);
String kirill2 = "Java";
if (input.equals(kirill2)){
System.out.println("yes");
}else{
System.out.println("no");
}
System.out.println(kirill);
kirill.close();
}
Also, note that .next() will only scan to the first delimiter (which is by default any whitespace), so if you want to make sure that the user only types "Java", then you should probably use .nextLine() instead of .next().
Let's take a quick look on your code, inside main():
Scanner kirill = new Scanner(System.in);
creates a scanner and assigns it to a variable, OK.
System.out.println(kirill.next());
Prints what the user types, but doesn't assign it to anything.
String kirill2 = "Java";
Just a String variable... OK.
if (input.equals(kirill2)){
If the scanner equals some text, then proceed. Hold on, you see what I just said? Comparing a Scanner and a String. This wouldn't end up right. Imagine a robot, and you give it a cup of water and a paper with "water" written on it, and ask if they are equal. Obviously they're not, and they can't be. You are comparing a set value with another set value, instead of the user's input. The following would be correct:
package quiz;
import java.util.Scanner;
public class quiz {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); //creates a scanner
String text = "Java"; //creates the text to be compared
String input = scan.nextLine(); //read some arbitrary text the user types
if (input.equals(text)){ //checks if user's input is equal to text
System.out.println("yes");
}else{
System.out.println("no");
}
scan.close(); //closes the Scanner
}
}
Although not required, it's a good practice to name variables after what they do or represent, or you will get confused very quickly...
So here, an easier method would be:
package quiz;
import java.util.Scanner;
public static void main(String[] args) {
String userInput;
String word = "Java":
Scanner in1 = new Scanner(System.in);
userInput = in1.next();
System.out.println( userInput );
if (word.equals(userInput)) {
System.out.println("Yes!");
}else{
System.out.println("No.");
}
System.out.println( userInput );
userInput.close();
}

Categories