I'm new to Java and I'm having a bit of trouble trying to call a variable (firstName) from another method to my main. Here is my code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter full name: ");
String fullName = reader.nextLine();
System.out.print("Enter graduation year: ");
int gradYear = reader.nextInt();
System.out.print("Greetings, " + firstName + ", your initials are ");
}
public static void getFirstName(String fullName) {
Scanner reader = new Scanner(System.in);
String firstName = fullName.substring(0,1).toUpperCase() + fullName.substring(1).toLowerCase();
}
}
First of note that after using nextInt() the following nextLine() won't work.
I guess, this is what you are trying to do.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter full name: ");
String fullName = reader.nextLine();
System.out.print("Enter graduation year: ");
int gradYear = reader.nextInt();
System.out.print("Greetings, " + getFirstName(fullName) + " your initial is, " + getInitial(fullName));
reader.close();
}
private static String getInitial(String fullName) {
return fullName.substring(0,1).toUpperCase();
}
public static String getFirstName(String fullName) {
return fullName.substring(1).toLowerCase();
}
You can return that value from a method and assign to a variable.
Let me show you.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter full name: ");
String fullName = reader.nextLine();
System.out.print("Enter graduation year: ");
int gradYear = reader.nextInt();
String firstName = getFirstName(fullName);
System.out.print("Greetings, " + firstName + ", your initials are ");
}
public static String getFirstName(String fullName) {
// Scanner reader = new Scanner(System.in);
String firstName = fullName.substring(1).toLowerCase(); // assuming first letter is the initials; best would be to substring by space or similar
return firstName;
}
Related
I am having a problem displaying the user input of subjects (subj) and their units (unit). It says null, which part do you think I have to fix/add? And if possible, can I add column on this so that subjects and units will align together? What do I have to do/import? This is my whole code (I am a beginner):
package com.mycompany.studentinfo;
import java.util.Scanner;
public class StudentInfo {
static String lname, fname, mname, program, subj;
static int studentno, unit;
public static void main(String[] args){
student();
academicinfo();
academicunit();
finaloutput();
}
static void student() {
try {
System.out.println("***PROVIDE THE FOLLOWING***");
Scanner sc = new Scanner(System.in);
System.out.print("Student No: ");
studentno = sc.nextInt();
String xd = sc.nextLine(); // extra
System.out.print("Last Name: ");
lname = sc.nextLine();
System.out.print("First Name: ");
fname = sc.nextLine();
System.out.print("Middle Name: ");
mname = sc.nextLine();
}
catch (Exception x){
System.out.println("***ERROR***");
student();
}
}
static void academicinfo() {
Scanner sc = new Scanner(System.in);
String[] subj = new String[8];
System.out.print("Program: ");
program = sc.nextLine();
System.out.println("Courses: ");
int i = 0;
while (i < subj.length) {
subj[i] = sc.nextLine();
i++;
}
}
static void academicunit() {
Scanner sc = new Scanner(System.in);
int[] unit = new int[8];
System.out.println("Units in corresponding order");
int i = 0;
while (i < unit.length) {
unit[i] = sc.nextInt();
i++;
}
}
static void finaloutput() {
System.out.println("PERSONAL INFORMATION");
System.out.println("\tStudent No. " + studentno);
System.out.println("\tLast Name: " + lname);
System.out.println("\tFirst Name: " + fname);
System.out.println("\tMiddle Name: " + mname);
System.out.println("\nACADEMIC INFORMATION");
System.out.println("Program: " + program);
System.out.println("Courses: ");
System.out.println(subj);
System.out.println(unit);
}
}
With this code, the final output will be:
PERSONAL INFORMATION
Student No. 123
Last Name: asd
First Name: asd
Middle Name: asd
ACADEMIC INFORMATION
Program: asd
Courses:
null
0
and the outcome I am trying to do is:
PERSONAL INFORMATION
Student No. 123
Last Name: asd
First Name: asd
Middle Name: asd
ACADEMIC INFORMATION
Program: asd
Courses: Units:
this is course 0
It is because in method academicInfo you are declaring and initialising a local variable String[] subj, while the class variable subj remains unchanged.
You ought to remove the declaration of subj in the foregoing method and declare and initialise subj as an array of Strings at class level.
public class StudentInfo {
static String lname, fname, mname, program;
static int studentno, unit;
static String[] subj = new String[8];
...
...
static void academicinfo() {
Scanner sc = new Scanner(System.in);
System.out.print("Program: ");
program = sc.nextLine();
System.out.println("Courses: ");
int i = 0;
while (i < subj.length) {
subj[i] = sc.nextLine();
i++;
}
}
...
Inside the method academicInfo you are creating the variable subj again, but as String[]. I believe you want to use the variable declared in line 4, in that case it would be like this:
static void academicinfo() {
Scanner sc = new Scanner(System.in);
System.out.print("Program: ");
program = sc.nextLine();
System.out.println("Courses: ");
subj = sc.nextLine();
}
Now, to align the texts, you must use the System.out.format, passing as a parameter the text and the division number, it would look like this:
String courses_text = "Courses:";
String units_text = "Units:";
System.out.format("%-5d"+courses_text,1);
System.out.format("%-5d"+units_text,2);
System.out.println();
System.out.format("%-5d"+subj, 1);
System.out.format("%-5d"+unit, 2);
See more about alignment here
package stringvars;
import java.util.Scanner;
public class ConcertID {
public static void main(String[] args) {
try (Scanner userInput = new Scanner (System.in)) {
String yourName;
System.out.print ("enter the last letter of your second name: ");
yourName = userInput.next();
String yourDOB;
System.out.print ("enter your Year Of Birth: ");
yourDOB = userInput.next();
String ConcertID;
ConcertID = yourName + " " + yourDOB;
System.out.println("your concert ID is " + ConcertID);
}
}
}
I'm trying to get the code to take the user input, add a number between 1 and 10 at the end and print it as Y18867. Currently it prints as Y 1886.
(And I've yet to figure out the math.random part.)
Let me recommend you start using the StringBuilder class to create concatenated strings. It has a better performance regarding time consuming to concatenate strings.
The following code generates the random number as well as the concertId string that you are trying to get.
public class ConcertID
{
public static void main(String[] args)
{
try (Scanner userInput = new Scanner(System.in))
{
String yourName;
System.out.print("Enter the last letter of your second name: ");
yourName = userInput.nextLine();
String yearOfBirth;
System.out.print("Enter your Year of Birth: ");
yearOfBirth = userInput.nextLine();
StringBuilder concertId = new StringBuilder();
concertId.append(yourName);
concertId.append(yearOfBirth);
concertId.append(generateNumber());
System.out.println(concertId.toString());
}
}
public static int generateNumber()
{
int number = 0;
Random random = new Random();
number = random.nextInt(1, 10);
return number;
}
}
How do I send a String variable from method 'name' to method 'main', and from there send it to method age?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = "";
name(scanner, name);
age(scanner, name);
}
static String name(Scanner scanner, String name) {
System.out.println("Pls, enter your name: ");
name = scanner.nextLine();
while(name.isEmpty()) {
System.out.println("Pls, enter your name: ");
name = scanner.nextLine();
}
return name;
}
static void age(Scanner scanner, String name){
System.out.println("Hello, " + name + ". How old are you?");
}
}
You don't need to pass name as parameter, but you rather want to return it from the method call.
Below works, but you also probably want to rename 'name' method to something else, like 'getName' to avoid confusion.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = name(scanner);
int age = age(scanner, name);
//do something with Age and name here
}
static String name(Scanner scanner) {
System.out.println("Pls, enter your name: ");
String name = scanner.nextLine();
while(name.isEmpty()) {
System.out.println("Pls, enter your name: ");
name = scanner.nextLine();
}
return name;
}
static int age(Scanner scanner, String name){
System.out.println("Hello, " + name + ". How old are you?");
//int age = ...
return age;
}
I can get and print the integer value in java but I am confuse how to get and print string. Can someone help
package hello;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int integer;
System.out.println("Please Enter Integer");
Scanner sc = new Scanner(System.in);
integer = sc.nextInt();
sc.close();
System.out.println("you entered : " +integer);
}
}
Program output
Please Enter Integer
5
you entered : 5
I am stuck in this program. I don't understand how to get string and print on screen
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextInt();
sc.close();
System.out.println("Your name"+name);
}
}
You need to change your type value name from int to String. And replace sc.nextInt() by sc.nextLine() or sc.next().
Example
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name " + name);
}
Use sc.nextLine() for reading string inputs
or
sc.next() (But this will read only a word before it encounters a space)
You can also use InputStreamReader for this purpose
eg.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in()));
String input = br.readLine();
name = sc.nextInt(); doesn't work for strings, only for integers, you should use sc.nextline instead.
And also you have to change int name to String name, due to other type of variable.
Your code should look like this:
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name"+name);
}
}
change int name to string name and use sc.nextLine()
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name"+name);
}
}
My current code is as follows, I need to print the user's input using the outputWithoutWhitespace() method but I am having a hard time understanding where to apply it/how to actually use it. I believe I should be using \t. How can I take away the whitespace when printing with the outputWithoutWhitespace() method?
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
int userCount = userInput.length();
return userCount;
}
}
package whitesp;
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
String omitSpace=userInput.replace(" ","");
int userCount = omitSpace.length();
return userCount;
}
}