Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am following this tutorial, for testing and this was a little test/lesson we were doing. I keep getting errors on the 11th line. Can someone tell me whats going on? This is the code, except its not complete.
import java.util.Scanner;
public class Test2Coding {
public static void main(String[] args) {}
{}
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
}
You have some extra brackets in the code. Your 3 lines of code should be directly inside the brackets after the main method declaration.
public static void main(String[] args) {
// code here
}
Just remove the brackets you are not using
import java.util.Scanner;
public class Test2Coding {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Trying to make it so pasting a 60 character long url, will print only the first 56 characters. I have only ever used Java in high school, so I am very inexperienced.
import java.util.Scanner;
public class CopyOfCopyOfethanrun
{
public static void main()
{
Scanner kb = new Scanner(System.in);
link();
}
static void link(Scanner kb, String[] args)
{
String link;
System.out.print("\n\n Enter Link: ");
link = kb.nextLine();
String input = link;
String firstfiftysix = "";
if (input.length() > 56)
{firstfiftysix = input.substring(0, 56);}
else{firstfiftysix = input;
}
System.out.println(firstfiftysix);
}
}
[Here is an image showing the error I am experiencing][1]
[1]: https://i.stack.imgur.com/T6ux7.png
Your link method has 2 arguments: When invoking link, you need to provide [A] a Scanner instance, and [B] an instance of an array of Strings.
When you call link();, you provide neither of those. Presumably you want link(kb, args); there. Your main must look like:
public static void main(String[] args)
it currently doesn't. Once you fix that, voila, you have your args. But, you should just get rid of that, you don't use it anywhere in link.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anyone tell me how to debug this program. I have encountered an error using string method.
import java.util.Scanner;
class StringUser{
public void show();
{
System.out.print("\nYou Entered: "+a);
}
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.print("Enter a String: ");
StringUser c=new StringUser();
String a=obj.nextLine();
c.show();
}}
Your a variable is local to the main method. If you want to access it in another method, you can pass it to that method :
public void show(String a) // also note that you mistakenly had a semi colon here
{
System.out.print("\nYou Entered: "+a);
}
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.print("Enter a String: ");
StringUser c=new StringUser();
String a=obj.nextLine();
c.show(a);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Every time I try running this I get a java exception code. Any Ideas?
package employ;
import java.util.Scanner;
public class Employ {
public static void main(String[] args) {
String []empna={};
int numofemp;
int []empnu;
String []empadd;
int []emphd;
//Scanner sc = new Scanner(System.in);
System.out.println("how many employees do you have?");
Scanner sc = new Scanner(System.in);
numofemp=sc.nextInt();
for (int j=0;j<numofemp;j++){
empnam (empna,j);
// System.out.println(empna[0]);
}
}
public static void empnam(String empna[], int j ){
System.out.println("What is your employees first and last name?");
Scanner n = new Scanner(System.in);
//String ns=n.nextLine();
empna[j]=n.nextLine();
}
}
You didn't initialize the array with the correct size, this line is wrong:
String []empna={};
Try this instead, right after the line where you read the value of numofemp:
String[] empna = new String[numofemp];
Remember, an array in Java is of fixed length and its size must be specified at the creation time, it won't grow as elements are being added to it. If a variable-length array were needed, then use an ArrayList.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple program in progress that needs the declaration lines
read = new BufferedReader(new FileReader("marks.txt"));
and
line = read.readLine();
to be class variables. How would I do this?
Here is the code I wrote so far.
import java.io.*;
import java.math.*;
public class WriteKong
{
public static String line;
public static BufferedReader read;
public static PrintWriter write;
public static void main(String[] args) throws IOException
{
read = new BufferedReader(new FileReader("marks.txt"));
line = read.readLine();
while(line != null)
{
System.out.println(line);
read.readLine();
}
}
public static void sort()
{
// THIS IS WHAT THE FUNCTION DOES:
// > check the fourth digit in the line
// > if there is no fourth digit then store the mark
// > if mark is less than 50 then write to "fail.txt"
// > if mark is 50 or greater then write to "pass.txt"
}
}
EDIT: I want these variables to be declared as a class variable. I don't want to go through the pain of redefining the same variables in all of the methods I use.
They are class variables in your code. The code satisfies the requirements given.
If you're confused why your loop does not read all the lines from the file it's because you never assign the newly read line to to line.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to print out only
Int from String and first 2 char from string as well so, how i can do that, please help me thank!!
suppose,
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class match1 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String address = "1234 main street";
}
}
Output should be like:
1234 ma
How can i do that please help me!! Thanks!!
String address = "1234 main street";
String[] tokens = address.split(" ");
System.out.println(tokens[0]+" "+tokens[1].substring(0,2));