changin string to int and unknown number of int

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 638 Views - Last Post: 03 April 2019 - 03:15 AM Rate Topic: -----

#1 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

changin string to int and unknown number of int

Posted 28 March 2019 - 10:05 AM

Hello! This is my first time posting here so I hope I do this right!
I need some help with my assignment as I have hit a wall.

What I have to do is to get a user-generated sting turned in to int. have the program return Result: and Result/10:.
I have to use .nextLine or .next.and can not use Integer.parseInt.
I am pleased I have gotten this far but I have like previously mentioned gotten stuck.

 import java.util.Scanner;
/* Make a program that prompts the user to input a string of numbers.
Convert the sting into int print out the int.
Prove that it is int by dividing the number with 10.

28-3-2019 */

public class Lab4_5 {
  public static void main(String[] args){

    // Start scanner
    Scanner scanObj = new Scanner(System.in);

    // Prompt user to imput text, save in string text.

    System.out.println("Please input a string of numbers.");

    String numbers = scanObj.nextLine();

       String numbersRev = "";

       for(int i = numbers.length() - 1; i >= 0; i--)
       {
           numbersRev = numbersRev + numbers.charAt(i);
       }

    // make sure to only use numbers incase user input letters.
    int i=0;
    int step=0;
    char x =numbers.charAt(i);
    if ((x >= 48 && x<=57)) //<- this here is an issue as it will only work if the code is just numbers instead of just disregarding non number characters that was my intention.


    while  (step <= numbersRev.length())
      {
          for (i = numbersRev.length()-1; i !=0; --i)
        {
        step[i]=numbersRev(Math.pow(10,i));// <- this was a great idea but sadly it  turns out that [i] gives back arrays and not int... 

          //add the steps sum together //<- later issue when I find sums to actually add.
        }
      }
      System.out.println();//<- will add the proper println lines when I have gotten over herdel.
    }
}


This is the errors I am currently having:

Lab4_5.java:39: error: array required, but int found
step[i]=numbersRev(Math.pow(10,i));
^
Lab4_5.java:39: error: cannot find symbol
step[i]=numbersRev(Math.pow(10,i));
^
symbol: method numbersRev(double)
location: class Lab4_5
2 errors

I tried googling for how to make an unknown amount of int but I only come up with the array answer so I think I am not wording it properly.
I do not know why it says: numbersRev can't find symbol
If I can have pointers from patient people that will not mind me asking followup questions that would be great!

//Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: changin string to int and unknown number of int

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 10:19 AM

You're making your life a little hard. There are already methods in Character to find digits etc. See Javadoc

Before advising, please tell describe the GOAL of your code

This post has been edited by g00se: 28 March 2019 - 10:21 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 10:36 AM

the goal is to write a program that Prompts the user to input a string of numbers.
Have the string checked for non-number characters and disregard them if they are there.
change the string of number into an int variable of numbers.
print out the int variable.
prove that it is an int by having it divided by 10 and printing that value as well.

Will also edit the main post to have the goals listed
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2836
  • View blog
  • Posts: 11,132
  • Joined: 03-December 12

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 11:35 AM

What are your limitations or constraints?


I mean, you could do a string change and compare the two initially to see if anything needs removing.

This post has been edited by astonecipher: 28 March 2019 - 12:22 PM
Reason for edit:: removed the parseInt method

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 11:50 AM

If you cannot use parseInt then you probably need to assemble the digits, test each one according to its position and create a new integer with calculations.
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 12:24 PM

Of course you won't be able to parseInt or even parseLong if it overflows

Your safest and easiest option is:


BigInteger bi = new BigInteger(numbers); // The correct way to instantiate - from a String


Why reverse the digits btw?

This post has been edited by g00se: 28 March 2019 - 12:24 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#7 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 12:26 PM

I am under the impression that we are suposed to use Acsii to find out whether or not we are working with just numbers.
How would you go around testing all in the string when you do not know how many dodgits there are in the string?


if ((x >= 48 && x<=57))
Is my very wrong way of trying to sort out to just numbers
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 12:28 PM

Depends on what you're going to do IF you find there are things other than numbers there
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 12:33 PM

If you are to remove all non-digit characters then the length of the remaining string tells you how many further calculations you need to perform.
Was This Post Helpful? 0
  • +
  • -

#10 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 12:53 PM

I am also suposed to use this system of getting the number
Say the string number is 1234 Then he wants us to some how have it be 1*10^3 +2*10^+2*10^1+4 and then add them all together. Hence why I thougth to revers the string. It is also the reason for math.pow.
I am sorry its comming out so wonky I will try to do better in the future
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 01:26 PM

Yes, that is effectively what my description is leading towards.

Although, you're formula has an error or typo.

If you haven't already you could write code to leave just digits. Then you could regroup to determine how to complete the task.
Was This Post Helpful? 0
  • +
  • -

#12 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: changin string to int and unknown number of int

Posted 28 March 2019 - 01:28 PM

Quote

Hence why I thougth to revers the string.

Yes i see. Actually it doesn't need to be reversed. It's just a matter of correctly indexing it. I'd probably index it in both directions
Was This Post Helpful? 0
  • +
  • -

#13 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: changin string to int and unknown number of int

Posted 29 March 2019 - 06:27 AM

I really appreciate the help in this guys! I decided I needed to sleep on it as I was getting frustrated and my mind was stuck in the same nonfunctioning track. Taking in what you guys said and with some coaching from a friend my code now looks like this (I decided to start fresh today) :
import java.util.Scanner;
/* Make a program that prompts user to input a string of numbers.
Convert the sting in to int print out the int.
Prove that it is int by dividing the number with 10.

Ellen Linderkers  [email protected]
29-3-2019 */

public class Lab4_5 {
  public static void main(String[] args){

    // Start scanner
    Scanner scanner = new Scanner(System.in);

    // Prompt user to imput text, save in string text.
    System.out.println("Please input a string of numbers.");

    // Assume input : 123abc456
    String input = scanner.nextLine();

    // Filter out any characters that are not numbers 
    String numbers = "";
    for(Char c : input) 
    {
        if(c >= 48 && x<= 57) 
        {
            numbers += c;
        }
    }

    // At this point we have a string of numbers: 123456
    for (int i = nubers.length(); i >= 0; i--) 
    {
        int value = 
        // Write the code that sums the numbers
      if(c >= 48 && x<= 57) 
        {
            numbers1++;
        }
          for (int step=numbers1; step >0; step--)
          {
            charAt(i)Math.pow(10,step)
          }
    }
}

Once again here are the restrictions:
  • User input string of numbers check
  • make sure that you only have numbers in a string check
  • change the string to int check
  • only use .next or .nextline to get string check
  • no using Integer.parseInt, Scanner.nextInt or Scanner.nextDouble check
  • use Ascii to achieve this check
  • have the numbers add together using following principle : say string is 1234 that would be 1math.pow(10,3)+2math.pow(10,2)+3math.pow(10,1)+4
  • HAve the system.out.print line("Result: " Sum)
  • Have the second System.out.println("Result /10: " sum/10) to prove that it is int


Note to self make nice lists like this always!!


I like to think I am much further along now than yesterday!
I would love some additional help when it comes to the formula of getting sum, how do I write it to add an unknown number of things together?
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: changin string to int and unknown number of int

Posted 29 March 2019 - 06:35 AM

Does that code run without errors? (Glancing at it, I don't think it does.)
Was This Post Helpful? 0
  • +
  • -

#15 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: changin string to int and unknown number of int

Posted 29 March 2019 - 06:47 AM

for the moment I have 2 errors
Lab4_5.java:36: error: illegal start of expression
if(c >= 48 && x<= 57)
^
Lab4_5.java:42: error: ';' expected
charAt(i)Math.pow(10,step);
^

I am very proud at how far I got that I may have wanted to show off our hard work :3
I am currently googling for how to make the unknown int thing.

*the "^" is supposed to be under the "M" in Math.pow
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2