Java if (Yes or No Statement) - java

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

Related

Take the whole line of a java chain and lenth

I am trying to make a java program that given a string, I return the length of it, but I do not know why it does not catch me all. I'm starting with the programing. I've got here.
import java.util.Scanner;
public class lengt {
public static void main (String [] args) {
Scanner in = new Scanner (System.in);
String cad = in.next();
System.out.println (cad);
System.out.println ("The length is:" + cad.length ());
}
}
There is a problem in your solution. in.next(); just take the first word of the chain, the one that is most immediate. If instead of using that you put it in.nextLine(); It takes you all the line you enter. You also have to be careful with length, which goes from 0 to n-1, when it comes to taking the length, since you could take one more and unnecessary. The solution for the problem that you pose would be, and there may be more, but one valid
import java.util.Scanner;
public class Example {
public static void main (String [] args) {
Scanner in= new Scanner (System.in);
String cad = in.nextLine();
System.out.println (cad);
System.out.println ("The length is:" + cad.length() - 1); //you can remove -1
}
}

Java User input related

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!

Jumping to lines in Java

I have just now started learning Java and one of the differences I noticed from C++ and VB is that Java has no goto statements, which I tend to use a lot while programming.
Is there any way I could jump between lines using another statement? I tried to use break and continue but to no avail (I might be doing something wrong).
Here is the code with goto statements and how I want it to operate:
public class HelloWorld {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
jump1:
System.out.print("What do you want to calculate? ");
String method = sc.nextLine();
if (method.equals("tax")) tax();
else {
System.out.print("Please input a valid method. \n\n");
goto jump1;
}
}
What is a good replacement for the goto commands?
A while loop in this instance.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html
For this (very specific) instance you could put
System.out.print("What do you want to calculate? ");
String method = sc.nextLine();
while (!method.equals("tax"))
{
System.out.print("Please input a valid method. \n\n");
method = sc.nextLine();
}
tax();
Obviously this only works if your only expected correct input is "tax", but it's a structure to build on.
You would want to put your if-statement in a while-loop by using the hasNextLine() function, which will loop until theres no more lines left to iterate over:
while(sc.hasNextLine()) {
// Your code..
}
For more info regarding the function check Scanner::hasNextLine documentation.
You should avoid "goto" statements in all languages, according to the rules of "structured programming", instead using if-then-else or do or while or for loops to control program flow.
Java DOES have a sort of "goto" statement that you COULD use to only slightly modify your code, but consider the while loop below and the break statement, which jumps out of the loop.
public static void main(String[] args) {
String method = "";
while(! method.equals("tax")){
System.out.print("What do you want to calculate? ");
method = sc.nextLine();
if(method.equals("tax"))
break;
System.out.print("Please input a valid method. \n\n");
}
tax();
}
The break statement enables your "Please ... valid" statement to display. You could also use this:
public static void main(String[] args) {
String method = "";
while(! method.equals("tax")){
System.out.print("What do you want to calculate? ");
method = sc.nextLine();
}
tax();
}
I also kind of like this:
public static void main(String[] args) {
String method = "";
while(1==1){
System.out.print("What do you want to calculate? ");
method = sc.nextLine();
if(method.equals("tax")
break;
System.out.print("Please input a valid method. \n\n");
}
tax();
}
You might go to the Java tutorials; they're good.
In this case you can use a do-while-loop, which will do the statement first and will only repeat if the statement is true:
public class HelloWorld {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
boolean right = true;
do {
if(right)
System.out.print("What do you want to calculate? ");
else
System.out.print("Please input a valid method. \n\n");
String method = sc.nextLine();
boolean right = false;
} while (!method.equals("tax"));
tax();
}
}

how to accept multiple lines of input just once and making sure the user is not asked for input once again

I have tried to solve the 3n+1 problem, and got very close. I think what happens here is the answer should accept multiple lines of input at once should not ask the user to give input again. I have tried the nextLine() method in a loop conditioned by the hasNextLong(), but the problem is whenever it does not find any more long types, it asks the user to give another input instead of breaking the loop. Is there any way to make sure it takes input only once, regardless of how many lines the user inputs?
The loop breaks if I enter a String. What I want to do is break when only the first input has no more long variables to deal with.
import java.util.Scanner;
import java.io.*;
public class te{
public static void main(String[] args){
Scanner key=new Scanner (new BufferedInputStream(System.in));
String s="";
while(key.hasNextInt()){
System.out.println("Entered loop");
s=s+""+key.nextLong();
}
System.out.println(s);
}
}
Not 100% sure what your trying to accomplish, but to answer this problem:
"..whenever it does not find any more long types, it asks the user to give another input instead of breaking the loop."
I just used a try/catch block. If the input is not a number, it breaks the loop. You can keep inputting numbers and hitting enter, and if an input is not a number, the loop will break; and it will print out the concatenated numbers.
import java.util.Scanner;
public class StackOverflow {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String s = "";
System.out.println("Enter Numbers: ");
while (true) {
try {
s += String.valueOf(scanner.nextInt()); // if input is not an int
} catch (Exception ex) { // it will throw exception
break;
}
}
System.out.println(s);
}
}
Edit: Scanning a line
Scanner input = Scanner(System.in);
System.out.printline("Enter some numbers: ");
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
while (lineScanner.hasNextLong()){
long num = lineScanner.nextLong();
// do something with num
}

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