i was running the PMD tool for my code and checking it for a simple program that inputs two strings for beginning purposes.when i do something like a=in.nextLine(); it shows object not created locally.Can you help?
import java.util.Scanner;
class Test {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String a, b;
a = in.nextLine();
b = in.nextLine();
in.close();
}
}
Related
This question already has answers here:
Scanner doesn't see after space
(7 answers)
Closed 5 years ago.
I'm new to Java so this is a very basic question. I get and error when entering a name with spaces. What do I need to change please? steering me to info is great too.
how do I allow input with spaces.
import java.util.Scanner;
public class yes {
public static void main(String[] args) {
Name = in.next();
....
}
}
Please use in.nextLine();
import java.util.Scanner;
public class Yes {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = in.nextLine();
....
}
}
Here's what you need to change :
import java.util.Scanner;
public class Yes {
public static void main(String[] args) {
String name;
Scanner in = new Scanner(System.in);
name = in.nextLine();
....
}
}
Here's why : The next() method reads the input on current line till a space is detected. It advances to next line only when you press Enter/Return.
On the other hand, nextLine() method advances to the next line and returns the input on the current line.
You can read more about Scanner class here.
Also, it is good to follow Java naming conventions. Thanks!
Use the Scanner.nextLine(); method.
import java.util.Scanner;
public class yes {
public static void main(String[] args) {
String name = in.nextLine();
....
}
}
Hope this helps!
I am very new in programming into Java.
My question is that I have a code (see below) and I want to compare them with if statement. An errors occur at line 9 (string test) and 11(if(test.equals). I completely do not have idea.
I have made a code with int and it works perfect, but that.
package bucky;
import java.util.Scanner;
class apples {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
String test = sc.nextLine();
if (test.equals("YES")) {
System.out.println("YES");
} else {
System.out.println("TIS IS ELSE");
}
}
}
You are almost there... define YES as string and that it
String test = sc.nextLine();
String YES = "yes";
if (test.equals(YES)) {
or even better use equalsIgnoreCase() so you can get rid off the case sensitive input
if (test.equalsIgnorecase(YES))
Basically I create a custom object in one program and instantiate with values in one program, then I create another program and want to print the instantiated object output.
import java.util.Scanner;
class example {
int value;
String name;
String city;
}
public class Yummy21 {
static example[] obj=new example[3];
static Scanner input=new Scanner(System.in);
public static void main(String args[])
{
init();
}
public static void init()
{
for(int i=0;i<3;i++)
{
obj[i]=new example();
}
for(int i=0;i<3;i++)
{
System.out.println("for the object "+i+" enter the name ");
obj[i].name=input.next();
System.out.println("for the object "+i+" enter the city ");
obj[i].city=input.next();
System.out.println("for the object "+i+" enter the name ");
obj[i].value=input.nextInt();
}
}
}
And the next program:
public class Yummy22 extends Yummy21 {
public static void main(String args[])
{
for(int j=0;j<3;j++)
{
System.out.println("the value of the object["+j+"].name is "+obj[j].name);
}
}
}
The first program works fine, meaning it takes the values and the second program shows NullPointerException.
Yummy22.main does not invoke Yummy21.init. perhaps put a
static {
init();
}
in Yummy21.
This being said, these 3 classes are abominations of Java. Please at least follow naming conventions. Class example should be Example. Also better to code all this with object level fields, constructors and non static members
serialize the data from program 1 and write it to disk and then read it from program 2.
So I have this assignment, and it says to create a arraylist using standard input, and constantly prompt the user for commands. Now, I understand how to add commands, WITHIN the code, but I dont understand how to take what the user puts in, and turn that into a command in the arraylist. Here's what I got so far:
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner s = new Scanner(System.in);
Scanner n = new Scanner(System.in);
System.out.println("Enter a command:");
String command = s.nextLine();
System.out.println("If you entered 'add', then enter a name:");
String input = n.nextLine();
}
}
EDIT New Code:
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner e = new Scanner(System.in);
System.out.println("Enter command, or type 'quit' to exit");
String quit = e.next();
boolean exit = quit;
if(exit = true){
System.exit();
}
}
}
You can request that the user types in his commands and then check using the method from the String class equalsIgnoreCase() and compare it and see if it is the same as add, remove, etc. (whatever commands you need). Use if-else statements to implement the logic. For example if user input resolves to add, then you add an element. If user input is not any of the common functions, then print message that there was an error and so on. I cannot type out the answer in code as this is an assignment.
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner e = new Scanner(System.in);
System.out.println("Enter command, or type 'quit' to exit \n");
while (!e.nextLine().equalsIgnoreCase("quit")) { // As long as input not equal to quit
if (e.nextLine().equalsIgnoreCase("add") {
System.out.println("Enter name to add:\n");
String name = e.nextLine();
names.add(name);
}
if (e.nextLine().equalsIgnoreCase("remove") {
System.out.println("Enter name to remove:\n");
String name = e.nextLine();
names.remove(name);
}
}
if(e.nextLine().equalsIgnoreCase("quit"){
scanner.close();
System.exit();
}
}
}
Check out the add() method in the java.util.List interface. Might help.
I'm on a project that requires passing values from main class to other classes. All of the classes are correctly working when they have their main functions. However, my project should have only one main class that calls other functions in other classes.
An example for main and another class is below. I want to scan the input through main class and pass it to parse class. Current implementation gives two error which are:
Exception in thread "main" java.lang.NullPointerException
at url.checker.Parse.GetInput(Parse.java:16)
at url.checker.Main.main(Main.java:14)
Java Result: 1
Given input: -url=google.com,-time=5,-email=asdfg#hotmail.com
Main.java:
package url.checker;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the parameters as '-url=' , '-time=' , '-email='.");
Parse.s = in.nextLine();
Parse.GetInput(Parse.s);
}
}
Parse.java:
package url.checker;
import java.util.Scanner;
public class Parse
{
static String s;
static String result[];
public static void GetInput(String s)
{
int i;
for(i=0;i<3;i++)
{
if (s.startsWith("-url="))
result[0] = s.substring(5, s.length());
else if (s.startsWith("-time="))
result[1] = s.substring(6, s.length());
else if (s.startsWith("-email="))
result[2] = s.substring(7, s.length());
else
System.out.println("You didn't give any input.");
}
}
}
Thank you very much for your help.
static String result[];
You never initialized the array; Since its a static it gets initialized to null by default.
Initialize using static String[] strings = new String[10];. This will hold 10 elements of String.