Java cannot find symbol integer

Compile Error: Cannot Find Symbol

I am receiving the error cannot find symbol when the code reaches the recursive call for increment and I have no idea why? Here is the code for increment. Any help will be greatly appreciated.

public void increment() < Integer first = 0; Character ch = num.charAt(num.length()-1); Integer last = Character.digit(ch, 10); if (num.length() >1) < if (last == 9) < last = 0; if (num.length() >= 2) < String temp = new String(num.substring(0, num.length()-2)); temp.increment(); >else < last++; >> > else < if (last == 9) < last = 0; first = 1; >else < last++; >> String t = new String(); String x = new String(); t = last.toString(); x = first.toString(); if (first > 0) < num.concat(x); >num.concat(t); > 

EDIT: I’m really new to java so the more basic you can make your answers, the better. Ok so the error I am receiving is: BigNatural.java.35: cannot find symbol symbol method increment() location: class java.lang.String temp.increment() And to clear up any other issues here is the whole code.

public class BigNatural < private String num; public BigNatural(String input) < num = input; >public BigNatural(BigNatural input) < num = input.toString(); >public BigNatural(Integer input) < num = input.toString(); >public BigNatural() < Integer i = 0; num = i.toString(); >public void increment() < Integer first = 0; Character ch = num.charAt(num.length()-1); Integer last = Character.digit(ch, 10); if (num.length() >1) < if (last == 9) < last = 0; if (num.length() >= 2) < String temp = new String(num.substring(0, num.length()-2)); temp.increment(); >else < last++; >> > else < if (last == 9) < last = 0; first = 1; >else < last++; >> String t = new String(); String x = new String(); t = last.toString(); x = first.toString(); if (first > 0) < num.concat(x); >num.concat(t); > public void decrement() < Character ch = num.charAt(num.length()-1); Integer last = Character.digit(ch, 10); if(num.length() >1) < if(last == 0) < String temp = new String(num.substring(0, num.length()-2)); temp.decrement(); >else < last--; >> else < if(last >0) < last--; >else < last = 0; >> String t = new String(); t = last.toString(); num.concat(t); > public String toString()
private String num; public BigNatural(String input) < num = input; >public BigNatural(BigNatural input) < num = input.toString(); >public BigNatural(Integer input) < num = input.toString(); >public BigNatural() < Integer i = 0; num = i.toString(); >public void increment() < Integer first = 0; Character ch = num.charAt(num.length()-1); Integer last = Character.digit(ch, 10); if (num.length() >1) < if (last < 9) < last++ >else < last = 0; if (num.length() >= 2) < String temp = new String(num.substring(0, num.length()-2)); temp.increment(); >> else < last++; >> else < if (last == 9) < last = 0; first = 1; >else < last++; >> String t = new String(); String x = new String(); t = last.toString(); x = first.toString(); if (first > 0) < num.concat(x); >num.concat(t); > public void decrement() < Character ch = num.charAt(num.length()-1); Integer last = Character.digit(ch, 10); if(num.length() >1) < if(last == 0) < String temp = new String(num.substring(0, num.length()-2)); temp.decrement(); >else < last--; >> else < if(last >0) < last--; >else < last = 0; >> String t = new String(); t = last.toString(); num.concat(t); > public String toString()

Источник

Читайте также:  How to stack in java

cannot find symbol — ArrayList; cannot find symbol — Integer [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.

I have imported both ArrayList and Integer. In my code I use the symbols in multiple places but I only get an error on one line. Here are the import statements:

import java.util.ArrayList; import java.lang.Integer; 
ArrayList primes = new ArrayList(); primes.add(2); primes.add(3); primes.add(5); primes.add(7); private static void addPrime(ArrayList primes)
while (lastValue < half) < addPrime(ArrayListprimes); lastValue = primes.get(primes.size()-1); > 

I have done research on the problem but the only answer I can find is that the symbols were not imported properly at the beginning of the program.

Try changing addPrime(ArrayList primes); to addPrime(primes); You don’t need to specify the type when calling a method. You may have other issues though.

3 Answers 3

A little background if you are not aware of already:

private static void addPrime(ArrayList primes)

is the method definition, where we define what the method will do for us. To make it do something for us we call the method passing the arguments, eg. addPrime(argument) . Here you got to pass actual argument variable of type which you have defined in method definition.

So addPrime(primes) will work for you.

I was not aware that the type of argument did not need to be passed when calling the method. This worked thanks!

You don’t need to specify the value type when calling a function. So instead of addPrime(ArrayList primes); just simply do addPrime(primes);

The addPrime(. ) function you have does not do anything in the example you gave.

import java.util.ArrayList; import java.lang.Integer; public class test < private static ArrayListaddPrime(ArrayList primes, int num) < //Seems like you are trying to add primes to the list? primes.add(num); return primes; >public static void main(String[] args) < ArrayListprimes = new ArrayList(); primes.add(2); primes.add(3); primes.add(5); primes.add(7); //Usage looks like primes = addPrime(primes, 13); // This will return 13 as the number 13 is the 5th element System.out.println(primes.get(4)); > > 

Coded a bit better would be creating an object class so you do not keep passing around an array list

import java.util.ArrayList; import java.lang.Integer; public class IntArrList < private ArrayListprimes; public IntArrList() < primes = new ArrayList(); > public void addPrime(int num) < primes.add(num); >public int getPrimeListValue(int index) < return primes.get(index); >public ArrayList getPrimeList() < return primes; >> public class test < public static void main(String[] args) < IntArrList arrL = new IntArrList(); arrL.addPrime(2); arrL.addPrime(3); arrL.addPrime(5); arrL.addPrime(7); // This will return 7 as the number 7 is the 4th element System.out.println(arrL.getPrimeListValue(3)); >> 

Источник

Cannot find symbol Integer.valueOf(int)

I have an old project which works well before, but I open it today and find compiler told me that ‘cannot find symbol Integer.valueOf(int)’ or ‘cannot find symbol Integer.toHexString(int)’ . It seems that there is something wrong with java.lang.Integer. I use IntelliJ and it compile failed as above, but Eclipse works fine. Besides, I use paste the same code to a new IntelliJ project, it also works fine. Is there anything wrong with configuration of my old IntelliJ project? Anyone meet this question before? Sample code are following:

public class ContainerTest < public static void main(String[] args) < @SuppressWarnings("unchecked") List numbers = new ArrayList(Arrays.asList(1, 2, 3)); // cannot find Integer.valueOf(int) here ListIterator listIterator = numbers.listIterator(); while (listIterator.hasNext()) < System.out.println("index" + listIterator.nextIndex() + " : " +listIterator.next()); listIterator.add(100); >System.out.println(numbers); > > public class BinaryFileTest < public static void main(String[] args) throws IOException < DataInputStream reader = null; try < String filename = "./target/classes/io/TreeInfo.class"; reader = new DataInputStream(new FileInputStream(new File(filename))); int i = 0, k; while (reader.available() != 0 && i++ < 4) < System.out.println(Integer.toHexString(k = reader.read())); // here System.out.println(k); >reader.close(); > catch (FileNotFoundException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >finally < reader.close(); >> > 

Источник

Java: cannot find symbol() [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.

Ok, I’m pretty new to using Java. So. I have been reading a few books and following some online tutorials so far. I have done mostly php and javascript for about 10 years. PHP doesn’t seem to be a heavy OOP language and JS with it’s whole prototyping was always weird to me. I thought learning Java would be a good healty way to wrap my head around a solid OOP language. Anyhow — Question is accessing and printing my variable p. If I have it as a String as below, it works great. When I change it everywhere to be and Int. It breaks. Why?

 class Player < private int level; public Player(int level) < this.level = level; >public void toString(int value) < this.level = value; >> public class RPG < public static void main(String[] args) < // Set up Keyboard input Scanner keyboard = new Scanner(System.in); System.out.println("What is your current level?"); int l = keyboard.nextInt(); Player p = new Player(l); System.out.println("Your level is: " + p.toString); >> 
RPG.java:40: cannot find symbol symbol : variable toString location: class Player System.out.println("You level is: " + p.toString); ^ 1 error 
RPG.java:18: cannot find symbol symbol : class Int location: class Player private Int level; ^ RPG.java:20: cannot find symbol symbol : class Int location: class Player public Player(Int level) < ^ RPG.java:28: cannot find symbol symbol : class Int location: class Player public Int GetLevel() < ^ 3 errors 

It is common to just pass Strings through an Object and that's it? If that's the correct wording, please correct me if I'm wrong. I want fully understand the terminology too. Also another question. Say I wanted to take a user input and plug that into their Player level. (say I could then do the math to alter their player stats). Would I then need to make use of a getter and setter even needing in this case?

Источник

Оцените статью