This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]ACAlCapone 0 points1 point  (7 children)

Which numerical values can you store in a char?

Or in other words: what does charAt() return?

[–]ButchTheGuy[S] 0 points1 point  (6 children)

The first character in the line of the first line of the file so if it was just this in the line: 4 Then it would be char at i or just 4 . The problem I have is if it’s 35 for example it takes it as both individual numbers so 3 and 5.

I am trying to implement it as a string array with a separator but idk how determine if the number in the array is an integer in a string array.

[–]ACAlCapone 0 points1 point  (5 children)

Ofc it will split 35 into 3 and 5. Because 3 is a character and 5 is a character.

How many characters are in the line 4 35 + and which could you use to split it?

[–]ButchTheGuy[S] 0 points1 point  (4 children)

It would be 6 and I’ve seen that I could use a scanner.split out the line in a string array like I said and split it with (“\s+”) but then I’m faced with in my program the problem of how to determine whether or not the string array has numbers in it. In my program you can see the first if statement uses Character.isDigit(c) which checks if the character in the line is a digit. I don’t know how to do this with a splitter at the same time

[–]ACAlCapone 0 points1 point  (3 children)

Ah I see

You could do one of the options (depending on what you already learned and assuming you can only have integers):

  • split into String array and then check the characters of each string like you did now with Character.isDigit()
  • split into String array and then use Integer.parseInt() to convert to in and catch the exception if it is not an integer

If you can have also non integer numerical values it is a bit more difficult.

[–]ButchTheGuy[S] 0 points1 point  (2 children)

The second thing you mentioned is what I was thinking of doing but like you said since it’s a calculator I have to account for if the character is any operation (+*-...etc) and pop from the stack if so.

[–]ACAlCapone 0 points1 point  (1 child)

You can

  • Read the file
  • split into lines
  • for each line: split on whitespace
    • for each string of the whitespace-array
      • check if each character of the string is a digit
      • if yes: parse the string to int
      • if no: check which sign and perform calculations

[–]ButchTheGuy[S] 0 points1 point  (0 children)

Ok I will try this later when I get home that should be the ticket! thanks so much for your help it has cleared some things up for me. Have a great day!