I'm new to Java, got stuck with the following:
Create a program that will output to console your names and where you are from. Make your program ask the user for this information.
Advanced Assignment: Change the program so that it runs in a continuous loop, asking the user for name and where they are from, and
outputting to the console. If the user types in "X" for either the
name or where they are from, terminate the loop.
I implemented the first part, but having trouble with advanced assignment. Can anyone please give me a hint, thank you!
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner myScan = new Scanner(System.in);
System.out.println("Enter your name");
String myName = myScan.next();
System.out.println("Enter your location");
String myLocation = myScan.next();
System.out.println("Your name is "+ myName + " and your location is " + myLocation);
}
}
You would nest the contents of your main method in a continuous loop and then check the inputs for "X", If either is "X" then you would "break" out of the loop.
public static void main(String[] args) {
Scanner myScan = new Scanner(System.in);
while(true) {
System.out.println("Enter your name");
String myName = myScan.next();
System.out.println("Enter your location");
String myLocation = myScan.next();
if(myName.equals("X") || myLocation.equals("X")){
break;
}
System.out.println("Your name is " + myName + " and your location is " + myLocation);
}
}
Try this:
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner myScan = new Scanner(System.in);
while(true){
System.out.println("Enter your name");
String myName = myScan.next();
if(myName.equals("X"))
break;
System.out.println("Enter your location");
String myLocation = myScan.next();
if(myLocation.equals("X"))
break;
System.out.println("Your name is "+ myName + " and your location is " + myLocation);
}
}
}
Related
I am supposed to make a program that asks users for a list of input. Then, from that list, my program is supposed to pick out the third answer and then print it out. It sounds really simple, but how do I assign numbers to each of the user inputs? Do I even do that? I am a beginner, and thank you so much for your help!
This is the code I have so far:
import java.util.*;
public class MyProgram
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
while(true) {
System.out.println("What do you appreciate in your life or school?");
String ans = scan.nextLine();
if(ans.equals(""))
{
break;
}
}
System.out.println("You said \"" + input3 + "\" as your third answer.");
}
}
You can strore the third input in a variable you initialized bevor the loop.
if there is no third input it prints: ""
import java.util.*;
public class MyProgram
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int index = 0;
String input3 = "";
while(true) {
System.out.println("What do you appreciate in your life or school?");
String ans = scan.nextLine();
if (index == 2) {
input3 = ans;
}
if(ans.equals("")){
break;
}
index++;
}
System.out.println("You said \"" + input3 + "\" as your third answer.");
}
}
This is the Question: Create an array of Strings and assign 5 names to it. Ask the user what their name is, if their name is the same as one that is already in the list do something. Get creative!, use a for-each loop to print every name in the array with a space in-between each indices.
This is what I have so far. One of the issues I am having is that the scanner is only comparing the input to the first name on the array and not the rest.
public static void main(String[] args) {
String[] names = {"Jose", "Alex", "Steven", "Sky", "Ana"};
Scanner scan = new Scanner(System.in);
System.out.println("What is your name? ");
String input = scan.next();
for (String n:names) {
if (n.equalsIgnoreCase(input)) {
System.out.print("Hooray! Your odds of finding a keychain with your name on it are high! =) ");
}
else {
System.out.print("Welcome to the rare names club!!! =D " );
System.out.print(names + " ");
}
break;
}
}
}
Feel free to comment on any other issues you see. I am new at this and I'd appreciate an feedback. Thx
Maybe this will be helpful. I think the "break" is called prematurely. There are lots of ways you can solve this, but I used a boolean to determine if the name was found. Then I used the boolean after the loop to determine what to print.
public static void main(String[] args) {
String[] names = {"Jose", "Alex", "Steven", "Sky", "Ana"};
Scanner scan = new Scanner(System.in);
System.out.println("What is your name? ");
String input = scan.next();
boolean isFound = false;
for (String n:names) {
if (n.equalsIgnoreCase(input)) {
isFound = true;
break;
}
}
if (isFound) {
System.out.print("Hooray! Your odds of finding a keychain with your name on it are high! =) ");
} else {
System.out.print("Welcome to the rare names club!!! =D " );
System.out.print(names + " ");
}
}
Same method but slightly different :)
`import java.util.Scanner;
import java.util.ArrayList;
public class New {
public static void main (String[]args) {
String [] cities = {"Poznan", "Warsaw", "Gdansk", "Wroclaw", "Krakow", "Lodz", "Katowice"};
Scanner scan = new Scanner(System.in);
System.out.println("What is the name of your city in Poland? ");
String name = scan.nextLine();
for (String n:cities) {
if (n.equalsIgnoreCase(name)) {
System.out.println("You are citizen of Poland from " +name);
System.out.println("Thank your for visiting " +name);
}
else {
System.out.println("You are not from Poland!!!" );
System.out.println("There is not city in Poland called" +name);
}
break;
}
}
}`
import java.util.Scanner;
public class Testing {
public static void main(String [] args) {
Scanner user_input = new Scanner( System.in );
String start;
System.out.print("(Don't put capital letters) ");
String color;
System.out.print("Enter Your Favorite Color: ");
color = user_input.next ( );
String animal;
System.out.print("Enter Your Favorite Animal: ");
animal = user_input.next ( );
String preference;
preference = color + " and that your favorite animal is a " + animal;
System.out.println("From the information I gathered, I figured out that you like the color " + preference);
class A {
public void Main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Do you want to hear a joke?");
String a = user_input.next ( );
if (a.equalsIgnoreCase("yes")) ;
System.out.println("What did the fish say when he ran into a wall? Dam");
if(a.equalsIgnoreCase("no")) ;
System.out.println("Oh..."); }
}
}
}
and this is the error
Testing.java:29: error: local variable user_input is accessed from within inner class; needs to be declared final
String a = user_input.next ( );
^
1 error
I've just been messing around with the code and the errors go up and up so i decided to ask someone more experienced than myself. I have been using java for only a day and this is my first program that i've created, i've searched for at least 2 hours for a solution but couldn't find one, so i decided to ask. I got it to compile before, but it was without "user_input.next" so it acted like the whole line of code below "class A" was invisible and it wouldn't let me type anything, making it useless. I than put in user_input so that i could input something, but TONS of errors came up, but this is the one error that keeps popping up.
The error is most likely caused by you declaring a scanner (input) and then using the scanner from the other class (user_input)
Try this code
import java.util.Scanner;
public class Testing
{
public void input1()
{
Scanner user_input = new Scanner( System.in );
System.out.print("(Don't put capital letters) ");
String color;
System.out.print("Enter Your Favorite Color: ");
color = user_input.next();
String animal;
System.out.print("Enter Your Favorite Animal: ");
animal = user_input.next();
String preference;
preference = color + " and that your favorite animal is a " + animal;
System.out.println("From the information I gathered, I figured out that you like the color " + preference);
}
public void input2()
{
Scanner input = new Scanner(System.in);
System.out.println("Do you want to hear a joke?");
String a = input.next();
if(a.equalsIgnoreCase("yes"))
{
System.out.println("What did the fish say when he ran into a wall? Dam");
}
else if(a.equalsIgnoreCase("no"))
{
System.out.println("Oh...");
}
}
public static void main(String[] args)
{
Testing tS = new Testing();
tS.input1();
tS.input2();
}
}
In this code there are two methods; input1 and input2. These methods contain the two conversations you tried to have with your command console. Then in the main method the class is initialized and it runs the two methods.
I hope you want to do this
public class Test2 {
private static Scanner user_input = new Scanner(System.in);
private static boolean wantsToContinue(){
System.out.println("Do you want to hear a joke?");
String a = user_input.next();
if(a.equalsIgnoreCase("yes"))
return true;
else
return false;
}
public static void main(String[] args)
{
System.out.print("(Don't put capital letters) ");
System.out.print("Enter Your Favorite Color: ");
String color = user_input.next();
System.out.print("Enter Your Favorite Animal: ");
String animal = user_input.next();
String preference = color + " and that your favorite animal is a " + animal;
System.out.println("From the information I gathered, I figured out that you like the color " + preference);
if(!wantsToContinue()){
//If not want to continue just return
System.out.println("Oh...");
return;
}
//Continue with your next joke here
System.out.println("What did the fish say when he ran into a wall? Dam");
}
}
Why when I run my program and enter 5, it allows me to enter my records, but when the main menu runs again and I enter 6, the changePhoneNumber method is not run and it goes back to the main menu. Is the while(true) loop somehow messing things up?
I have a class called Record that looks like:
public static void main(String[] args) {
BankMethods method = new BankMethods();
Scanner input = new Scanner(System.in);
int optionSelected = 0;
while(true){
System.out.println("5. Add a New Record");
System.out.println("6. Change the Phone Number in the Current Record");
optionSelected = input.nextInt();
if (optionSelected == 5){
Scanner getRecord = new Scanner(System.in);
System.out.println("Enter First Name: ");
String firstName = getRecord.nextLine();
System.out.println("Enter Last Name: ");
String lastName = getRecord.nextLine();
System.out.println("Enter Phone Number: ");
String phoneNumber = getRecord.nextLine();
method.addNewRecord(firstName, lastName, phoneNumber);
}
if (optionSelected == 6){
System.out.println("What would you like to change your phone "
+ "number to? ");
String newNumber = input.nextLine();
method.changePhoneNumber(newNumber);
}
and the other class...BankMethods:
public class BankMethods {
LinkedList recordInformation = new LinkedList();
Bankdata mainMenu = new Bankdata();
public void addNewRecord(String firstName, String lastName,
String phoneNumber){
recordInformation.add(firstName); recordInformation.add(lastName);
recordInformation.add(phoneNumber);
}
public void changePhoneNumber(String newNumber){
recordInformation.set(2, newNumber);
System.out.println(recordInformation);
}
The problem is that you are using 2 Scanners to read the one InputStream. When you open the second Scanner you will not be able to read using the original one as the second will have exclusive access to it.
For this application you could easily use a single Scanner.
See: Do not create multiple buffered wrappers on a single InputStream
The correct way is to use one read(scanner) for a input stream. Edited the previous answer to use single read option
Complete program that works is given below
package com.stackoverflow.framework;
import java.util.LinkedList;
import java.util.Scanner;
public class Record {
static Scanner input = new Scanner(System.in);
public static String readData() {
return (input.nextLine());
}
public static void main(String[] args) {
BankMethods method = new BankMethods();
int optionSelected = 0;
while (true) {
System.out.println("5. Add a New Record");
System.out
.println("6. Change the Phone Number in the Current Record");
optionSelected = Integer.parseInt(readData());
if (optionSelected == 5) {
// Scanner getRecord = new Scanner(System.in);
System.out.println("Enter First Name: ");
String firstName = readData();
System.out.println("Enter Last Name: ");
String lastName = readData();
System.out.println("Enter Phone Number: ");
String phoneNumber = readData();
method.addNewRecord(firstName, lastName, phoneNumber);
}
if (optionSelected == 6) {
System.out.println("What would you like to change your phone "
+ "number to? ");
// Scanner getRecord = new Scanner(System.in);
String newNumber = readData();
method.changePhoneNumber(newNumber);
}
}
}
}
class BankMethods {
LinkedList recordInformation = new LinkedList();
public void addNewRecord(String firstName, String lastName,
String phoneNumber) {
recordInformation.add(firstName);
recordInformation.add(lastName);
recordInformation.add(phoneNumber);
}
public void changePhoneNumber(String newNumber) {
recordInformation.set(2, newNumber);
System.out.println(recordInformation);
}
}
I am trying to execute the following code but keep getting this error:
Error: Main method not found in class ScannerDemo, please define the main method as:
public static void main(String[] args)
import java.util.Scanner;
class ScannerDemo
public class Main {
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String userName;
System.out.println("Enter a number");
username = sc.nextLine();
System.out.println("your number is" + username + "enter your next number");
username2 = sc.nextline();
System.out.println("your total is" + username2 );
}
}
I think I must be importing the Scanner class wrong, I have tried different methods but nothing has worked for me yet.
You have two class declarations in a row at the top! This is wrong. Your import is fine. Try:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
...
}
}
Your class definition is wrong. You are trying to define two classes, ScannerDemo and Main. Replace:
class ScannerDemo
public class Main
With just:
public class ScannerDemo
Also, in your main method, you should be referring to the userName variable, instead of username, and you are not defining username2. Note that Java identifiers are case sensitive:
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
String userName = sc.nextLine();
System.out.println("your number is" + username + "enter your next number");
String username2 = sc.nextline();
System.out.println("your total is" + username2 );
}
import java.util.Scanner;
public class ScannerDemo {
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String userName;
System.out.println("Enter a number");
username = sc.nextLine();
System.out.println("your number is" + username + "enter your next number");
username2 = sc.nextline();
System.out.println("your total is" + username2 );
}
}
import java.util.Scanner;
public class ScannerDemo {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
String userName;
System.out.println("Enter a number");
int userName = sc.nextInt();
System.out.println("your number is " + userName);
System.out.println("enter your next number");
int userName2 = sc.nextInt();
System.out.println("your total is " + (userName2 + userName));
}
}
Hope this helps
import java.util.Scanner;
public class ScannerDemo {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
String userName;
System.out.println("Enter a number");
int username = sc.nextInt();
System.out.println("your number is " + username);
System.out.println("enter your next number");
int username2 = sc.nextInt();
System.out.println("your total is " + (username2 + username));
}
}