how to use arrays in methods [closed] - java

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.

Related

Do while loop condition not matching with variables inputted through scanners [closed]

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 12 hours ago.
Improve this question
I am a beginner in Java and this issue arose when I was working on a HackerRank problem that I have solved but it still confuses me why it wouldn't work the first iteration of code I made to solve it. This code function is to separate a string and an integer into two columns with the integer being limited to three digits and string having 10 chr limit, covered by "========". Also, I intend the code only ends when the user has inputted "nothing" or white spaces.
However, the do while loop keeps going as its condition does not match the inputted variables created by the scanner, being white spaces. I had a clue that it might be when I used an integer scanner as it interfered with the string scanner, but I tried clearing the scanner by using nextLine(), and it wouldn't work with the loop. I tried using scanner.reset() and also declaring the integer first as a string and then converting it back to an integer but the loop keeps going. I tried simplifying it, and I found out that the loop ends when I use "word = scanner.nextLine();" but it wouldn't work with the loop. Hope you guys can educate me and possible ways to fix this issue.
package hackerRankTestCode;
import java.util.Scanner;
import java.lang.*;
import java.util.concurrent.atomic.DoubleAdder;
public class TestCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String word = "";
Integer number = 0;
String baruNumber = "";
System.out.println("================================");
do {
word = scanner.next();
number = scanner.nextInt();
String blank = new String(new char[15 - word.length()]).replace("\0", " ");
if(number<100 && number>=10){
baruNumber = "0"+number;
System.out.println(word+blank+baruNumber);
}
else if(number>=0 && number<10) {
baruNumber = "00"+number;
System.out.println(word+blank+baruNumber);
}
else if(number>=100) {
System.out.println(word+blank+number);
}
}
while(!word.isBlank() && number != 0);
scanner.close();
System.out.println("================================");
}
}

Why am I getting an error for actual and formal argument lists differ in length? [closed]

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.

Why `.read()` Method of a number return different number [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Please explain the use of System.in.read() method in this example that I'm learning about from another post. As a beginner, I find it unclear when I input a number and get different one in the output, please clarify.
import java.io.IOException;
public class MainClass {
public static void main(String[] args) {
int inChar;
System.out.println("Enter a Character:");
try {
inChar = System.in.read();
System.out.print("You entered ");
System.out.println(inChar);
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
System.in.read() reads values as their binary value; for example reading "a" will result in the value of "97" - the mapping of this is available here https://www.asciitable.com/.
In order to get the textual representation of this in Java you want to read this as either a Character or a String - Character is a single value, while a String is a combination of Characters one after the other. For example:
public class MainClass {
public static void main(String[] args) {
System.out.println("Enter a Character:");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.print("You entered ");
System.out.println(input);
}
Have a look at the Scanner class to see other options, you can use scanner.nextInt() to get an Integer back.

Chopper Java: what is wrong with this? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
import java.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers");
Scanner chopper = first Scanner();
int x =chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
This keeps telling me that a ';' is expected on the line that starts with scanner chopper, what could the problem be?
P.S. Do you know how i can get it to keep count of how many integers were typed in?
You have written first Scanner() instead of new Scanner(System.in).
You need to specify an input source, and since you want the user to input the numbers, you should use System.in as argument.
This line:
Scanner chopper = first Scanner();
should be:
Scanner chopper = new Scanner(System.in);
new Scanner(System.in) creates a new Scanner object that take input from the console, first Scanner() is syntactically incorrect and is what gives the error.
Why does your code say first Scanner(); instead of new Scanner();? I think that's the problem.

How to make Scanner read more than one line? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have a text corpus, which I have to read, split, sort and perform other operations on it.
In the very beginning, when I split it, I see that the Scanner only reads one line. This is the code:
public class CorpusTest {
public static void processCorpus(Scanner scanner) throws IOException{
String line="0";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
}
String[] w = line.replaceAll("[^a-zA-Z\\s]","").toLowerCase().split(" ");
for (int i = 0; i < w.length; i++) {
w[i].trim();
}
System.out.println("Word" + "\t" + "Frequency");
System.out.println(Arrays.toString(w));
}
public static void main(String [] args) throws IOException{
File temp = new File("input.txt");
Scanner scanner = new Scanner(temp);
CorpusTest.processCorpus(scanner);
}
}
I tried adding:
String text = new Scanner( new File("input.txt") ).useDelimiter("\\A").next();
But I get errors because in the method above I am working with an array.
The while loop only reads the last line, which is no good.
I'm not sure what your issue is, and it seems as if you might be trying to make things more difficult than they need to be. Why not simply read your lines in with the Scanner, one at a time, put them into a StringBuilder, and then when the text has been read in, convert to a String and manipulate your String to your hearts content?
#user2864740 helped me out with redirecting me to the right source. I used this instead of the loop in the beginning of my code:
String content = new Scanner(new File("input.txt")).useDelimiter("\\Z").next();
String[] w = content.replaceAll("[^a-zA-Z\\s]","").replaceAll("\n","").toLowerCase().split(" ");
Now it works.

Categories

Resources