Question

I need to guess from user (number as string) and convert that into int array. If...

I need to guess from user (number as string) and convert that into int array. If user put "789" array[0] should be 7. However, since two methods are in different classes and I cannot use constructors (except for main), I have no idea how I can do this.

Code should be something like this.

import java.util.Scanner;

Class Guess {

Scanner sc= new Scanner(System.in);

public String askGuess(){

String guess = sc.nextLine();

return guess;

}

Class convert is just showing the concept.

Class Convert {

public int[] convert(//Needs to be String)
   {
       String converted = //import guess somehow
       int[] conArray = new int [convert.length()];

       for(int i = 0; i < converted.length(); i++)
       {
           conArray [i] = converted.charAt(i)
       }

       return conArray;
   }

}

Class Main {

Guess guessGet = new Guess();

System.out.printf("Guess? ");

guessGet .askForGuess();

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:-

import java.util.*;
class Guess{
   Scanner sc = new Scanner(System.in);
   public String askGuess(){
       String guess = sc.nextLine();
       return guess;
   }
}
class Convert{
   public int[] convert(String s){
       String converted = s;
       int[] conArray = new int[converted.length()];
       for(int i = 0;i<converted.length();i++){
           // parseInt() expects string as argument, but converted.charAt(i) is character,
           // so we need to convert it to string
           conArray[i] = Integer.parseInt(String.valueOf(converted.charAt(i)));
       }
       return conArray;
   }
}
class Main{
   public static void main(String[] args) {
       Guess guessGet = new Guess();
       System.out.print("Guess? ");
       String numStr = guessGet.askGuess();
       Convert con = new Convert();
       int[] numArray = con.convert(numStr);

// Print numArray
       for(int i = 0;i<numArray.length;i++){
           System.out.print(numArray[i]+" ");
       }
   }
}

OUTPUT:-

NOTE:- If you have any doubts, please comment below. THANK YOU!!! Please give positive rating.

Add a comment
Know the answer?
Add Answer to:
I need to guess from user (number as string) and convert that into int array. If...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • I wrote two java classes. One is main and other one is getter and setter. I...

    I wrote two java classes. One is main and other one is getter and setter. I brought methods from Digit class to Main class to print result. However, for some reason, only welcome print statement got printed and others got ignored. On the output screen, only welcome statement appear. nDigit should be input from user. import java.util.Scanner; public class Main {    public static void main(String[] args)    {        Digit digitGet = new Digit();              ...

  • Write a program that stores a phrase as an array of words, and then prints it...

    Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{    public static void main(String[] args) {    String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • I need to remove the first line of code (Summer 2017 Temperature ?°C vs Ice Cream...

    I need to remove the first line of code (Summer 2017 Temperature ?°C vs Ice Cream Sales) and put the rest in a array where the number before the comma is the index and the number after is the value at said index. The array should be of size 30. Code: import java.util.Scanner; public class Q3 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String[] chart = new String[30]; String input1= ""; String inputTitle = ""; while(!(input1.equals("end"))){...

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...

  • The part in bold is giving me an error. Ther error is that I cant convert...

    The part in bold is giving me an error. Ther error is that I cant convert an int to a boolean. The program asks the user to input two integer values and determines whether the first is divisible (without a remainder) by the second import java.util.Scanner; public class CheckDivisible { int isDivisible(int dividend, int divisor) { if(dividend % divisor == 0) return 1; else return 0; } public static void main(String[] args) { int dividend = 0; int divisor =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT