I keep getting the error
"cannot find symbol
symbol : variable input
location: class CountNumbers" in my program and I have put it all throughout the program.
import java.util.Scanner;
public class CountNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char[] chars = createArray();
System.out.println("The numbers are:");
displayArray(chars);
int [] counts = countNumbers(chars);
System.out.println();
System.out.println("The occurences of each number are:");
displayCounts(counts);
}
public static char[] createArray() {
char[] chars = new char[100];
for (int i = 0; i < chars.length; i++)
chars[i] = input.nextInt();
return chars;
}
public static void displayArray (char[] chars) {
for (int i = 0; i < chars.length; i++) {
if ((i + 1) % 20 == 0)
System.out.println(chars[i]);
else
System.out.print(chars[i] + " ");
}
}
public static int[] countNumbers(char[] chars) {
int[] counts = new int[100];
for (int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;
return counts;
}
public static void displayCounts(int[] counts) {
for (int i = 0; i < counts.length; i++) {
if ((i + 1) % 10 == 0)
System.out.println(counts[i] + " " + (char)(i + 'a'));
else
System.out.print(counts[i] + " " + (char)(i + 'a') + " ");
}
}
}
Thanks for the help.
input is a local variable to the main method which you then try to use in the createArray method. If you want input to be accessible in other methods, it needs to be a member or static variable.
Or in your case, since you only use input in createArray, you can move the creation of input to the createArray method.
Related
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 10 months ago.
I was solving this question(the title) and I cant find the mistakes present so please help
Input : The buffalo is stuck in a soggy field and roads are flooded
everywhere.
Output : buffalo, soggy, flooded
There are 3 words that have consecutive letters.
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence");
String str = sc.nextLine();
str = str.toUpperCase();
String s = " ";
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch != ' ')
s = s + ch;
else {
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == s.charAt(j + 1)) {
System.out.println(s);
count++;
s = " ";
}
}
}
}
System.out.println("There are " + count + " consecutive words");
}
}
Output
Enter the sentence
I love apples
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:1512)
at com.company.Main.main(Main.java:18)
Process finished with exit code 1
I tried solving it but it gets complicated as such
Here:
for (int j = 0; j < s.length(); j++){
if (s.charAt(j) == s.charAt(j + 1)){
System.out.println(s);
count++;
s = " ";
}
}
You are looping through a string comparing the j index character with the j+1 index character. When j is the index of the last character j+1 will be out of range. If you want to do the comparation this way, you just have to go to the penultimate index (where you will compare the penultimate with the last).
This way the code should work:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence");
String str = sc.nextLine();
str = str.toUpperCase();
String s = " ";
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch != ' ')
s = s + ch;
else {
for (int j = 0; j < s.length()-1; j++) {
if (s.charAt(j) == s.charAt(j + 1)) {
System.out.println(s);
count++;
}
}
s = " ";
}
}
System.out.println("There are " + count + " consecutive words");
}
And as you can see I have moved the s = " "; piece of code outside of the for and if block, leaving it just inside the else block. If you don't do that the string variable s will not be cleaned after each word.
I can't run the following piece of code. I would like to know why I keep getting the .noSuchElementException error. I've seen in another post is due to the fact I'm using the same Input stream for both inputs, but creating a new Scanner or using the .close method doesn't seem to fix my problem.
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
int tooBig = 0; // parts too big
int tooSmall = 0; // parts too small
int perfectParts= 0; // perfect parts
int a = scanner.nextInt();
scanner.close();
for (int i = 0; i <= a ; i++) {
int j = scanner.nextInt();
if(j == 1) {
tooBig++;
} else if (j == -1) {
tooSmall++;
} else if (j == 0) {
perfectParts++;
}
scanner.close();
}
System.out.println(perfectParts + " " + tooBig
+ " " + tooSmall);
}
}
Edit after having removed the scanner.close() method. I still get the same error:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
int tooBig = 0; // parts too big
int tooSmall = 0; // parts too small
int perfectParts= 0; // perfect parts
int a = scanner.nextInt();
for (int i = 0; i <= a ; i++) {
int j = scanner.nextInt();
if(j == 1) {
tooBig++;
} else if (j == -1) {
tooSmall++;
} else if (j == 0) {
perfectParts++;
}
}
System.out.println(perfectParts + " " + tooBig
+ " " + tooSmall);
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int tooBig = 0; // parts too big
int tooSmall = 0; // parts too small
int perfectParts= 0; // perfect parts
int a = scanner.nextInt();
for (int i = 0; i <= a ; i++) {
int j = scanner.nextInt();
if(j == 1) {
tooBig++;
} else if (j == -1) {
tooSmall++;
} else if (j == 0) {
perfectParts++;
}
}
System.out.println(perfectParts + " " + tooBig
+ " " + tooSmall);
scanner.close();
}
}
scanner.close(); Should only be called at the very end. It's also not even necessary in your specific case and likely the reason why you're getting that exception.
Explanation:
if the input string is 'hello worlds', output will be 2.
Length of the word "hello" = 5
Length of the word "worlds" = 6
add their length to get total length = 5+6 = 11
which is not a single digit, so continuously add all digits till we get single digit i.e. 1+1=2
Therefore, the single digit is = 2 (as answer/output).
I tried with my code as follows:
import java.util .*;
class Codestring {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter word");
String word = sc.nextLine();
int len2 = 0, len1 = 0, count = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == ' ') {
len2 = count;
System.out.println(len2);
count = 0;
} else {
count++;
}
}
len1 = count;
System.out.println(len1);
int c = len1 + len2;
System.out.println(c);
ArrayList<Integer> array = new ArrayList<Integer>();
do {
array.add(c % 10);
c /= 10;
}
while (c > 0);
System.out.println(array);
while (array.size() >= 2) {
array = reduce(array);
return array;
}
}
private static ArrayList<Integer> reduce(ArrayList<Integer> array) {
for (int i = 0; i < array.size(); i++) {
array = array[i] + array[i + 1];
}
return array;
}
}
I reached to my output as:
Enter word
hello worlds
5
6
11
[1, 1]
Below is the code that might answer your question
import java.util .*;
class Codestring {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter word");
String word = sc.nextLine();
String strSplits[] = word.split(" ");
int length = 0;
for (String strTmp : strSplits) {
System.out.println("Word: " + strTmp + ", Length: " + strTmp.length());
length += strTmp.length();
}
System.out.println("Total words: " + strSplits.length);
System.out.println();
System.out.println("Consolidated Length: " + reduce(length));
sc.close();
}
public static long reduce(long length) {
while (length > 9) {
System.out.println("Initial Length: " + length);
long y = 0, factor = 1;
// go through each digit from the bottom and calc the diff.
while (length > 9) {
y += factor * Math.abs(length % 10 - length / 10 % 10);
length /= 10;
// each digit is worth 10x the last.
factor *= 10;
}
length = y;
}
return length;
}
}
import java.util.*;
public class Main
{
public int digit(int num)
{
int s=0;
while(num>0)
{
int r=num%10;
s+=r;
num=num/10;
}
if(s>9)
{
s=digit(s);
}
return s;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String input1=sc.nextLine();
String a[]=input1.split("\\s");
int num=0;
for(int i=0;i<a.length;i++)
{
int n=a[i].length();
num+=n;
}
Main ob=new Main();
int n=ob.digit(num);
System.out.println(n);
}
SIMPLE METHOD TO SOLVE THIS :).IN this we first add the length of the string and then add that sum up to a single digit.
I'm making a program that counts the frequency of letters from a user-entered string, and have recently encountered the 'Arithmetic Exception' error.
I cannot for the life of me figure out what's causing it, even though I know it's because something is being divided by 0.
Here's my code:
package day1.examples;
import java.util.Scanner;
public class rl_frequency_count {
public static int input;
public static void main(String[] args) {
System.out
.println("Please enter some text that you would like to work out the occurence for.");
System.out
.println("However, do remember that any other characters outside of the alphabet will NOT be counted.");
Scanner stringUser = new Scanner(System.in);
String input = stringUser.nextLine();
input = input.replaceAll("\\s+", "");
input = input.toLowerCase();
// counting occurrence of character with loop
int i;
int charCountA = 0;
int charCountB = 0;
int charCountC = 0;
int charCountD = 0;
int charCountE = 0;
int charCountF = 0;
int charCountG = 0;
int charCountH = 0;
int charCountI = 0;
int charCountJ = 0;
int charCountK = 0;
int charCountL = 0;
int charCountM = 0;
int charCountN = 0;
int charCountO = 0;
for (i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'a') {
charCountA++;
getOccurence(charCountA, "A");
}
}
for (i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'b') {
charCountB++;
getOccurence(charCountB, "B");
}
}
for (i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'c') {
charCountC++;
getOccurence(charCountC, "C");
}
}
for (i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'm') {
charCountM++;
getOccurence(charCountM, "M");
}
}
}
// method for the occurrence
public static void getOccurence(int number, String letter) {
double occ = number / input * 10; //
System.out.println();
System.out.println("Number of " + letter + "'s - " + number);
System.out.println("Occurence of " + letter + " - " + occ + "%");
}
}
I know that I only have ABC and M in at the moment but was gonna work those in later.
This is the first time i've posted on here and i'm still newish to Java so any help whatsoever is greatly appreciated!
I ran it and it says line 67. here is the total:
public static void getOccurence(int number,String letter){
double occ = number / input *10; //
System.out.println();
System.out.println("Number of "+ letter +"'s - "+ number);
System.out.println("Occurence of "+ letter +" - "+ occ + "%");
}
To fix:
double occ = (number > 0) ? number/input * 10 : 0;
This sets occ to 0 in case of number being set to 0. Good luck.
Hope this helps.
The line of code causing the error is in your method:
public static void getOccurence(int number,String letter){
double occ = number / input *10; // <------ERROR FROM HERE (input is always 0)
System.out.println();
System.out.println("Number of "+ letter +"'s - "+ number);
System.out.println("Occurence of "+ letter +" - "+ occ + "%");
}
The input variable is declared in your class here:
Line 6: public static int input;
Since you didn't initialize it nor does the value is being changed in your codes, the value of input remains as 0 through out the entire program. (Default value for an uninitialized int variable is 0)
Since it is always 0, you are always dividing a number with 0.
double occ = number / 0*10;
public static void main (String args[])
{
String c = "Message";
int width;
int height;
char character;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the character : ");
character = sc.next().charAt(0);
System.out.println("Enter your width: ");
width=sc.nextInt();
System.out.println("Enter your height: ");
height=sc.nextInt();
System.out.println();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 || i == height-1) {
System.out.print(character);
} else if (j ==width-1) {
String middle = character + " " + c + " " + character;
System.out.print(middle);
}
}
System.out.println();
}
}
}
I am trying to make the MESSAGE display in the rectangle. Also, is there a way i can move my rectangle in the center of the screen too?
That code will do your trick but please notice that this is ugly.
First you are taking the whole user input line instead of the first character.
public static void main(String args[]) {
String c = "Message";
char character;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the character : ");
character = sc.next().charAt(0);
System.out.println();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 13; j++) {
if (i == 0 || i == 2) {
System.out.print(character);
} else if (j == 0) {
String middle = character + " " + c + " " + character;
System.out.print(middle);
}
}
System.out.println();
}
}
output :
aaaaaaaaaaaaa
a Message a
aaaaaaaaaaaaa
Don't use the [for loop], the StringUtils class in Apache Commons Lang3 can help you make repeating String.
See:
http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html
there are 3 methods:
(1)static String repeat(char ch, int repeat)
Returns padding using the specified delimiter repeated to a given length.
(2)static String repeat(String str, int repeat)
Repeat a String repeat times to form a new String.
(3)static String repeat(String str, String separator, int repeat)
Repeat a String repeat times to form a new String, with a String separator injected each time.