Code is pulling some error "Says no line found" - java

Project: What’s My Name?
From the keyboard enter your first and then your last name, each with its own
prompt. Store each in a separate String and then concatenate them together to
show your full name. Call both the project and the class FullName. When your
program is finished running, the output should appear similar to that below:
What is your first name? Cosmo
What is your last name? Kramer
Your full name is Cosmo Kramer
Here is what i got:
import java.io.*;
import java.util.*;
public class Tester
{
public static void main(String args[])
{
Scanner kbReader=new Scanner(System.in);
String a="Cosmo";
String b="Kramer";
a=kbReader.nextLine();
System.out.println("What is your first name?"+a);
b=kbReader.nextLine();
System.out.println("What is your last name?"+b);
System.out.println("Your full name is"+a+" "+b);
}}
What is wrong with my code? Both Eclipse and the web compiler doesn't work both do state a different error saying that I have no lines.
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Tester.main(Tester.java:10)

You appear to be reading before you display your message,
String a="Cosmo";
String b="Kramer";
a=kbReader.nextLine();
System.out.println("What is your first name?"+a);
b=kbReader.nextLine();
System.out.println("What is your last name?"+b);
System.out.println("Your full name is"+a+" "+b);
and I would recommend you use descriptive variable names. So, I think you wanted something like -
System.out.println("What is your first name?");
String firstName = kbReader.nextLine();
System.out.println("What is your last name?");
String lastName = kbReader.nextLine();
System.out.println("Your full name is " + firstName + " " + lastName);
Which seems to do what you asked when I run it (and provide your input) -
What is your first name?
Cosmo
What is your last name?
Kramer
Your full name is Cosmo Kramer
Finally, I note that FullName is not Tester.

Related

How should I re-prompt a user for a valid string?

I'm not the best programmer and am quite new to it. I've been trying for hours to get this program correct but I can't seem to come up with a way to make it work out the way I'd like to. Here's what I want to do:
Write a program that prompts a user for their name and then displays "Hello, [Name Here]!"
If the user does not enter anything but pressed Enter anyways, you should re-prompt for the user's name. This flow should look like the following:
Whats is your name?
Please Enter your name:
Please Enter your name: Programming Practice
Hello, Programming Practice!
Here's how I'm thinking of the program before I start writing anything in my IDE:
Ask the user for their name
Give them a chance to enter their name
If their entry is not in name format, give them output saying incorrect format
Give them a chance to enter in proper format
Repeat steps 4 and 5 as many times as it takes for them to enter proper format
Print "Hello, [Name Here]!"
END
Here's what I've got so far:
package lol;
import java.util.Scanner;
public class Whatever {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.printf("What is your name?\n");
String name = sc.nextLine();
if (name != "Programming Practice")
{
System.out.println("Please enter a valid name");
String name2 = sc.nextLine();
System.out.println("Hello, " + name2 );
}
else
{
System.out.println("Hello, " + name );
}
}
}
Right now the output I'm getting regardless of my entries are:
What is your name?
Please enter a valid name
Hello,
You can throw the sc.nextLine() into a while(true) loop and break out when you get a result you deem valid.
String name = "";
while(true) {
name = sc.nextLine();
if (name.equals("Programming Practice")) {
System.out.println("Hello, " + name);
break;
} else {
System.out.println("Please enter in the correct format");
continue;
}
}
A better way of approaching this problem would be by using do-while loop.
do {
// Your processing
} while (checkCondition());
The logic is based on the idea that the user will be prompted to enter details at least once.

How do I use an int and printf

I need to use the following in my program and not sure how to get it to work.
Ask the user to enter their name(String variable) and age (int
variable).
Also I need to display their name, age and a welcome message using a printf statement.
This is what I have so far. Can anyone help me? Please
package myfirstprogram;
import java.util.Scanner;
public class MyFirstProgram {
public static void main(String[] args) {
String name;
int age;
Scanner sc = new Scanner(System.in);
System.out.printf("Please Type Your Name then press the Enter key.");
System.out.printf("Please Type Your Age then press the Enter Key.");
name = sc.next();
age = sc.next();
System.out.printf("Hello. My name is " + name + ", I am pleased to meet you.");
System.out.printf("Your Age is " + age);
System.out.printf("Hello and Welcome, " + name);
}
}
Did you even try to compile your code to see what errors are in it?
Are you using a GUI program (like Eclipse, et al.) to compile your code? Or javac on the command-line? Either way your code compiles with the error:
javac myfirstprogram/MyFirstProgram.java
myfirstprogram/MyFirstProgram.java:17: incompatible types
found : java.lang.String
required: int
age = sc.next();
^
1 error
This is saying that, as shmosel and others rightly pointed out in the comments, Scanner.next() returns a string, which is fine when you are getting the user's name input that is of type String, but this won't work for age input as you have defined age as an int.
So to "get it to work" you need to do as the compiler instructs which is to change the line 17 to:
age = sc.nextInt();
Then your program should "work" as you expect.
Hope this helps!
I would try something like this:
public static void main(String[] args) {
String name;
int age;
Scanner sc = new Scanner(System.in);
System.out.printf("Please enter your name");
name = sc.next();
System.out.printf("Please enter your age");
age = sc.nextInt();
System.out.printf("Hello " + name + "\nYour age is: " + age);
sc.close();
}
The reason this code works is because, like others have said, the age variable is an integer, not a String (what sc.next() returns). Also, it is much easier to separate both questions because that way the program will easily distinguish what is being inputted as the age and the name.
If you want it to be a bit more fail proof, you can throw an exception that will check to see that the user did not input a String as an integer.
A good place to see how libraries work in Java is both Stackoverflow and the Java API website (depending on your version of Java there are different sites)
Java™ Platform, Standard Edition 7
API Specification
Java™ Platform, Standard Edition 8
API Specification
If you need help with anything else in Java and the above is complicated, try these websites:
Tutorials Point - Java
Best of luck!

Why is indexOf() not recognizing spaces?

For this program it asks for the user to input their full name. It then sorts out the first name and last name by separating them at the space the put between the first and last name. However, indexOf() is not recognizing the space and only returns -1. Why is that? Thanks.
Here is the prompt off of PracticeIt:
Write a method called processName that accepts a Scanner for the console as a parameter and that prompts the user to enter his or her full name, then prints the name in reverse order (i.e., last name, first name). You may assume that only a first and last name will be given. You should read the entire line of input at once with the Scanner and then break it apart as necessary. Here is a sample dialogue with the user:
Please enter your full name: Sammy Jankis
Your name in reverse order is Jankis, Sammy
import java.util.*;
public class Exercise15 {
public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);
processName(inputScanner);
}
public static void processName(Scanner inputScanner) {
System.out.print("Please enter your full name: ");
String fullName = inputScanner.next();
int space = fullName.indexOf(" "); // always return -1 for spaces
int length = fullName.length();
String lastName = fullName.substring(space+1,length+1);
String firstname = fullName.substring(0, space);
System.out.print("Your name in reverse order is " + lastName + ", " + firstname);
}
}
As next will return the next token use nextLine not next to get the whole line
see http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
When you do String fullName = inputScanner.next() you only read till the next whitespace so obviously there is no whitespace in fullName since it is only the first name.
If you want to read the whole line use String fullName = inputScanner.nextLine();

Input add text on line before input

The code below asks for your name, takes the input, and then displays your name.
Code:
public static void main(String args[]) {
System.out.println("What is your name?");
Scanner name = new Scanner(System.in); //Passed in "Vincent"
System.out.println("Hello, " + name.nextLine() + "!");
}
Output:
What is your name?
Vincent
Hello, Vincent!
Question:
How would I combine the first two lines, so that I can have something similar to:
"What is your name: namehere"
You are using println which adds the new line character at the end of your string.
Use this instead:
System.out.print("What is your name? ");
Read more here.
Just change println to print so the newline is not printed.
System.out.print("What is your name: ");

Generating reverse words

i am a beginner in java and i am doing practiceit questions off the internet.I tried attempting the question but i dont understand the error.
Write a method called processName that accepts a Scanner for the console as a parameter and that prompts the user to enter his or her full name, then prints the name in reverse order (i.e., last name, first name). You may assume that only a first and last name will be given. You should read the entire line of input at once with the Scanner and then break it apart as necessary. Here is a sample dialogue with the user:
Please enter your full name: Sammy Jankis
Your name in reverse order is Jankis, Sammy
public static void processName(Scanner console) {
System.out.print("Please enter your full name: ");
String full=console.nextLine();
String first=full.substring(0," ");
String second=full.substring(" ");
System.out.print("Your name in reverse order is: "+ second + "," + first);
}
Maybe i will go about explaining my code.So i try to break the two words apart.So i use substring to find the both words and then i hardcode to reverse them.I think the logic is right but i still get these errors.
Line 6
You are referring to an identifer (a name of a variable, class, method, etc.) that is not recognized. Perhaps you misspelled it, mis-capitalized it, or forgot to declare it?
cannot find symbol
symbol : method substring(int,java.lang.String)
location: class java.lang.String
String first=full.substring(0," ");
^
Line 7
You are referring to an identifer (a name of a variable, class, method, etc.) that is not recognized. Perhaps you misspelled it, mis-capitalized it, or forgot to declare it?
cannot find symbol
symbol : method substring(java.lang.String)
location: class java.lang.String
String second=full.substring(" ");
^
2 errors
33 warnings
public static void processName(Scanner console) {
System.out.print("Please enter your full name: ");
String[] name = console.nextLine().split("\\s");
System.out.print("Your name in reverse order is: "+ name[1] + "," + name[0]);
}
Of course it only works if the name has 2 words. For longer names you should write a method which would reverse an array
Take a look at the documentation for the substring() method. It does not take a string as its second parameter.
String first=full.substring(0," ");
String second=full.substring(" ");
What you may want instead is the indexOf() method. First find the index of the space character. Then find the substring up to that point.
int n = full.indexOf(" ");
String first=full.substring(o, n); //gives the first name
As per Java API, substring() accepts either one int argument like substring(int beginIndex) and two int arguments like substring(int startIndex, int endIndex) but you are calling with String arguments. So you're getting those errors. More info can be found here
String API.
go to here http://docs.oracle.com/javase/6/docs/api/java/lang/String.html and read to understand.
public class ex3_11_padString {
public static void main(String[] args) {
System.out.print("Please enter your full name: ");
String f_l_Name = console.nextLine();
String sss[] = f_l_Name.split(" ", 2);
System.out.print("Your name in reverse order is " + sss[1] + ", " + sss[0]);
}
}

Categories