This is the code I have so far and this is only one file of multiple ones that all belong to the same program.
As soon as I want to compile and check my errors eclipse tells me that it cannot resolve the scanner and I have no idea how to fix this problem nor what it exactly means.
import java.util.Scanner;
public class PieShop {
static FoodItem foodItem = new FoodItem();
public static void main(String[] args) {
Scanner_in.consoleLine("Enter Food item File name:");
foodItem.foodItemFile=new File(Scanner_in.getConsole());
foodItem.addFoodItem();
foodItem.displayAll();
foodItem.choices();
}
}
Below code should work fine provided that you pass right value to scanner source
import java.util.Scanner;
public class PieShop {
static FoodItem foodItem = new FoodItem();
public static void main(String[] args) {
Scanner Scanner_in = new Scanner(source);
Scanner_in.consoleLine("Enter Food item File name:");
foodItem.foodItemFile=new File(Scanner_in.getConsole());
foodItem.addFoodItem();
foodItem.displayAll();
foodItem.choices();
}
}
This would be the correct way if you wan to read the input from the console:
import java.util.Scanner;
public class PieShop {
private static FoodItem foodItem = new FoodItem();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Initialize scanner
System.out.println("Enter Food item File name:"); // Print yourtext
foodItem.foodItemFile = new File(scanner.nextLine()); // Read from scanner
foodItem.addFoodItem();
foodItem.displayAll();
foodItem.choices();
}
}
Related
import java.util.Scanner;
public class A {
static void aValue1(){
Scanner scan = new Scanner(System.in);
System.out.print("Enter value of a: ");
double numA = scan.nextDouble();
static void aValue2(){
System.out.println(numA);}
public static void main(String[] args) {
aValue1();
aValue2();
}
}
I understand that numA is a local variable and can not be used again in a different method, but is there any way I declare numA as static outside of the method and still be able to get the user input for it?
I want the user to type what numA is and I want to use it in every method.
Yes you can, you could have numA as a static field
import java.util.Scanner;
public class A {
static double numA;
static void aValue1(){
Scanner scan = new Scanner(System.in);
System.out.print("Enter value of a: ");
numA = scan.nextDouble();
static void aValue2(){
System.out.println(numA);}
public static void main(String[] args) {
aValue1();
aValue2();
}
}
I'm still trying to make the text editor to run with cmd but I'm stuck.
import java.util.Scanner;
public class TextEd {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Editor editor = new Editor();
editor.copiedText();
}
}
class Editor {
Scanner scan = new Scanner(System.in);
public void copiedText() {
System.out.println("Paste your text here");
String text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you want to do?\n"
+ "1. count characters"?;
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
The problem is: everytime i try to execute i get an error "cannot find symbol 'text". I know I need to call it frim the other method, but hod do i do that?
Yoy need to make it a class field:
class Editor {
private Scanner scan = new Scanner(System.in);
private String text = "";
public void copiedText() {
System.out.println("Paste your text here");
text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you want to do?\n"
+ "1. count characters"?;
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
Also field scan in class TextEd seems to have no purpose and should therefore be removed.
You have declared text as a local variable in copiedText(). Local variables cannot be seen in other methods. Try setting a field variable (a private variable in the Editor class) that can be seen by all methods
your code has compile error because the parentheses in System.out.println() in menu are not closed properly.
you want to call a method of a text String in another method, so you can pass text to that method or just define it as a field in your class so that other methods can reach that variable.
import java.util.Scanner;
public class TextEd {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Editor editor = new Editor();
editor.copiedText();
}
}
class Editor {
Scanner scan = new Scanner(System.in);
String text;
public void copiedText() {
System.out.println("Paste your text here");
text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you want to do?\n"
+ "1. count characters?");
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
be aware of blocks, every variable that is defined in other blocks or methods, is not reachable in other blocks or methods.
package leitura;
import java.util.Scanner;
public class main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String nome = in.nextLine();
}
}
sorry, I think you should change your class name from 'main' to any other valid identifier. and you'd better add some words to prompt you to enter in. such as System.out.print("Please enter name:");
How can I use my Scanner user_input across multiple classes? I have read a few articles, but apparently I am missing something. I even tried following a few other stackoverflow questions, and the result is below:
import java.util.Scanner;
public class HelloWorld{
public static final Scanner user_input = new Scanner(System.in);
public static void main(String []args){
String test1 = user_input.next();
System.out.println("Test 1: " + test1);
}
}
class TestClass{
public static void test_method(){
String test2 = HelloWord.user_input.next();
System.out.println("Test 2: " + test2);
}
}
If someone could help me, I would truly appreciate it.
P.S. I am new to Java, have background in Python.
From #ferdz comment, something like this would be better:
import java.util.Scanner;
public class HelloWorld {
public static final Scanner user_input = new Scanner(System.in);
public static void main(String[] args) {
String test1 = user_input.next();
System.out.println("Test 1: " + test1);
// These two lines actually instantiate the TestClass below,
// we pass in the Scanner as a parameter (user_input), and
// then it gets used in the test_method internally.
TestClass testClass = new TestClass(user_input);
testClass.test_method();
}
private static class TestClass {
public void test_method(Scanner scanner) {
String test2 = scanner.next();
System.out.println("Test 2: " + test2);
}
}
}
Your only problem is you have missed spelt the word world in your second class.
So change:
String test2 = HelloWord.user_input.next();
to:
String test2 = HelloWorld.user_input.next();
and it should work
I have a student object class and I have to create an arraylist of these students.
Im trying to create the program that inputs information into the student object then store it into an arraylist.
The first command to this array is to add a student object to the array the next command would be to remove a student from the array.
How would I get a user to input a command then go to another method that leads to storing information if everytime i try to complie i get static errors.
import java.util.Scanner;
import java.util.ArrayList;
public class CollegeTester
{
Scanner input = new Scanner(System.in);
ArrayList<Student> array = new ArrayList<Student>();
CollegeTester collegeTester = new CollegeTester();;
public static void main(String[] args)
{
new CollegeTester().getCommand();
}
public void getCommand()
{
System.out.println("Enter a command: ");
String command = input.nextLine();
if(command.equals("add"))
collegeTester.addCommand();
}
public void addCommand()
{
System.out.println("Enter a Name: ");
String name = input.nextLine();
}
}
There is another loophole. Your main method is not static. Your program will not run without it. Change it to
public static void main(String[] args)
{
}
Also then you cannot access non-static methods directly from static method.
Either you should instantiate class and then call methods or make methods static.
public static void main(String[] args){
CollegeTester collegeTester = new CollegeTester();
collegeTester.addCommand();
}
If you want to call specific method on specific options, then you should use switch case.
You are trying to access to not static method from static method. At first make an instance of CollegeTester class, than call it. For example:
import java.util.ArrayList;
import java.util.Scanner;
public class CollegeTester {
private Scanner input = new Scanner(System.in);
private ArrayList<Student> array = new ArrayList<Student>();
public static void main(String[] args) {
new CollegeTester().getCommand();
}
public void getCommand() {
System.out.println("Enter a command: ");
String command = input.nextLine();
if (command.equals("add"))
this.addCommand();
}
public void addCommand() {
System.out.println("Enter a Name: ");
String name = input.nextLine();
}
}
public class Student {
// Some properties and fty.
}