Main.java:18: error: array required, but String found - string

Can anyone resolve this java code i have issues in type casting .
public class Main{
public static void main(String[] args) {
int n=234;
int ans=1;
int diff;
int sum=0;
String s = Integer.toString(n);
for(int i=0;i<s.length();i++){
ans=ans*Integer.parseInt(s[i]);//not converting pls help me
sum=sum+Integer.parseInt(s[i]);//not type casting pls help me
}
diff=ans-sum;
System.out.println(ans);
}
}

s is a String if you want a number out of the string, you have to convert it to a char[] then can you access a specific index
Integer.parseInt(s.toCharArray()[i])

Related

can anyone explain this error and also about this case too like why I cant equivalate that two values?

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(remove(str));
}
static String remove(String str)
{
int start=0;
if(str.charAt(0)!=str.charAt(1)){
return str;
}
if(str.charAt(0)=str.charAt(1)){
str=str.substring(start+1,str.length());
remove(str);
}
}
}
/Root/src/Main.java:22: error: unexpected type
if(str.charAt(0)=str.charAt(1)){
^
required: variable
found: value
1 error

The method next() is undefined for the type String

I'm new to programming and I'm trying to call a scanner from a method- and receiving the error before compiling, I could not find an answer to this in this forum or outplace, that I could understand
public class BL {
static double outcome = 0;
private static String input;
private static void main() {
Scanner input = new Scanner(System.in);
}
public static void sort() {
**input= input.next();** // the error is under .next()
}
Thanks upfront!
I guess you want
public static void sort() {
Scanner in = new Scanner(System.in);
input = in.next(); //or in.nextLine() for the whole line
}
The Scanner is the class that reads input from the keyboard. It is the class who has the method next(), that retrieves the input of the user (until it finds a space or a new line) converted in a String.

cannot find symbol for method in a class of array objects

I have a class Card to represent a deck of cards as an array of objects Card[], with methods to shuffle and print the deck. I have a test class TestCard to generate the deck (in TestCard) then shuffle and print the deck (as methods in Card.) I get the message
TestCard.java:17: error: cannot find symbol
mydeck.writeDeck();
^
symbol: method writeDeck()
location: variable mydeck of type Card[]
Here is the Card.java code.
public class Card {
public static final int NUMCARDS=52;
String suit;
int value;
String name;
public Card() {
suit = " ";
value = 0;
name = " ";
}
public Card(String suit, int value, String name) {
this.suit=suit;
this.value=value;
this.name=name;
}
public void setData(String su,int va, String na) {
suit = su; value = va; name = na;
}
public void writeDeck(Card[] cards) {
int count=0;
for (Card mycard : cards ) {
System.out.print(mycard.name+mycard.suit);
count++;
if (count==13) {
System.out.println();
count= 0;
}
}
System.out.println();
}
private void swap(Card[] arr,int i, int j) {
Card temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public void shuffle(Card[] carr) {
int index;
for (int i=NUMCARDS-1; i>0; i--) {
index = (int) (Math.random() * (i+1));
swap(carr, i, index);
}
}
}
and here is TestCode.java code
public class TestCard {
public static final int NUMCARDS=52;
public static void main(String args[]) {
Card[] mydeck = new Card[NUMCARDS];
for (int i=0; i<NUMCARDS; i++) {
int j=i%13+1;
String v;
v = Integer.toString(j);
String s;
if (i<13) s="C";
else if (i<26) s="D";
else if (i<39) s="H";
else s="S";
mydeck[i] = new Card(s,j,v);
}
System.out.print(mydeck[5].suit+mydeck[5].value);
mydeck.writeDeck();
mydeck.shuffle();
mydeck.writeDeck();
}
}
I have spent 8 hours trying various fixes, using tutorials from various we sites, but have not succeeded.
mydeck.writeDeck(); is trying to call the writeDeck() method of the instance mydeck. But if you look at the code:
Card[] mydeck = new Card[NUMCARDS];
my deck is not a Card object, it's an array of Card objects. According to your code, you must pass in a Card[] as a parameter.
Turn .writeDeck(Card[] cards) into a static method:
public static void writeDeck(Card[] cards)
and then invoke the method like so:
Card.writeDeck(mydeck)
Marvin's answer worked like a charm. According to the Oracle documentation, for a method to work on an array of objects, the method must be defined for the object. But you can't just simply say arrayvar.method(). You have to say Objectclass.method(arrayvar). Good news is you can pass two array variables if you are working with both.

How to string with spaces as input and store in char array in java

String w="ab cd";
// String w=sc.next(); need this line to work
int length=w.length();
char arr2[]=new char[length];
for(int j=0;j<length;j++)
{
arr2[j]=w.charAt(j);
}
for(int i=0;i<arr2.length;i++)
{
System.out.println(arr2[i]);
}
This code works fine for string literal but fails when i take input at runtime.Any solution for it? It only prints ab i.e. before space
this is the demo program
public class demo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String w=sc.nextLine();
char arr[]=w.toCharArray();
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}

How to print the area of a circle in another method

I am trying to print out the answer for the Area of a circle in the myPrint(); method but it keeps calculating zero why?
I did my calculations in the Math(); method that I created. I am very new at coding and would greatly appreciate the help.
Thanks
package Project1;
import java.util.Scanner;
public class Code1 {
static String myStr;
static double radius;
static double answer;
static double pie;
public static void main(String[] args){
Name();
Info();
Math();
myPrint();
}
public static void Name(){
Scanner input = new Scanner(System.in);
System.out.println("What Is Your Name");
myStr = input.nextLine();
}
public static void Info(){
Scanner input = new Scanner(System.in);
String myStr;
System.out.println("What is Your Phone Number");
myStr = input.nextLine();
System.out.println("What is Your Age");
myStr = input.nextLine();
System.out.println("What is Your Postal Code");
myStr = input.nextLine();
}
public static void Math(){
double radius,answer;
double pie = 3.14;
Scanner input = new Scanner(System.in);
System.out.println("What is the Area of the Circle");
radius = input.nextDouble();
answer = pie *(radius * radius);
}
public static void myPrint(){
System.out.print("The answer is:"+ answer);
}
}
You have the static variable answer, but you declare answer again in Math(), so when you assign answer equal to your calculation, that's not changing the global variable, but it's changing the local variable. When you print answer in myPrint(), the answer will be 0 because it will have been assigned a value of 0 by default.

Resources